Multiple Action Classes with MVCPortlet
Liferay MVCPortlet has ability to move individual actions into separate classes.
Following steps are required
Step 1 : Add the following initialization parameter to your portlet.xml file:
<init-param>
<name>action.package.prefix</name>
<value>com.tana.portlet.action</value>
</init-param>
(CustomActionCommand class is located in this package com.tana.portlet.action)
Step 2 : Implementing an ActionCommand interface
Your custom Action class should follow the two approach
1) Custom action class must be suffix with -ActionCommand
2) Custom action class must be implemented with the ActionCommand
interface
Portlet Class - CustomPortlet.java
package com.tana.portlet.action;
public class CustomPortlet extends MVCPortlet {
@Override
public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
// TODO Auto-generated method stub
System.out.println("CustomPortlet porlet is called!");
PortletRequestDispatcher dispatcher = null;
String param = (String) renderRequest.getAttribute("param");
if(param != null && param.equals("secondPage")) {
renderResponse.setContentType("text/html");
System.out.println("********************");
dispatcher = getPortletContext().getRequestDispatcher("/html/customportlet/second.jsp");
dispatcher.include(renderRequest, renderResponse);
} else {
super.doView(renderRequest, renderResponse);
}
}
}
Custom action class - CustomActionCommand.java
processCommand() method of a CustomActionCommand class is called to perform Action.
public class Custom
ActionCommand implements
ActionCommand {
@Override
public boolean
processCommand(PortletRequest request, PortletResponse response)
throws PortletException {
// TODO Auto-generated method stub
System.out.println("CustomActionCommand class is called!");
request.setAttribute("param","secondPage");
return true;
}
}
Step 3 : JSP page
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:actionURL var="actionCommandUrl">
<portlet:param name="<%=javax.portlet.ActionRequest.ACTION_NAME %>" value="Custom"></portlet:param>
</portlet:actionURL>
This is the <b>CustomPortlet</b> portlet in View mode.
<br>
<br>
<form action="<%=actionCommandUrl%>" method="post" name="myform">
<input type="submit" value="Call Custom-Action Class">
</form>
- Create another jsp page under /html/customportlet/second.jsp this directory.
Multiple action can be perform into a single request. This would be done by setting javax.portlet.ActionRequest.ACTION_NAME parameter as param name and list of action as value which is separated by comma.
e.g <portlet:actionURL var="actionCommandUrl">
<portlet:param name="<%=javax.portlet.ActionRequest.ACTION_NAME %>" value="Action1,Action2">
</portlet:param>