Create a Struts Portlet
in Liferay
Step 1)Create
a struts portlet
Step 2) put
this jar file into the lib folder
commons-digester-1.8.jar
portals-bridges-common-1.0.jar
portals-bridges-struts-1.2.7-1.0.jar
Step 3) open
liferay-plugin-package.properties file ,here you can add portal
dependencies jar file
portal-dependency-jars=\
antlr2.jar,\
antlr3.jar,\
aopalliance.jar,\
asm-commons.jar,\
commons-beanutils.jar,\
commons-codec.jar,\
asm-util.jar,\
asm-xml.jar,\
asm.jar,\
struts-el.jar,\
struts.jar,\
commons-validator.jar
portal-dependency-tlds=\
liferay-portlet.tld,\
struts-bean-el.tld,\
struts-bean.tld,\
struts-html-el.tld,\
struts-html.tld,\
struts-logic-el.tld,\
struts-logic.tld,\
struts-tiles-el.tld,\
struts-tiles.tld,\
struts-nested.tld
Step 4)Edit
the web.xml file
<servlet>
<servlet-name>PortletActionServlet</servlet-name>
<servlet-class>com.liferay.util.bridges.struts.LiferayPortletServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>PortletActionServlet</servlet-name>
<url-pattern>/portlet_action/*</url-pattern>
</servlet-mapping>
Step 5) Edit
the Portlet.xml file
Change the
<portlet-class> tag
<portlet-class>org.apache.portals.bridges.struts.StrutsPortlet</portlet-class>
<init-param>
<name>ServletContextProvider</name>
<value>com.liferay.util.bridges.struts.LiferayServletContextProviderWrapper</value>
</init-param>
<init-param>
<name>ViewPage</name>
<value>/portlet_action/struts-exercise/addname</value>
</init-param>
<init-param>
<name>EditPage</name>
<value>/portlet_action/struts-exercise/edit</value>
</init-param>
Step 6) Create
StrutsSimple-config.xml
<?xml
version="1.0"?>
<!DOCTYPE
struts-config
PUBLIC
"-//Apache
Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!--
Forms Beans -->
<form-beans>
<form-bean
name="addNameForm"
type="com.test.form.AddNameForm"
/>
</form-beans>
<!--
Action Mappings -->
<action-mappings>
<action
path="/struts-exercise/view"
forward="/html/strutsdemo/view.jsp"
/>
<action
path="/struts-exercise/addname"
type="com.test.action.AddNameAction"
name="addNameForm"
scope="request"
validate="true"
input="/html/strutsdemo/edit.jsp">
<forward
name="/struts-exercise/addname/success"
path="/html/strutsdemo/view.jsp"
redirect="true"
/>
</action>
<action
path="/struts-exercise/edit"
forward="/html/strutsdemo/edit.jsp"/>
</action-mappings>
<!--
Custom Request Processor -->
<!--
<controller
processorClass="org.apache.portals.bridges.struts.PortletTilesRequestProcessor"
/>
-->
<!--
Message Resources -->
<message-resources
parameter="content.test.Language"
/>
</struts-config>
Step
7)create a
portlet class called AddNameAction under src/com/test/action,
package
com.test.action;
import
com.test.form.AddNameForm;
import
javax.portlet.ActionRequest;
import
javax.portlet.ActionResponse;
import
javax.portlet.PortletMode;
import
javax.portlet.PortletPreferences;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
public
class
AddNameAction extends
Action {
public
ActionForward execute(
ActionMapping
mapping, ActionForm form, HttpServletRequest req,
HttpServletResponse
res)
throws
Exception {
System.out.println("AddNameAction
class!!!!!!!!!!!!!");
ActionRequest
aReq = (ActionRequest) req.getAttribute("javax.portlet.request");
ActionResponse
aRes = (ActionResponse) req.getAttribute("javax.portlet.response");
PortletPreferences
prefs = aReq.getPreferences();
String
userName = ((AddNameForm)form).getUserName();
prefs.setValue("name",userName);
prefs.store();
aRes.setPortletMode(
PortletMode.VIEW
);
return
mapping.findForward("/struts-exercise/addname/success");
}
}
Step
8)create
a portlet class called AddNameForm
under src/com/test/form
package
com.test.form;
import
javax.servlet.http.HttpServletRequest;
import
org.apache.struts.action.ActionErrors;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.action.ActionMessage;
public
class AddNameForm extends ActionForm {
private
static final long serialVersionUID = 1L;
private
String userName;
public
String getUserName() {
return
userName;
}
public
void setUserName(String userName) {
this.userName
= userName;
}
public
void reset(ActionMapping mapping, HttpServletRequest req) {
userName
= null;
}
public
ActionErrors validate(
ActionMapping
mapping, HttpServletRequest req) {
ActionErrors
errors = new ActionErrors();
if
((userName == null) || (userName.length() < 1)) {
errors.add(
"userName",
new ActionMessage("error.lastName.required"));
}
System.out.println("AddNameForm
class!!!!!!!!!!!!!");
return
errors;
}
public
String toString() {
return
userName;
}
}
Step
9)Create a
view.jsp file under docroot/html/strutsdemo folder ,put this code
into the view.jsp file
view.jsp
<%@
taglib
uri="http://java.sun.com/portlet"
prefix="portlet"
%>
<%@
taglib
uri="http://portals.apache.org/bridges/struts/tags-portlet-html"
prefix="html"
%>
<%@
taglib
uri="http://portals.apache.org/bridges/struts/tags-portlet-html"
prefix="html"
%>
<%@page
import="javax.portlet.PortletPreferences"%>
<portlet:defineObjects
/>
<p>This
is the Struts portlet.</p>
<%
PortletPreferences
prefs = renderRequest.getPreferences();
String
username = (String)prefs.getValue("name",
"");
%>
<p>Hello
<%=username%>!</p>
<html:link
page="/portlet_action/struts-exercise/edit">Back
to Edit Mode</html:link>
Step
10)create
edit.jsp file under docroot/html/strutsdemo folder
edit.jsp
<%@
taglib
uri="http://java.sun.com/portlet"
prefix="portlet"
%>
<%@
taglib
uri="http://portals.apache.org/bridges/struts/tags-portlet-html"
prefix="html"
%>
<%@
taglib
uri="http://struts.apache.org/tags-bean"
prefix="bean"
%>
<%@
taglib
uri="http://struts.apache.org/tags-logic"
prefix="logic"
%>
<%@
taglib
uri="http://struts.apache.org/tags-nested"
prefix="nested"
%>
<%@
taglib
uri="http://struts.apache.org/tags-tiles"
prefix="tiles"
%>
<portlet:defineObjects
/>
<p>The
Portlet
is in Edit Mode.</p>
<html:form
action="/struts-exercise/addname"
method="post">
<table>
<tr>
<td>Name:</td>
<td><html:text
name="addNameForm"
property="userName"/></td>
</tr>
</table>
<html:submit>Add
Name</html:submit>
</html:form>
<html:link
page="/portlet_action/struts-exercise/view">Back
to View Mode</html:link>
Step
11)deploy and run
your portlet
Enter Name and click
on Add Name