Introduction:
Liferay have come up with their portlet frame work called Liferay MVC. Liferay MVC is portlet development framework and we can develop JSR 168&286 standards portlets and deploy into Liferay Portal.
Liferay MVC is portlet framework specially designed for liferay and we can develop portlet and deploy into liferay portal.
This frame work has many features and it uses Liferay APIs to rapid portlet development and easy to use. It is very light weight frame work and it come up with liferay Plugins SDK.
Liferay MVC portlet is default liferay portlet development frame work and it is inbuilt in Liferay Plugins SDK. We need not to do any additional configuration or need not to add any additional jar files when we develop portlet using Liferay MVC. It’s also follows all JSR portlet standards and it inherited the Generic Portlet features.
Liferay IDE has support to create Liferay MVC portlet and deploy into servers.
Generally when we use Generic Portlet developer need to handle more code but when we use Liferay MVC it will cut down development effort when we think in prospective of code, it have very good mechanism to handle portlet lifecycle.
MVC portlet hierarchy
When we moving to up to down it meaning we are getting many features in the child class i.e. MVC Portlet have many features.
We already have experience with Generic Portlet and we already well aware of Portlet Life cycle and its JSR 168&286 features.
Go through following articles for more understand about portlet development and life cycle.
In the generic portlet development portlet class need to override doView(--) ,processAction(--) for every use of portlet action or render.
But when we use Liferay MVC Portlet we need not override any of the above methods because those methods already implemented and they have designed good mechanism to render views and perform portlet action without overdoing any of the method.
Here we will handle page navigation or render the views by simply using some request parameters in URL and similarly we will specify portlet class action method name as request parameter in portlet Action URL to perform action.
In the portlet Action Phase we will simply specify the portlet class method name in URL parameter so that it will perform action. So here we need not override processAction(--) method
Examples
Navigate from one jsp page to another jsp page
<portlet:renderURL var="linkToJspPageOne"> <portlet:param name="mvcPath" value="/html/jsps/page1.jsp"/> </portlet:renderURL> OR <portlet:renderURL var="linkToJspPageTwo"> <portlet:param name="jspPage" value="/html/jsps/page2.jsp"/> </portlet:renderURL> Note: We need to use jspPage or mvcPath URL parameter and need specify the jsp page path as value then it will navigate to jsp page or render the specified view. In the portlet class we need not specify any thing means we need not override doView(--) method. |
Perform Portlet Action Phase
Action URL <portlet:actionURL var="portletActionURL" name="addEmployee"> </portlet:actionURL> OR <portlet:actionURL var="portletActionURL"> <portlet:param name="javax.portlet.action" value="addEmployee"/> </portlet:actionURL> OR <portlet:actionURL var="portletActionURL"> <portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="addEmployee"/> </portlet:actionURL> When we access above URL through hyper link or as form action then it will execute portlet call action method and the method name is addEmployee. Portlet Class Action Method public class EmplyeePortletAction extends MVCPortlet { public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { //business logic here } } Note: We simple create Portlet Action URL and we need to specify the portlet class action method name as URL name attribute URL name attribute is same as Portlet Class action method name so that it will execute the method and perform action phase. We need not to override processAction(--) method. We can specify the portlet class action method name in URL name attribute or we can specify as URL request parameter i.e. javax.portlet.action |
When we perform multiple actions then we simple create multiple Action URLs and use URL name attribute to specify the action method name so that it will perform action phase.
It is very convenient to developer to avoid many if else blocks in processAction(--) method and we need not override process action method.
Access Action URL request Parameters in Render Phase or JSP page.
We already know when we specify URL parameters in Action URL those are not available in jsp page or render phase.
We have to use Set Render Parameters method in action phase or we have to use Copy Render Parameters method in action method or action phase.
Scenario
We have some request parameter in Portlet Action URL and after perform action phase through portlet class action method then we need to access Action URL parameters in Render Phase or JSP page.
Case:1 <portlet:renderURL var="portletRenerURL"> <portlet:param name="employeeName" value="Meera Prince"/> </portlet:renderURL> In JSP Page Emplyee Name: <%=renderRequest.getParameter("employeeName")%><br/> In the Case: 1 we are using Portlet Render URL that is why we can access URL request parameters in Render Phase or JSP page. Case:2 <portlet:actionURL var="portletActionURL" name="addEmployee"> <portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="addEmployee"/> <portlet:param name="employeeName" value="Meera Prince"/> </portlet:actionURL> Emplyee Name: <%=renderRequest.getParameter("employeeName")%><br/> In the Case: 2 we are using Portlet Action URL and we are performing action phase, after perform action phase the parameter we passed in request URL not available in Render Phase or JSP page. Solution:1 We have to use copy request parameter method in portlet class action method so that all URL parameters carried to render phase and it is available in JSP page. <portlet:actionURL var="portletActionURL" name="addEmployee"> <portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="addEmployee"/> <portlet:param name="employeeName" value="Meera Prince"/> </portlet:actionURL> Portlet Class Action Method public class EmplyeePortletAction extends MVCPortlet { public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { PortalUtil.copyRequestParameters(actionRequest, actionResponse); } } Solution:2 We can use set render parameter method in portlet class action method to set URL parameters and those URL parameters are carried to render phase or jsp page. <portlet:actionURL var="portletActionURL" name="addEmployee"> <portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="addEmployee"/> <portlet:param name="employeeName" value="Meera Prince"/> </portlet:actionURL> Portlet Class Action Method public class EmplyeePortletAction extends MVCPortlet { public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String employeeName=ParamUtil.getString(actionRequest,"employeeName"); actionResponse.setRenderParameter("employeeName",employeeName); } } |
JSP Navigation or Render different Views from Action Phase
In general portlet development after performing action phase we need to navigate result page.
To Perform Action URL we already know URL parameters are not available in render phase that is what we cannot set jspPage/mvcPath in Action URL parameter.
We need to set this parameter value in Portlet class action method so that after perform action it will render specified jsp page that we mention as mvcPath
view.jsp <portlet:actionURL var="portletActionURL" name="addEmployee"> <portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="addEmployee"/> <portlet:param name="employeeName" value="Meera Prince"/> </portlet:actionURL> Portlet Class Action Method public class EmplyeePortletAction extends MVCPortlet { public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String employeeName=ParamUtil.getString(actionRequest,"employeeName"); actionResponse.setRenderParameter("employeeName",employeeName); actionResponse.setRenderParameter("mvcPath","/html/jsps/displayEmployee.jsp"); } } displayEmployee.jsp Emplyee Name: <%=renderRequest.getParameter("employeeName")%><br/> |
Access Form input data or URL request parameters in Portlet Class Action Method
In portlet development we will send data from client to server using form input elements or as URL request parameters.
In the server side or portlet class action method we will use get Parameter or we have liferay Util class called Param Utilto access the URL or form input data.
When we use Param Util we have flexibility to access desired data type we need not to do explicit type casting.
If we use get Parameter then we needs to do type casting the value to get desired data type because request parameters always is string.
Example methods in Param Util class
String employeeAddress=ParamUtil.getString(actionRequest,"employeeAddress"); int age=ParamUtil.getInteger(actionRequest,"age"); float price =ParamUtil.getFloat(actionRequest,"price"); boolean valid=ParamUtil.getBoolean(actionRequest,"valid"); |
Jsp page <portlet:actionURL var="addEmployeeActionURL" name="addEmployee"> <portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="addEmployee"/> <portlet:param name="requestParam" value=" requestParamValue"/> </portlet:actionURL> <form action="<%=addEmployeeActionURL%>" name="emplyeeForm" method="POST"> Employee Name<br/> <input type="text" name="<portlet:namespace/>employeeName" id="<portlet:namespace/>employeeName"/><br/> Employee Address<br/> <input type="text" name="<portlet:namespace/>employeeAddress" id="<portlet:namespace/>employeeName"/><br/> <input type="submit" name="addEmployee" id="addEmployee" value="Add Employee"/> </form> Portlet Class Action Method public class EmplyeePortletAction extends MVCPortlet { public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String employeeName=ParamUtil.getString(actionRequest,"employeeName"); String employeeAddress=ParamUtil.getString(actionRequest,"employeeAddress"); String requestParamValue=ParamUtil.getString(actionRequest,"requestParam"); } } OR public class EmplyeePortletAction extends MVCPortlet { public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String employeeName=actionRequest.getParameter("employeeName"); String employeeAddress=actionRequest.getParameter("employeeAddress"); String requestParamValue= actionRequest.getParameter("requestParam"); } } |
Required Name Space Parameter Behavior in Liferay
Liferay 6.2 we have to append portlet Name space for every name of input element i.e. form input elements or request parameters names otherwise portlet action class ignore the parameters which does not have portlet name space to names.
Scenario
Jsp page In the following form we are not appending portlet name space to form input element names. <portlet:actionURL var="addEmployeeActionURL" name="addEmployee"> <portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="addEmployee"/> </portlet:actionURL> <form action="<%=addEmployeeActionURL%>" name="emplyeeForm" method="POST"> Employee Name<br/> <input type="text" name="employeeName" id="employeeName"/><br/> Employee Address<br/> <input type="text" name="employeeAddress" id="employeeName"/><br/> <input type="submit" name="addEmployee" id="addEmployee" value="Add Employee"/> </form> Portlet Class Action Method public class EmplyeePortletAction extends MVCPortlet { public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String employeeName=ParamUtil.getString(actionRequest,"employeeName"); String employeeAddress=ParamUtil.getString(actionRequest,"employeeAddress"); } } In above case employeeName and employeeAddress form input data not accessible in portlet class action .The form elements name are not appended with portlet name space such scenarios portlet class ignore those request parameters or form inputs Solution:1 Need to append <portlet:namespace/> tag to every input element name. Jsp page <portlet:actionURL var="addEmployeeActionURL" name="addEmployee"> <portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="addEmployee"/> <portlet:param name="requestParam" value=" requestParamValue"/> </portlet:actionURL> <form action="<%=addEmployeeActionURL%>" name="emplyeeForm" method="POST"> Employee Name<br/> <input type="text" name="<portlet:namespace/>employeeName" id="<portlet:namespace/>employeeName"/><br/> Employee Address<br/> <input type="text" name="<portlet:namespace/>employeeAddress" id="<portlet:namespace/>employeeName"/><br/> <input type="submit" name="addEmployee" id="addEmployee" value="Add Employee"/> </form> Portlet Class Action Method public class EmplyeePortletAction extends MVCPortlet { public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String employeeName=ParamUtil.getString(actionRequest,"employeeName"); String employeeAddress=ParamUtil.getString(actionRequest,"employeeAddress"); String requestParamValue=ParamUtil.getString(actionRequest,"requestParam"); } } Solution:2 We can make it false to following tag value in liferay-portlet.xml file <requires-namespaced-parameters>false</requires-namespaced-parameters> Solution:3 We can use alloy tag library form tags. When we use AUI tags it will append portlet name space to each input element name. Jsp page <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %> <aui:input type="text" name="employeeAddress" id="employeeName"/><br/> <aui:input type="submit" name="addEmployee" id="addEmployee" value="Add Employee"/ |
<input type="text" name="<portlet:namespace/>employeeAddress" id="<portlet:namespace/>employeeName"/> Is same As <aui:input type="text" name="employeeAddress" id="employeeName"/> |
Send the Data from Portlet Class Action method to JSP pages.
Generally we need requirement that need to send some data from portlet class action method to jsp pages.
After perform some business logic we need to send some data from portlet class action method to jsp pages
We have three ways to send data to JSP Pages.
- Set Arrtibute and Get Attribute on portlet request object
- Set Render Parameter method on portlet response object
- Copy Render Parameters method
Set Arrtibuteand Get Attribute on portlet request object
We will use set Attribute to set some object or value in portlet class Action method and we will use get Attribute method in jsp page to get value in jsp.
Example:
Portlet Class Action Method public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String employeeName=ParamUtil.getString(actionRequest,"employeeName"); String employeeAddress=ParamUtil.getString(actionRequest,"employeeAddress"); Map<String,String> employeeMap=new HashMap<String,String>(); employeeMap.put("employeeName",employeeName); employeeMap.put("employeeAddress",employeeAddress); actionRequest.setAttribute("employeeMap", employeeMap); } |
In the above portlet class action method we are passing employee data from map object, we will set map object in request.
Get the map object in JSP page
Jsp page <%@page import="java.util.Map"%> <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <portlet:defineObjects /> <h1>Display Employee Derails</h1> <% Map<String,String> employeeMap=(Map<String,String>)renderRequest.getAttribute("employeeMap"); if(employeeMap!=null){ %> Emplyee Name: <%=employeeMap.get("employeeName")%> <br/> Emplyee Address: <%=employeeMap.get("employeeAddress")%><b/> <%}%> |
Set Render parameter method on Portlet Response Object
Set Render Parameter method will set the parameter and its values in portlet query string. We can only set the string values using this method.
Example:
In the portlet class action method
Portlet Class Action Method public void addEmployee(ActionRequest request,ActionResponse response)throws PortletException,java.io.IOException{ String emplyeeName=request.getParameter("employeeName"); String employeeAddress=request.getParameter("employeeAddress"); response.setRenderParameter("employeeName",emplyeeName); response.setRenderParameter("employeeAddress",employeeAddress); } Get the Parameter Values in JSP Page that already set in Process Action Emplyee Name: <%=renderRequest.getParameter("employeeName")%> <br/> Emplyee Address: <%=renderRequest.getParameter("employeeAddress")%><br/> |
Copy Render Parameters method
<portlet:actionURL var="addEmployeeActionURL" name="addEmployee"> <portlet:param name="mvcPath" value="/html/helloworld/displayEmployee.jsp"/> <portlet:param name="emplyeeName" value="Meera Prince"/> </portlet:actionURL> Portlet Class Action Method public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { PortalUtil.copyRequestParameters(actionRequest, actionResponse); } Now we can access request parameters in displayEmployee jsp page Jsp page Emplyee Name: <%=renderRequest.getParameter("employeeName")%> <br/> |
Liferay Tag Libraries and its Usage in JSP page.
Liferay have rich built in tag libraries and they have many components in set of library.
The following are useful tag libraries in JSP page
- AUI Tag Library
- Liferay Portlet Tag Library
- Liferay Theme Tag Library
- Liferay UI Tag Library
- Liferay Util Tag Library
To Use all tag Libraries in JSP page
Add following Tag lib URI in any of the portlet application jsp page so that we can use many tags in JSP page. For details information about tag please have look into tag lib tld file.
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %> <%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %> <%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %> <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %> <%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %> |
Access Liferay Implicit Objects
Liferay have provided some of the important implicit variables to use in JSP pages.To access those implicit variables we need add following tags in jsp page so that we can access renderRequest, renderResponse, themeDisply, user and Layout.
Add following tags in JSP page
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %> <%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %> <%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %> <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %> <%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %> <liferay-theme:defineObjects /> <portlet:defineObjects /> |
Liferay MVC Employee Portlet Example Code.
Download MVC Portlet Examples
MVC Portlet Directory Structure
Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>LiferayMVCEmployee-portlet</display-name> <jsp-config> <taglib> <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri> <taglib-location> /WEB-INF/tld/liferay-portlet.tld </taglib-location> </taglib> <taglib> <taglib-uri>http://liferay.com/tld/aui</taglib-uri> <taglib-location>/WEB-INF/tld/aui.tld</taglib-location> </taglib> </jsp-config> </web-app> |
Portlet.xml
<?xml version="1.0"?> <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0"> <portlet> <portlet-name>employeeliferaymvc</portlet-name> <display-name>Employee MVC Portlet</display-name> <portlet-class> com.meera.liferaymvc.EmployeePortletAction </portlet-class> <init-param> <name>view-template</name> <value>/html/jsps/view.jsp</value> </init-param> <expiration-cache>0</expiration-cache> <supports> <mime-type>text/html</mime-type> <portlet-mode>view</portlet-mode> </supports> <portlet-info> <title>Employee MVC Portlet</title> <short-title>Employee Portlet Action</short-title> <keywords></keywords> </portlet-info> <security-role-ref> <role-name>administrator</role-name> </security-role-ref> <security-role-ref> <role-name>guest</role-name> </security-role-ref> <security-role-ref> <role-name>power-user</role-name> </security-role-ref> <security-role-ref> <role-name>user</role-name> </security-role-ref> </portlet> </portlet-app> |
Liferay-portlet.xml
<?xml version="1.0"?> <!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd"> <liferay-portlet-app> <portlet> <portlet-name>employeeliferaymvc</portlet-name> <icon>/icon.png</icon> <instanceable>false</instanceable> <header-portlet-css>/css/main.css</header-portlet-css> <footer-portlet-javascript> /js/main.js </footer-portlet-javascript> <css-class-wrapper> employeeliferaymvc-portlet </css-class-wrapper> </portlet> <role-mapper> <role-name>administrator</role-name> <role-link>Administrator</role-link> </role-mapper> <role-mapper> <role-name>guest</role-name> <role-link>Guest</role-link> </role-mapper> <role-mapper> <role-name>power-user</role-name> <role-link>Power User</role-link> </role-mapper> <role-mapper> <role-name>user</role-name> <role-link>User</role-link> </role-mapper> </liferay-portlet-app> |
Liferay-display.xml
<?xml version="1.0"?> <!DOCTYPE display PUBLIC "-//Liferay//DTD Display 6.2.0//EN" "http://www.liferay.com/dtd/liferay-display_6_2_0.dtd"> <display> <category name="category.sample"> <portlet id="employeeliferaymvc"></portlet> </category> </display> |
Portlet Action Class
package com.meera.liferaymvc; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.util.bridges.mvc.MVCPortlet; public class EmployeePortletAction extends MVCPortlet { public void addEmployee(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String employeeName = ParamUtil.getString(actionRequest, "employeeName"); String employeeAddress = ParamUtil.getString(actionRequest, "employeeAddress"); Map<String, String> employeeMap = new HashMap<String, String>(); employeeMap.put("employeeName", employeeName); employeeMap.put("employeeAddress", employeeAddress); actionRequest.setAttribute("employeeMap", employeeMap); actionResponse.setRenderParameter("mvcPath","/html/jsps/displayEmployee.jsp"); } } |
Default portlet view page (view.jsp)
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <portlet:defineObjects /> <portlet:renderURL var="addEmployee" windowState="normal"> <portlet:param name="mvcPath" value="/html/jsps/addEmployee.jsp"/> </portlet:renderURL> <h1>Welcome to Liferay MVC Employee Portlet</h1> <a href="<%=addEmployee.toString()%>">Add Employee</a><br/> |
Add Employee JSP page (/html/jsps/addEmployee.jsp)
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <portlet:defineObjects /> <portlet:actionURL var="addEmployeeActionURL" windowState="normal" name="addEmployee"> </portlet:actionURL> <h1> Add Employee</h1> <form action="<%=addEmployeeActionURL%>" name="emplyeeForm" method="POST"> Employee Name<br/> <input type="text" name="<portlet:namespace/>employeeName" id="<portlet:namespace/>employeeName"/><br/> Employee Address<br/> <input type="text" name="<portlet:namespace/>employeeAddress" id="<portlet:namespace/>employeeName"/><br/> <input type="submit" name="addEmployee" id="addEmployee" value="Add Employee"/> </form> |
Display Employee JSP page (/html/jsps/displayEmployee.jsp)
<%@page import="java.util.Map"%> <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <portlet:defineObjects /> <h1>Display Employee Details</h1> <% Map<String,String> employeeMap=(Map<String,String>)renderRequest.getAttribute("employeeMap"); if(employeeMap!=null){ %> Emplyee Name: <%=employeeMap.get("employeeName")%> <br/> Emplyee Address: <%=employeeMap.get("employeeAddress")%><b/> <%}%> |
Portlet out Put Screens
Portlet Default view page
Add Employee Screen
Display Employee Screen
Related Articles
Author
0 comments:
Post a Comment