Liferay have provided flexibility to develop Portlet with
different web framework such as JSF, Spring, Struts and Vaadin.One
of the good choice is spring based Portlets. Liferay supported spring based Portlet
and we can deploy into Liferay portal server.
Liferay with spring make us better way of using spring
capabilities and Liferay capabilities. Spring Portlet development, we can use
all Liferay APIs so that we can develop Portlets very easy and if the developers
have best understanding about spring then its good choice to develop spring Portlets in Liferay.
Liferay did not provide any tools or any features to
develop spring based Portlets but it will support spring based Portlets and that
can be run in Liferay portal server.
Generally when we want run other web framework based
application which run in Portlet based containers we need Portlet bridges and
these Portlet bridges is responsible for convert normal web application  lifecycle to Portlet lifecycle, so that it can
run in Portlet based containers or we can say it can run in portal servers .
Liferay also have such capable that accommodate other framework based
applications run in Liferay portal.
Portlet bridges will be provided by framework vendors so
that we can use those bridges to develop specific frame work based Portlets
same way spring also proving Spring MVC Portlet bridge with this
we can develop Portlets and that can be run in any portal servers which follow
the JSR 168&286 specifications.
To develop spring Portlet and run into Liferay portal
servers we need Spring MVC Portlet bridge that
already provided by spring people and the implementation is available in spring-webmvc-portlet.jar
(spring-web-portlet.jar) 
Liferay IDE does not have support to develop spring Portlet
but we can use Liferay MVC Portlet then we convert into spring Portlet.
Download Portlet 
Before Strat
Development please go through following Topics
Liferay Savvy already explained the development of spring
Portlet and its example Please go through the following topic
Now we will go through the full details of spring Portlet
development. Before understand this tutorial we need a basic knowledge about
development of spring web based application so that we can have better understand
when we go through following topics.
However we already gone through the spring Portlet
development in the above link even though we will reminder the steps to develop
the spring Portlet
Liferay Spring
Portlet Development Steps
- Create Liferay MVC Portlet with Liferay IDE
- Configure Spring related configuration in Portlet web.xml
- Create Portlet Application Context xml file and do necessary configuration
- Configure Spring Dispatcher Portlet
- Create Portlet Configuration xml file and configure necessary configuration
- Specify all Dependent Jar files in liferay-plugin-package.properties
Create Liferay MVC Portlet with Liferay IDE
Crate Liferay MVC Portlet using Liferay IDE is very
straight forward way and you go thorough following topics.
Configure Spring related configuration in Portlet “web.xml”
Same like web application spring configurations we need to
configure “Context Config Location” init param, View Renderer Servlet and if required Context Loader Listener need to be
configured in Portlet web.xml file.
Context configuration location this is path of xml file
where your application context xml file is available and this is entire Portlet
plugin application context.
The following is required configuration web.xml file
| 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>/WEB-INF/context/portletApplicationContext.xml</param-value> 
</context-param> 
<!-- <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> --> 
<servlet> 
<servlet-name>ViewRendererServlet</servlet-name> 
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>ViewRendererServlet</servlet-name> 
<url-pattern>/WEB-INF/servlet/view</url-pattern> 
</servlet-mapping> 
</web-app> | 
Create Portlet Application Context xml file and do
necessary configuration
Portlet application context xml file where we will define
all spring beans, data source and session factory information. All this beans
will be available once Portlet application is ready. Now we need to create Portlet
application xml configuration file in above specified path (WEB-INF/context/portletApplicationContext.xml)
The following is configuration in
portletApplicationContext.xml
| 
<?xml version='1.0' encoding='UTF-8'?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
<context:annotation-config /> 
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
<bean class="org.springframework.web.portlet.handler.ParameterMappingInterceptor" /> 
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
<property name="basenames"> 
<list> 
<value>classpath:content/Language</value> 
</list> 
</property> 
<property name="defaultEncoding" value="UTF-8" /> 
</bean> 
</beans> | 
Note:
<context:annotation-config /> is tag and it will
provide Inversion Of Control(IOC) through the annotation rather than manually configure all the spring beans in the xml files.
Configure Spring Dispatcher Portlet
In portlet.xml file we need to specify
the spring dispatcher Portlet as Portlet class tag we need to specify the Portlet
specific Context Config Location xml file where we will define view
resolvers means path pattern for JSP pages and its location.
The following is
configuration in 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>LiferaySpring</portlet-name> 
<display-name>LiferaySpring</display-name> 
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class> 
<init-param> 
<name>contextConfigLocation</name> 
<value>/WEB-INF/context/LiferaySpring-portlet.xml</value> 
</init-param> 
<expiration-cache>0</expiration-cache> 
<supports> 
<mime-type>text/html</mime-type> 
</supports> 
<resource-bundle>content.Language</resource-bundle> 
<portlet-info> 
<title>LiferaySpring</title> 
<short-title>LiferaySpring</short-title> 
<keywords>LiferaySpring</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> | 
When the xml configuration file name like “portletName-portlet.xml”
then we no need to configure the “contextConfigLocation”  init param in the portlet.xml file. If file
name not follow “portletName-portlet.xml” pattern then we need to explicitly
configured the contextConfigLocation  init param.
Create Portlet Configuration xml file and configure necessary
configuration
Now we need to create Portlet specific context
configuration in the specified location (we already specified in portlet.xml
file path as WEB-INF/context/LiferaySpring-portlet.xml) and need to
configure the view resolver information
The following is configuration in Portlet specific context
xml file(WEB-INF/context/LiferaySpring-portlet.xml) 
| 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.0.xsd 
"> 
<context:annotation-config /> 
<context:component-scan base-package="com.liferaysavvy.liferay.spring" /> 
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="viewClass" 
value="org.springframework.web.servlet.view.JstlView" /> 
<property name="prefix" value="/WEB-INF/jsp/" /> 
<property name="suffix" value=".jsp" /> 
</bean> 
</beans> | 
<context:component-scan/>  this tag will load all classes or beans
specified  java package in context. So
make sure all beans or classes should be loaded in the Portlet context
otherwise it won’t be available in the run time.
Specify all Dependent Jar files in “liferay-plugin-package.properties”
We are developing spring based Portlet so it need all
spring frame work implementation jar files and important one is spring-webmvc-portlet.jar.
All required jar files will be
specified in the liferay-plugin-package.properties file so that it will
copy the all specified jar files to Portlet WEB-INF/lib directory, once you deploy the Portlet and these
jars will be available in (ROOT/WEB-INF/lib).
If any Portlet required jar which
in not available in Liferay portal then we need to manually place jar file in Portlet
lib directory (WEB-INF/lib)
The following is configuration in liferay-plugin-package.properties
| 
name=LiferaySpring 
module-group-id=liferay 
module-incremental-version=1 
tags= 
short-description= 
long-description= 
change-log= 
page-url=http://www.liferay.com 
author=Liferay, Inc. 
licenses=LGPL 
liferay-versions=6.2.0+ 
portal-dependency-jars=\ 
    spring-aop.jar,\ 
    spring-asm.jar,\ 
    spring-aspects.jar,\ 
    spring-beans.jar,\ 
    spring-context-support.jar,\ 
    spring-context.jar,\ 
    spring-core.jar,\ 
    spring-expression.jar,\ 
    spring-jdbc.jar,\ 
    spring-jms.jar,\ 
    spring-orm.jar,\ 
    spring-oxm.jar,\ 
    spring-transaction.jar,\ 
    spring-web-portlet.jar,\ 
    spring-web-servlet.jar,\ 
    spring-web-struts.jar,\ 
    spring-web.jar,\ 
    commons-beanutils.jar,\ 
    commons-codec.jar,\ 
    commons-collections.jar,\ 
    commons-compress.jar,\ 
    commons-configuration.jar,\ 
    commons-dbcp.jar,\ 
    commons-digester.jar,\ 
    commons-discovery.jar,\ 
    commons-email.jar,\ 
    commons-fileupload.jar,\ 
    commons-httpclient.jar,\ 
    commons-io.jar,\ 
    commons-lang.jar,\ 
    commons-logging.jar,\ 
    commons-math.jar,\ 
    commons-pool.jar,\ 
    commons-validator.jar,\ 
    aopalliance.jar,\ 
    jstl-api.jar,\ 
    jstl-impl.jar 
portal-dependency-tlds=\ 
    c.tld | 
 To specify the Portlet controller or action class we
should use @Controller annotation in
java class so that it will be handle all Portlet related actions and this class
should be available in the package that we specified in Portlet specific xml
context file and the tag is <context:component-scan/>
Generally in spring based web application
entire work flow handled by Http Servlet Request and Http Servlet Response. When
we come to spring based Portlet all lifecycle will be managed by three phases
and it will be managed by different requests and responses that we already discussed.
The following are different phases of Portlet
lifecycle
Action Phase (Action Request,
Action Response)
Render Phase (Render Request,
Render Response)
Serve Resource Phase
(Resource Request, Resource Response)
Portlet also have different modes
like view, edit and help if
you want use Different Portlet spring controller to handle these modes we can
define multiple controller each controller handle one mode.
The following is
Example Spring Portlet Controller
| 
@Controller 
@RequestMapping(value = "VIEW") 
//@RequestMapping(value =
  "EDIT") 
//@RequestMapping(value =
  "HELP") 
public class
  LiferaySpringController{ 
@RenderMapping(params
  ="action=hompage") 
public String
  renderHomePage(RenderRequest request, RenderResponse response) throws
  SystemException { 
//Portlet Render Phase 
} 
@ResourceMapping("validateUserbyEmail") 
public void
  validateUserbyEmail(ResourceRequest request,ResourceResponse response) throws
  SystemException, 
SystemException { 
//Portlet Serve Resource Phase 
} 
@ActionMapping(params =
  "action=findUser") 
public void
  findUser(ActionRequest actionRequest,ActionResponse actionResponse)throws
  SystemException { 
//Portlet Action Phase 
} 
} | 
Portlet Spring controller methods
execution will be decided by request
parameter values and its names.
Portlet Controller Invocation
| 
@RequestMapping(value
  = "VIEW") it specify that in the requested URL one of
  the value should be  “VIEW” then it controller will be
  invoked. 
Example URL 
http://localhost:8080/web/guest/test? 
p_p_id=LiferaySpring_WAR_LiferaySpringportlet& 
p_p_lifecycle=0& 
p_p_state=normal& 
p_p_mode=view& 
p_p_col_id=column-2& 
p_p_col_count=1& 
_LiferaySpring_WAR_LiferaySpringportlet_action=homepage. 
Portlet
  Controller VIEW Mode 
@Controller 
@RequestMapping(value =
  "VIEW") 
public class
  LiferaySpringController{ 
} 
Portlet Controller
  EDIT Mode 
@Controller 
@RequestMapping(value =
  "EDIT") 
public class
  LiferaySpringControllerEditMode{ 
} 
Portlet
  Controller HELP Mode 
@Controller 
@RequestMapping(value =
  "HELP") 
public class
  LiferaySpringControllerHelpMode{ 
} | 
Render Phase Method Invocation
| 
@RenderMapping(params
  ="action=view") it specify that Requested
  Portlet Render URL  will have param
  name “action” and it value “homepage” 
  then specified method below this annotation will be executed and
  method name can be anything. 
Portlet Render URL
  Creation 
<portlet:renderURL
  var="renderHomeURL"> 
<portlet:param name="action"
  value="homepage"/> 
</portlet:renderURL> 
Example URL 
http://localhost:8080/web/guest/test? 
p_p_id=LiferaySpring_WAR_LiferaySpringportlet& 
p_p_lifecycle=0& 
p_p_state=normal& 
p_p_mode=view& 
p_p_col_id=column-2& 
p_p_col_count=1& 
_LiferaySpring_WAR_LiferaySpringportlet_action=homepage 
Method in
  Controller 
@RenderMapping(params ="action=homepage") 
public String
  renderHomePage(RenderRequest request, RenderResponse response) throws
  SystemException { 
//Portlet Render Phase 
} | 
Action Phase Method Invocation
| 
@ActionMapping(params
  = "action=findUser")  it specify that Portlet action URL have
  request param name action and its value findUser then annotation below
  method will be executed and method name can be anything. 
Portlet Action URL
  Creation 
<portlet:actionURL
  var="findUserActionURL" windowState="normal"> 
<portlet:param name="action"
  value="findUser"/> 
</portlet:actionURL> 
Example URL 
http://localhost:8080/web/guest/test? 
p_p_id=LiferaySpring_WAR_LiferaySpringportlet& 
p_p_lifecycle=1& 
p_p_state=normal& 
p_p_mode=view& 
p_p_col_id=column-2& 
p_p_col_count=1& 
_LiferaySpring_WAR_LiferaySpringportlet_action=findUser 
Method in
  Controller 
@ActionMapping(params = "action=findUser") 
public void
  findUser(ActionRequest actionRequest,ActionResponse actionResponse)throws
  SystemException { 
//Portlet Action Phase 
} | 
Server Resource Phase Method Invocation
| 
@ResourceMapping("validateUserbyEmail")  it specify that Portlet Resource URL have
  request param name resource_id
  and its value validateUserbyEmail then annotation below method will be
  executed and method name can be anything. 
Portlet Resource
  URL Creation 
<portlet:resourceURL
  id="validateUserbyEmail" var="validateUserByEmailURL"> 
</portlet:resourceURL> 
Example URL 
http://localhost:8080/web/guest/test?p_p_id=LiferaySpring_WAR_LiferaySpringportlet& 
p_p_lifecycle=2& 
p_p_state=normal& 
p_p_mode=view& 
p_p_resource_id=validateUserbyEmail& 
p_p_cacheability=cacheLevelPage& 
p_p_col_id=column-2& 
p_p_col_count=1 
Method in
  Controller 
@ResourceMapping("validateUserbyEmail") 
public void
  validateUserbyEmail(ResourceRequest request,ResourceResponse response) throws
  SystemException, 
SystemException { 
//Portlet Serve Resource Phase 
} | 
Note: 
We already know in the Portlet
all requested parameters name will be appended with Portlet name space when it
access in the controller we can ignore Portlet name space.
Page Navigation and Render JSP Views in Spring
Portlet
Generally in the Portlet we will
perform different actions and will navigate to different JSP pages. It will be
like when click some links we will go to other page there we will perform some
action then based on action result it will be navigate to particular views.
In Portlet Navigation and render JSP
views will be handled in the Portlet
Render Phase so that it will be executed render methods based on URL
request parameter names and its values.
All JSP views render will be managed
through the request param name and it values and in the controller it will be
taken care by render phase methods
and these methods will be return either string or ModelAndView object.
In the simple navigation or
render view, in the return statement we need to specify the particular JSP page name so that when request
param name and value matches then it will navigate or render specified JSP page
and all JSP pages will be available in the path that we already configured in
the Portlet specific context xml file (WEB-INF/jsp/)
Render Phase
Method in Controller
| 
@RenderMapping(params
  ="action=homepage") 
public String
  renderHomePage(RenderRequest request, RenderResponse response) throws SystemException
  { 
return "homepage"; 
} | 
If we above method invoked it
will render or navigate to homepage.jsp
page.
When we want to render some JSP
page and want to send some data to JSP page, it will be display some data while
render JSP page then we will use method that will be return ModelAndView Object.
In the object constructor first
parameter is JSP page name and other is object parameter we can send any data
or object.
| 
@RenderMapping(params
  ="action=listusers") 
public ModelAndView
  listUsersView(RenderRequest request, RenderResponse response) throws
  SystemException { 
return new
  ModelAndView("list_users","userList",UserLocalServiceUtil.getUsers(-1,
  -1)); 
} | 
When the above method invoked it
will render list_users.jsp
page and we can get all users list while render JSP page.
If we have multiple navigation or
multiple views while using Portlet then we need to create multiple render phase
methods and each and every method will be identified by request parameters names
and its values
| 
@RenderMapping(params
  ="action=listusers") 
public ModelAndView
  listUsersView(RenderRequest request, RenderResponse response) throws
  SystemException { 
return new
  ModelAndView("list_users","userList",UserLocalServiceUtil.getUsers(-1,
  -1)); 
} 
@RenderMapping(params ="action=view") 
public String
  renderHomePage(RenderRequest request, RenderResponse response) throws
  SystemException { 
return "view"; 
} 
@RenderMapping(params
  ="action=validateUser") 
public String
  renderValidateuserPage(RenderRequest request, RenderResponse response) throws
  SystemException { 
return
  "validate_user"; 
} 
@RenderMapping(params
  ="action=viewUserRigistration") 
public String
  renderCreateAccountPage(RenderRequest request, RenderResponse response)
  throws SystemException { 
return
  "form_submission"; 
} 
@RenderMapping(params
  ="action=viewUserRigistrationData") 
public String
  renderUserRegistrationDataPage(RenderRequest request, RenderResponse
  response) throws SystemException { 
return
  "user_registration_data"; 
} | 
To invoke all above methods we
should use Portlet render URL because all methods are part of Render Phase so
we should use Portlet Render URL.
| 
Portlet Render URL
  Creation 
<portlet:renderURL
  var="renderHomeURL"> 
<portlet:param name="action"
  value="homepage"/> 
</portlet:renderURL>  
Example URL 
http://localhost:8080/web/guest/test? 
p_p_id=LiferaySpring_WAR_LiferaySpringportlet& 
p_p_lifecycle=0& 
p_p_state=normal& 
p_p_mode=view& 
p_p_col_id=column-2& 
p_p_col_count=1& 
_LiferaySpring_WAR_LiferaySpringportlet_action=homepage 
Method in
  Controller 
@RenderMapping(params ="action=homepage") 
public String
  renderHomePage(RenderRequest request, RenderResponse response) throws
  SystemException { 
         return "homepage"; 
} | 
Note:
While specify the JPS page name
in the method we should ignore .JSP
extension just page name is enough.
Handling all views with single method
In the above all scenarios we
have written different render phase methods in controller to render different
views based on request parameters name and value match in the controller
annotation rather than that if we want use one method that handle all views rendering
is pretty good .
Render Phase
Method in Controller
| 
Method in
  Controller 
private static final String DEFAULT_VIEW_PAGE_NAME
  = "view"; 
@RenderMapping 
public String
  renderView(RenderRequest request, RenderResponse response) { 
String renderViewPageName =
  ParamUtil.getString(request, "jspPageName"); 
logger.info("renderView
  Method"); 
if (renderViewPageName != null 
&&
  !renderViewPageName.equalsIgnoreCase("")) { 
return
  renderViewPageName.trim(); 
} else { 
return DEFAULT_VIEW_PAGE_NAME; 
} 
} 
Example URLs 
<portlet:renderURL
  var="jspPageOneURL"> 
            <portlet:param name=" jspPageName "
  value="jsppage_one"/> 
</portlet:renderURL> 
<portlet:renderURL
  var="jspPageTwoURL"> 
            <portlet:param name="jspPageName "
  value="jsppage_two"/> 
</portlet:renderURL> 
<portlet:renderURL
  var="jspPageThreeURL"> 
            <portlet:param name="jspPageName "
  value="jsppage_three"/> 
</portlet:renderURL> 
Example URL 
http://localhost:8080/web/guest/test? 
p_p_id=LiferaySpring_WAR_LiferaySpringportlet& 
p_p_lifecycle=0& 
p_p_state=normal& 
p_p_mode=view& 
p_p_col_id=column-2& 
p_p_col_count=1& 
_LiferaySpring_WAR_LiferaySpringportlet_jspPageName
  = jsppage_three | 
In above method will execute
every render request and it will search for parameter name called “jspPageName” if it available then it
will navigate or render the specific JSP page otherwise it will go to default
view or JSP page.
Navigate to JSP page from Action Phase to Render
Phase.
Assume we have submitted some
from once that data stored in the database then we want navigate to specific
JSP page and display stored data in the JSP.
| 
Portlet Action
  URL 
<portlet:actionURL
  var="registrationActionURL" windowState="normal"> 
<portlet:param
  name="action" value="userRegistration"/> 
</portlet:actionURL> 
Action Phase
  Method in Controller 
@ActionMapping(params =
  "action=userRegistration") 
public void
  createAccount(ActionRequest actionRequest, 
ActionResponse actionResponse)
  throws IOException, PortletException{ 
try { 
logger.info("====From
  submitted succesfully====="); 
} catch (Exception e) { 
} 
actionResponse.setRenderParameter("action",
  "viewUserRigistrationData"); 
} 
Render Phase
  Method in Controller 
@RenderMapping(params ="action=viewUserRigistrationData") 
public String
  renderUserRegistrationDataPage(RenderRequest request, RenderResponse
  response) throws SystemException { 
return
  "user_registration_data"; 
} | 
In this case once we performed
action in action phase we will use setRenderParameter
method to set parameter and value to the request URL and as for Portlet
lifecycle it will go Render phase there it will look for specified parameter
and value , if it match then it will render that view.
If we want to use Single method
that handle all views then we have to use as follows
| 
Portlet Action
  URL 
<portlet:actionURL
  var="registrationActionURL" windowState="normal"> 
<portlet:param
  name="action" value="userRegistration"/> 
</portlet:actionURL> 
Action Phase
  Method in Controller 
@ActionMapping(params =
  "action=userRegistration") 
public void
  createAccount(ActionRequest actionRequest, 
ActionResponse actionResponse)
  throws IOException, PortletException{ 
try { 
logger.info("====From
  submitted succesfully====="); 
} catch (Exception e) { 
} 
actionResponse.setRenderParameter("jspPageName",
  " user_registration_data"); 
} 
Render Phase
  Method in Controller 
private static final String DEFAULT_VIEW_PAGE_NAME
  = "view"; 
@RenderMapping 
public String
  renderView(RenderRequest request, RenderResponse response) { 
String renderViewPageName
  = ParamUtil.getString(request, "jspPageName"); 
logger.info("renderView
  Method"); 
if (renderViewPageName != null 
&&
  !renderViewPageName.equalsIgnoreCase("")) { 
return renderViewPageName.trim(); 
} else { 
return DEFAULT_VIEW_PAGE_NAME; 
} 
} | 
Send Data from Controller to JSP Page
Generally in the development we
will send some data from controller to JSP pages.
We can use different ways to can
send data from controller 
Using Set
Attribute Method on Request 
We will use setAttribute(--) to pass data from
controller to JSP page and we will use reference name and Object data as parameters.
actionRequest.setAttribute("userData",
userData);
renderRequest.setAttribute("userData",
userData);
| 
Set Data in the
  Action Phase in Controller 
@ActionMapping(params =
  "action=userRegistration") 
public void
  createAccount(ActionRequest actionRequest, 
ActionResponse actionResponse)
  throws IOException, PortletException{ 
try { 
String firstName =
  ParamUtil.getString(actionRequest, "firstName",StringPool.BLANK); 
String lastName =
  ParamUtil.getString(actionRequest, "lastName",StringPool.BLANK); 
String screeName = ParamUtil.getString(actionRequest,
  "screeName",StringPool.BLANK); 
String emailAddress =
  ParamUtil.getString(actionRequest, 
"emailAddress",StringPool.BLANK); 
int userGander =
  ParamUtil.getInteger(actionRequest, "sex",1); 
String address =
  ParamUtil.getString(actionRequest, "address",StringPool.BLANK); 
List<String> userData=new
  LinkedList<String>(); 
userData.add(firstName); 
userData.add(lastName); 
userData.add(screeName); 
userData.add(emailAddress); 
String
  userGenederString=userGander==1?"Male":"Famale"; 
userData.add(String.valueOf(userGenederString)); 
userData.add(address); 
actionRequest.setAttribute("userData",
  userData); 
// write logic to add data in
  database 
logger.info("====From
  submitted succesfully====="); 
} catch (Exception e) { 
logger.error("user not found"+e.getLocalizedMessage()); 
} 
actionResponse.setRenderParameter("action",
  "viewUserRigistrationData"); 
} 
} 
Access Data in
  JSP Page 
<% 
List<String> userList=new
  LinkedList<String>(); 
if(renderRequest.getAttribute("userData")!=null){ 
userList=(List<String>)renderRequest.getAttribute("userData"); 
} 
%> 
Note: 
While access data we will type
  cast to required object type | 
In render phase also we can set
some data and access in the JSP pages
| 
Set Data in the
  Render Phase in Controller 
@RenderMapping(params
  ="action=listuserssearchcontainer") 
public String
  listUserSeachContainer(RenderRequest request, RenderResponse response) throws
  SystemException { 
List<User>
  userList=UserLocalServiceUtil.getUsers(-1, -1); 
request.setAttribute("userList",
  userList); 
return
  "list_users_search_container"); 
} 
OR 
@RenderMapping(params
  ="action=listuserssearchcontainer") 
public ModelAndView
  listUserSeachContainer(RenderRequest request, RenderResponse response) throws
  SystemException { 
List<User> userList=UserLocalServiceUtil.getUsers(-1,
  -1); 
return new
  ModelAndView("list_users_search_container","userList",userList); 
} 
Access Data in
  JSP Page 
<% 
List<User> userList=new
  LinkedList<String>(); 
if(renderRequest.getAttribute("userList")!=null){ 
userList=(List<User>)renderRequest.getAttribute("userList"); 
} 
%> 
Note: 
While access data we will type
  cast to required object type | 
Set Render Parameters in Action Phase methods
If we want send some string data
then we can use set render parameters
method on action Response object so that these values are appended in
as query string parameters.
| 
Set Render
  Parameter in Action Phase 
@ActionMapping(params =
  "action=userRegistration") 
public void
  createAccount(ActionRequest actionRequest,ActionResponse actionResponse)
  throws PortletException{ 
actionResponse.setRenderParameter("name",
  "meera prince"); 
} 
Get the Parameter
  Value in The Render phase 
@RenderMapping(params
  ="action=homepage") 
public String
  renderHomePage(RenderRequest renderRequest, RenderResponse renderResponse)
  throws SystemException { 
String
  name=ParamUtil.getString(renderRequest,"name"); 
return "homepage"; 
} 
Same parameter
  Value also available in JSP Page 
<% 
String
  name=ParamUtil.getString(renderRequest,"name"); 
%> | 
Access Request Parameters in Controller methods
We can use ParamUtil class or
direct request object to access parameters values from request URL
| 
Portlet Render
  Phase  
@RenderMapping(params
  ="action=view") 
public String
  renderHomePage(RenderRequest renderRequest, RenderResponse renderResponse)
  throws SystemException { 
String
  name=ParamUtil.getString(renderRequest,"name","defualtValue"); 
int
  age=ParamUtil.getInt(renderRequest,"age","defualtValue"); 
Boolean
  isExist=ParamUtil.getBoolean(renderRequest,"isExist",false); 
return "view"; 
} 
Portlet Serve
  Resource Phase  
@ResourceMapping("validateUserbyEmail") 
public void
  validateUserbyEmailAction(ResourceRequest resourceRequest, 
ResourceResponse response)
  throws IOException, PortalException, 
SystemException { 
String
  name=ParamUtil.getString(resourceRequest,"name","defualtValue"); 
int
  age=ParamUtil.getInt(resourceRequest,"age","defualtValue"); 
Boolean
  isExist=ParamUtil.resourceRequest(actionRequest,"isExist",false); 
} 
Portlet Action
  Phase  
@ActionMapping(params =
  "action=userRegistration") 
public void
  createAccount(ActionRequest actionRequest, 
ActionResponse actionResponse)
  throws IOException, PortletException{ 
String
  name=ParamUtil.getString(actionRequest,"name","defualtValue"); 
int age=ParamUtil.getInt(actionRequest,"age","defualtValue"); 
Boolean
  isExist=ParamUtil.getBoolean(actionRequest,"isExist",false); 
} | 
Note:
We can also use request.getParameter(“parameterName”)
to access requested URL parameter values from request URL
Server Resource Phase in Spring Portlet
Generally this Phase we will use
to sever some images, files, xml data and JSON data from server.
To invoke this phase methods we
will use server resource Portlet URL so that it will invokes and finally we can
write or serve different content type data that is JSON, XML, Image, XLS and
other MIME types.
| 
Server Resource
  URL 
<portlet:resourceURL id="validateUserbyEmail"
  var="validateUserByEmailURL"> 
</portlet:resourceURL> 
Serve Resource
  Phase in Controller 
@ResourceMapping("validateUserbyEmail") 
public void
  validateUserbyEmailAction(ResourceRequest request, 
ResourceResponse response)
  throws IOException, PortalException, 
SystemException { 
String emailAddress =
  ParamUtil.getString(request,"emailAddress"); 
ThemeDisplay themeDisplay =
  (ThemeDisplay)
  request.getAttribute(com.liferay.portal.kernel.util.WebKeys.THEME_DISPLAY); 
JSONObject
  jsonUser=JSONFactoryUtil.createJSONObject(); 
try { 
User
  useObject=UserLocalServiceUtil.getUserByEmailAddress(themeDisplay.getCompanyId(),
  emailAddress); 
if(useObject!=null){ 
logger.info("use is
  available"); 
jsonUser.put("success",true); 
jsonUser.put("firstName",useObject.getFirstName()); 
jsonUser.put("lastName",useObject.getLastName()); 
jsonUser.put("email",useObject.getEmailAddress()); 
jsonUser.put("screeName",useObject.getScreenName()); 
} 
logger.info(jsonUser.toString()); 
} 
catch (Exception e) { 
jsonUser.put("success",false); 
logger.error("user not
  found"+e.getLocalizedMessage()); 
} 
logger.info("Resource
  Method"); 
ServletResponseUtil.write(PortalUtil.getHttpServletResponse(response),jsonUser.toString()); 
} 
Note: 
Generally we use this serve
  resource phase to download files or in the Ajax based interaction. | 
To download file or image we will
use following method and we will specify the content type (xml, xls, png, jpg
and other mime formats)
| 
ServletResponseUtil.sendFile(response,
  fileName, bytes, contentType); | 
If we want use multiple server
resource phase methods we simply in Portlet serve resource distinguish with id
attribute
| 
<portlet:resourceURL id="validateUserbyEmail"
  var="validateUserByEmailURL"> 
</portlet:resourceURL> 
<portlet:resourceURL id="validateUserbyUserId"
  var="validateUserByUserIdURL"> 
</portlet:resourceURL> 
@ResourceMapping("validateUserbyEmail") 
public void validateUserbyEmail(ResourceRequest
  request, 
ResourceResponse response)
  throws IOException, PortalException, 
SystemException { 
} 
@ResourceMapping("validateUserbyUserId") 
public void
  validateUserbyUserId(ResourceRequest request, 
ResourceResponse response)
  throws IOException, PortalException, 
SystemException { 
} | 
Portlet Screens
Screen: 1
Screen: 2
Screen: 3
Screen: 4
Screen: 5
Screen: 6
Screen: 7
Author
 

 
 
 Posts
Posts
 
 
