Blogs
<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.
|
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
|
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 addEmplyee(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 addEmplyee(ActionRequest actionRequest,
ActionResponse actionResponse)
throws IOException, PortletException {
String employeeName=ParamUtil.getString(actionRequest,"employeeName");
actionResponse.setRenderParameter("employeeName",employeeName
);
}
}
|
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 addEmplyee(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/>
|
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 addEmplyee(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 addEmplyee(ActionRequest actionRequest,
ActionResponse actionResponse)
throws IOException, PortletException {
String employeeName=actionRequest.getParameter("employeeName");
String employeeAddress=actionRequest.getParameter("employeeAddress");
String requestParamValue= actionRequest.getParameter("
requestParam");
}
}
|
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 addEmplyee(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 addEmplyee(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"/>
|
- Set Arrtibute and Get Attribute on portlet request object
- Set Render Parameter method on portlet response object
- Copy Render Parameters method
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);
}
|
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
/>
<%}%>
|
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/>
|
<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/>
|
- AUI Tag Library
- Liferay Portlet Tag Library
- Liferay Theme Tag Library
- Liferay UI Tag Library
- Liferay Util Tag Library
<%@ 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" %>
|
<%@ 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 />
|
<?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>
|
<?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>
|
<?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>
|
<?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>
|
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");
}
}
|
<%@ 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/>
|
<%@ 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>
|
<%@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/>
<%}%>
|