Pages

Wednesday 5 March 2014

Spring MVC Portlet in Liferay with annotations







Spring MVC Portlet in Liferay 
I 
Spring framework is well known framework and provide lots of features and functionality. These features and functionality are organized in modular fashion. Spring MVC is part of Web module of Spring framework.

Spring also support its counter part Spring MVC for portlet. Portlet MVC framework is mirror image of Web MVC framework in Spring. There are little different in Spring MVC Portlet framework.


We will first get brief introduction about Spring MVC (Web MVC) framework and then we see how to write Spring MVC Portlet in Liferay.


In Spring MVC, there are 3 things. 1) M-model 2)V-view 3)C-Controller. Following is the core architecture of Spring (Web) MVC framework.





  • You can see that, Front Controller works as C(Controller).
  • which will take the incoming request and dispatch to related handler (Controller).
  • Handler (Controller) will process the request and send back the data in form of M(Model) back to Front Controller
  • Then Front Controller will select particular view with the help of View Resolver and send response back to client.
For detail information about the Spring (WEB) MVC framework you can refer the following link.

In Spring MVC Portlet framework, the Front Controller will be DispatcherPortlet instead of DispatcherServlet.
Now let us start work for Spring MVC portlet in liferay.  Below are the steps to create Spring MVC portlet.

STEP 1 :- CREATING PORTLET PROJECT SKELETON BY CREATING LIFERAY PORTLET


We will first create the skeleton of portlet project by creating Liferay MVC portlet. Then we will do changes to migrate it into Spring MVC Portlet.
Please refer my previous blog Create Liferay Portlet and create Liferay MVC portlet. Give project name as "first-spring-mvc"(and "-portlet" will be appended by wizard. so don't give it explicitly in project name).

After successfully creating Spring MVC portlet, the project structure will look like as per below screenshot.




STEP 2 :- DEFINING PORTLET CLASS FOR SPRING MVC 

Open portlet.xml file under WEB-INF folder. It will look like below snippet.

  1. <portlet>  
  2.   <portlet-name>first-spring-mvc</portlet-name>  
  3.   <display-name>First Spring Mvc</display-name>  
  4.   <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>  
  5.   <init-param>  
  6.    <name>view-jsp</name>  
  7.    <value>/view.jsp</value>  
  8.   </init-param>  
  9.   <expiration-cache>0</expiration-cache>  
  10.   <supports>  
  11.    <mime-type>text/html</mime-type>  
  12.   </supports>  
  13.   <portlet-info>  
  14.    <title>First Spring Mvc</title>  
  15.    <short-title>First Spring Mvc</short-title>  
  16.    <keywords>First Spring Mvc</keywords>  
  17.   </portlet-info>  
  18.   <security-role-ref>  
  19.    <role-name>administrator</role-name>  
  20.   </security-role-ref>  
  21.   <security-role-ref>  
  22.    <role-name>guest</role-name>  
  23.   </security-role-ref>  
  24.   <security-role-ref>  
  25.    <role-name>power-user</role-name>  
  26.   </security-role-ref>  
  27.   <security-role-ref>  
  28.    <role-name>user</role-name>  
  29.   </security-role-ref>  
  30.  </portlet>  


Now our portlet class will be DispatcherPortlet (provided by Spring Framework). Replace
com.liferay.util.bridges.mvc.MVCPortlet with org.springframework.web.portlet.DispatcherPortlet so that it will look like as per below snippet

  1. <portlet-name>first-spring-mvc</portlet-name>  
  2.   <display-name>First Spring Mvc</display-name>  
  3.   <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>  

I have just shown first few lines of portlet.xml file to show the change (in <portlet-class>). Rest content of portlet.xml will remain as it is.


After doing this change,you may get error like The portlet class org.springframework.web.portlet.DispatcherPortlet was not found on the Java Build Path in portlet.xml file.


This is because the class DispatcherPortlet is not yet present in the class path. I will show how to resolve this in STEP 5.

STEP 3 :- CREATING SPRING APPLICATION CONTEXT FILE 

Next is to define the Spring application context file. Any spring project must have atleast one Spring application context (xml) file where all beans are defined.


We will create one xml file just under docroot/WEB-INF folder and name it first-spring-mvc-portlet.xml. The file name is not just co-incident the same name as portlet. We must have to follow the following pattern for Spring application context file name.


<<PORTLET_NAME>>-portlet.xml 
  • Where PORTLET_NAME is the value of <portlet-name> element in portlet.xml file.
  • Make sure that all character should be in lower case in application context file name.
  • For Example, if value of <portlet-name> in portlet.xml file is SampleInput then the Spring application context file must be sampleinput-portlet.xml
Add the following content in it.

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.  xmlns:context="http://www.springframework.org/schema/context"  
  5.  xmlns:aop="http://www.springframework.org/schema/aop"  
  6.  xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.         http://www.springframework.org/schema/context  
  10.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  11.         http://www.springframework.org/schema/aop  
  12.   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  13.  <context:annotation-config />  
  14.  <bean  
  15.   class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
  16. </beans>  

Explanation:-
  • It defines parent element <beans> 
  • We have defined two elements <context:annotation-config> and <bean  class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />. This both entries are required to execute annotation that we will use in controller class in STEP 9 in this blog.

STEP 4 :- POINTING SPRING APPLICATION CONTEXT FILE TO PORTLET CLASS.

We had defined DisptacherPortlet class as portlet class in portlet.xml file. Its our central(Front) controller. We have to tell it where we have defined our application context file.

We will point it through init param in portlet.xml file. param name will be "contextConfigLocation" and its value will the location of spring application context file.

After adding this, the portlet.xml file will look like below snippet (I am only showing the code which I have changed)


<portlet-name>first-spring-mvc</portlet-name>  
<display-name>First Spring Mvc</display-name>  
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>  
 <init-param>  
  <name>contextConfigLocation</name>  
  <value>/WEB-INF/first-spring-mvc-portlet.xml</value>  
 </init-param>  
 <init-param>  
  <name>view-jsp</name>  
  <value>/view.jsp</value>  
 </init-param>  


Note:- 
  • You are free to define Spring application context file in any folder under WEB-INF folder. You have to just mention the location accordingly in <value> element under <init-param> element in portlet.xml file.
  • For example if you created folder name context just under WEB-INF and placed spring application context file in it then you have to give the path as /WEB-INF/context/first-spring-mvc-portlet.xml in <value> element under <init-param> 
STEP 5 :- PUTTING SPRING JARS INTO CLASS PATH.

Next step is to put Spring dependencies (JARs) into class path. For this you NO need to search JARs explicitly. Just follow below steps to add these dependencies.

open file liferay-plugin-package.properties resides just under /WEB-INF folder. It should looks like below snippet.


  1. name=First Spring Mvc  
  2. module-group-id=liferay  
  3. module-incremental-version=1  
  4. tags=  
  5. short-description=  
  6. change-log=  
  7. page-url=http://www.liferay.com  
  8. author=Liferay, Inc.  
  9. licenses=LGPL  


Now while you have opened it(liferay-plugin-package.properties file ) just open Properties tab as shown in below screenshot.



In Properties tab you will notice Portal Dependency Jars and Portal Dependency Tlds section at right side. On clicking on Add button from Portal Dependency Jars, small window will be opened from where we can select the JAR files as per below screenshot.



select on following JARs and click on OK button.


    spring-web-servlet.jar
    spring-web-portlet.jar
    spring-web.jar
    spring-transaction.jar
    spring-jdbc.jar
    spring-expression.jar
    spring-core.jar
    spring-context.jar
    spring-beans.jar
    spring-asm.jar
    spring-aop.jar
    commons-beanutils.jar
    commons-collections.jar
    commons-fileupload.jar
    commons-io.jar
    commons-lang.jar
    jstl-api.jar
    spring-context-support.jar
    jstl-impl.jar

Note:- jstl-api.jar and jstl-impl.jar is not required for spring MVC portlet. But I have just added if we need to use JSTL tag.


The similar way you can also add TLDs.


After adding these JARs, just open the Source tab of liferay-plugin-package.properties file. It will be look like below screenshot.





You will notice that, new property portal-dependency-jars added and its value is comma separated JARs that we added through Properties tab.

Once you become expert, you can add/remove Jars files directly through Source tab. 


till now, we only prepared list of JARs required in classpath. Still these JARs are not present in classpath. We need to build and deploy the portlet so that these JARs are made available in classpath.


To know how this works, first open the Liferay Portlet Plugin API just under the project before deploying portlet. It will looks like below screenshot.






You can notice that just few JARs are present under it.


Now just build and deploy the portlet. You can just drag build.xml file to Ant window, expand it and double click on 'deploy' target. You can refer my previous blog 
Create Liferay Portlet to know how to build and deploy portlet.

Once the portlet is deployed,refresh the project and open the Liferay Portlet Plugin API library again. You will observe that all the Jars that we added in liferay-plugin-package.properties  file are added here as shown in below screenshot.






This way, whatever JARs are required, just put in liferay-plugin-package.properties file and deploy the portlet. They will be automatically placed in class path. If you not find any JAR in this list then you have to explicitly add into lib folder. 


You can observe that the error in portlet.xml file came in STEP 2 will be removed. If you still able to see the same error then just do clean and build the project from Eclipse-->Project menu.


STEP 6 :- PUTTING ViewRenderServlet ENTRY IN web.xml FILE.

This is very small but very important steps. Since SpringMVC will support all its functionality in portlet context with the help of this servlet. Define this Servlet entry as per below snippet.


<servlet>  
  <servlet-name>view-servlet</servlet-name>  
  <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>  
  <load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
  <servlet-name>view-servlet</servlet-name>  
  <url-pattern>/WEB-INF/servlet/view</url-pattern>  
</servlet-mapping>  

These are hard-core setting so just put this entry in web.xml as it is.

STEP 7 :- CONFIGURING VIEW

SpringMVC framework support many view technologies (like JSP, Freemarker,Velocity,PDF etc). For simplicity, we will use the jsp.

In Spring framework, DispatcherPortlet will take the help of ViewResolver to choose the view (JSP in our case).


So we need to configure view (through View Resolver) in Spring application context file (first-spring-mvc-portlet.xml ). Add following entry in first-spring-mvc-portlet.xml file



<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.InternalResourceView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>

Explanation:-
  • We had defined ViewResolver as bean in application context file
  • In prefix property we have to give the folder path where we kept all JSP files.
  • in suffix property we have to give as ".jsp".
  • For example, suppose we created jsp folder directly under /WEB-INF, then the prefix will be "/WEB-INF/jsp/" and suffix will be ".jsp". 
  • I will show how this works in STEP 9.
  • The Order property is not required if we have only one View Resolver. If we define more than one View Resolver (ex. one is for JSP, another is for Freemarker etc.) then we have to give the order value so that DispatcherPortlet will scan in that order to find the view.
STEP 8 :- CREATE JSP

Now we will create JSP. Create folder jsp under /WEB-INF folder and create one jsp file called defaultRender.jsp and simple write one line in it like "<h1>This is Default Render Jsp</h1>"

STEP 9 :- CREATE REQUEST HANDLER (CONTOLER)

If you observe the very first image in this blog, I have mentioned controller (right side). These controllers will do the actual job. The Front Controller (DispatcherPortlet) will only delegate the request to appropriate request handler (Controller).

So our next step is to write request handler. We will call it Controller, so don't confuse this Controller with the Front Controller (DispatchPortlet).


First create pacakge com.myowncompany.test.springmvc.controller and class MyFirstSpringMVCTestController in it. You are free to choose any package and class name you want. I had put "Controller" at the end of class name to just denote that this is my controller, however its not required but a good practice. 


I also give name "controller" to package so that its clearly understand that this is the package where all my controller are reside. Again its not required.


Portlet will have View, Edit and Help mode. In Spring MVC Portlet framework, we can have separate Controller (Request Handler) for each these mode.


We have to explicitly tell DispatcherPortlet that which mode will be supported by Controller. We will do this by giving annotation to MyFirstSpringMVCTestController so that it will looks like below snippet.
  1. package com.myowncompany.test.springmvc.controller;  
  2.   
  3. import javax.portlet.RenderRequest;  
  4. import javax.portlet.RenderResponse;  
  5.   
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.ui.Model;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.portlet.bind.annotation.RenderMapping;  
  10.   
  11. @Controller(value = "MyFirstSpringMVCTestController")  
  12. @RequestMapping("VIEW")  
  13. public class MyFirstSpringMVCTestController {  
  14.   
  15.   
  16. }  

Explanation:
  • First we had given annotation @Controller which will used to denote that this is our controller. Value will be same as class name
  • Second annotation is @RequestMapping("VIEW") which tell to DispatcherPortlet  that this controller will support VIEW mode.
  • One controller can support only one portlet mode at a time. So in case if we require EDIT mode, then we have to write separate Controller.

Add following method in MyFirstSpringMVCTestController class
@RenderMapping  
 public String handleRenderRequest(RenderRequest request,RenderResponse response,Model model){  
      return "defaultRender";  }         

Explanation:-
  • first we have defined @RenderMapping annotation to this method. This annotation tell that this is default render method. It means whenever we place this portlet, it will render this method. You also can write another render method with "action" as key and its value. When we create RenderURL and passing value which match the "action" value of render method, then it will be called. I will show how to write another render method with "action" as key in next blog.
  • The method name is handleRenderRequest. You are free to give any name.
  • Its first parameter is RenderRequest and second parameter is RenderResponse. third parameter is object of type Model. We can set attribute in this Model object and can access it in JSP. We will see it in next blog.
  • For simplicity there is no any other code and this method is returning just one String "defaultRender". We have to return the name of the JSP at the end of this render method. 
  • In STEP 7 we have defined prefix as /WEB-INF/jsp/ and suffix as ".jsp" in View Resolver. So in this case it will pre-pand "/WEB-INF/jsp/" to defaulRender and append ".jsp". So the final string will be /WEB-INF/jsp/defaultRender.jsp. 
  • This way DispatcherPortlet will find the jsp path and render it.
STEP 10 :- DEFINE CONTROLLER(REQUEST HANDLER) IN SPRING APPLICATION CONTEXT

The last step is to define Controller in Spring application context file. Open the spring application context file first-spring-mvc-portlet.xml and put the entry for MyFirstSpringMVCTestController class. It will look like as per below snippet.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.  xmlns:context="http://www.springframework.org/schema/context"  
  5.  xmlns:util="http://www.springframework.org/schema/util"  
  6.  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.         http://www.springframework.org/schema/context  
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.         http://www.springframework.org/schema/util  
  11.         http://www.springframework.org/schema/util/spring-util-3.0.xsd">  
  12.           
  13.  <context:annotation-config />  
  14.  <bean  
  15.   class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
  16.   
  17.  <bean class="com.myowncompany.test.springmvc.controller.MyFirstSpringMVCTestController" />  
  18.    
  19.  <bean id="jspViewResolver"  
  20.   class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  21.   <property name="viewClass"  
  22.    value="org.springframework.web.servlet.view.InternalResourceView" />  
  23.   <property name="prefix" value="/WEB-INF/jsp/" />  
  24.   <property name="suffix" value=".jsp" />  
  25.   <property name="order" value="1" />  
  26.  </bean>  
  27. </beans>  


Explanation:-

I have added entry for MyFirstSpringMVCTestController as bean.
And it’s done. You can now build and deploy this portlet. Place it on Liferay page and you will notice that whatever we have written in defaultRender jsp page will display.

So far I have used just one method (default render) render method. We can add more than one render method (which will be differentiate by value of "action" key). 


We also can add more than one Action method and Resource Method. 


Action method will be called when we create url by <portlet:actionURL> and resource method can be called when we create url by <portlet:resourceURL> from JSP.


I have written separate blog 
Render and Action methods in Spring MVC portlet into explain how these methods (Render and Action) will work in Spring MVC Portlet framework. 

You can refer current blog to create TEMPLATE for Spring MVC Portlet project.


To know more about Render and Action method in Spring MVC portlet.



Render and Action methods in Spring MVC portlet in Liferay
In this article, I will show how to write render and action methods in Spring MVC portlet created in Liferay.

This article is an addendum of my previous blog 
Spring MVC Portlet in Liferay So Please refer it to understand how to create Spring MVC portlet in Liferay.

In last blog (
Spring MVC Portlet in Liferay) we have created default render method. We will continue the same example in that blog and will see the following things.

  • Create Render method with "action" as key
  • Create Action method with "action" as key with Default Render method
  • Create Action method with "action" as key with Specific Render method

1.Create Render method with "action" as key

We have seen how to create default render method and when we deploy the portlet, the default render method get executed. Sometimes it requires to call specific render method  instead of default render method. 

Let us do it practically.

Add following code in MyFirstSpringMVCTestController  class
  1. @RenderMapping(params = "action=renderOne")  
  2. public String renderOneMethod(RenderRequest request, RenderResponse response, Model model){  
  3.   return "render1";  
  4.  }  


Explanation:-
  • This method having @RenderMapping annotation with params attribute. 
  • The value of params  is action=renderOne. It means this render method will be called when we create RenderURL from JSP and pass action parameter with value renderOne
  • In another word, the key of this render method(value of action) should be match with the value of action request parameter that we pass in renderURL
  • This method return string "render1". It means it will render "render1.jsp". This is similar concept that we have seen for default Render method in previous blog (the returning string represent the jsp name).
  • Method name can be anything you want.
Create Jsp file called render1.jsp at the same path (WEB-INF/jsp)where we have created defaultRender.jsp and place one line content like "<h1>This is Render 1 JSP</h1>"

Add following code into defaultRender.jsp file.
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>    
<h1>This is Default Render Jsp</h1>  
  <portlet:defineObjects/>  
  <portlet:renderURL var="renderOneMethodURL">  
 <portlet:param name="action" value="renderOne"></portlet:param>  
</portlet:renderURL>  
  <a href="${renderOneMethodURL}">Call RenderOne method</a>  


Explanation:-

  • First we have added portlet taglib reference 
  • Second line we added in previous blog to just denote that this is default render method.
  • Next we have created renderURL with the help of <portlet:renderURL> tag. We have given var name so that it can be referred anywhere in the page with JSTL. We also passed portlet parameter called action and its value as "renderOne"
  • If you notice, we kept the same value for action parameter as the key action for render method (in annotation). It means this renderURL will call the render method in spring controller with matching action parameter value with action key.
Now its time to see our work. Build and deploy the portlet. We already place the portlet on liferay page. So just refresh it to see the changes. You can notice that the defaultRender jsp get render and generate output as per below screenshot.


Now click on link Call RenderOne method and you will notice that the rende1.jsp get displayed as per below screenshot.


If you want, you can put logger at each method to see the flow of execution.



2.Create Action method with "action" as key with Default Render method

We can create the Action method with similar way we have created the above Render method. Add the code in defaultRender.jsp so that it will looks like below snippet.

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>  
  
<h1>This is Default Render Jsp</h1>  
  
<portlet:defineObjects/>  
  
<portlet:renderURL var="renderOneMethodURL">  
 <portlet:param name="action" value="renderOne"></portlet:param>  
</portlet:renderURL>  
  
<portlet:actionURL var="actionOneMethodURL">  
  <portlet:param name="action" value="actionOne"></portlet:param>  
</portlet:actionURL>  
  
<a href="${renderOneMethodURL}">Call RenderOne method</a>  
  
<form action="${actionOneMethodURL}" method="post">  
 User Name: <input type="text" name="userName">  
 <input type="submit">    
</form>  


Explanation:-

  • We have now created actionURL with variable actionOneMethodURL so that it can be refer anywhere in jsp with JSTL.
  • We also have passed action parameter with value "actionOne"
  • We will create the action method in controller class with action key and set its value as "actionOne" so that it can be call when we actionURL called from jsp.
  • At the end we had created form and placed one input text and submit button. The action of the form will be actionURL created by <portlet:action> tag.
  • Here the concept will be same as renderURL. When we hit the submit button, the respective action method will be called who's action key value is matched with the action parameter value in  <portlet:action> tag.
Next, we will create action method as per below snippet in controller class.
  1. @ActionMapping(params = "action=actionOne")   
  2.  public void actionOneMethod(ActionRequest request, ActionResponse response) {  
  3.   String userName=ParamUtil.get(request, "userName", "");  
  4.   log.info("userName is==>"+userName);  
  5.  }  


Explanation:-

  • We have created action method with annoation @ActionMapping and its param attribute is "action=actionOne". It means this action method have key action and its value is actionOne
  • So this method will be called by actionURL who's action parameter value is "actionOne".
  • Method name can be anything you want.
  • In this method, I am accessing request parameter through Liferay utility class(ParamUtil).
  • At the last, I am printing the request parameter through logger. You can create logger in Liferay by creating class level static variable with the help of Liferay utility class LogFactoryUtil as shown in below snippet.
  1. private static Log log = LogFactoryUtil.getLog(MyFirstSpringMVCTestController.class);  

  • We have to pass class reference in which we are defining logger in getLog method
  • You can notice that, we not returning anything. I mean the return type of this method is void.
  • So you may wonder, after this method get executed what is the output. And the answer is it will execute default Render method.
Now its time to see this in action. Deploy the portlet and you will see the portlet get rendered as per below screenshot.


Give any value in text box, say Nilang and click on Submit button. You will notice that the same jsp (defaultRender.jsp) will be displayed. You can check the logs and confirm that control goes to action method and execute the logs and then execute the default render method. Below is the snapshot of logs.


You can put log in default render method to understand it more clear.


3.Create Action method with "action" as key with Specific Render method

We have seen that after action method get executed, control will goes to default render method. Spring MVC framework also provides a way to execute specific render method instead of default render after executing action method.

To test it, create one render method as per below snippet.
  1. @RenderMapping(params = "action=renderAfterAction")   
  2.  public String testRenderMethod(RenderRequest request, RenderResponse response){  
  3.   log.info("In renderAfterAction method");  
  4.   return "renderAfterAction";  
  5.  }  


Explanation:-

  • we created render method with its key action set to renderAfterAction. Also we are returning "renderAfterAction". Means it will search for renderAfterAction.jsp file under /WEB-INF/jsp folder.
Create new jsp file renderAfterAction under /WEB-INF/jsp folder and just enter one line content in it like "<h1>This is Render After Action JSP </h1>"

Next we will do modification in our action method so that it will looks like below snippet.
  1. @ActionMapping(params = "action=actionOne")   
  2.  public void actionOneMethod(ActionRequest request, ActionResponse response) {  
  3.   String userName=ParamUtil.get(request, "userName", "");  
  4.   log.info("userName is==>"+userName);  
  5.   response.setRenderParameter("action", "renderAfterAction");  
  6.  }  

Explanation:-

  • The only change we have done is added last line code response.setRenderParameter("action", "renderAfterAction");
  • This will tell controller that after executing the action method, set render parameter action to "renderAfterAction".
  • It means, the action method wants to execute specific render method with value of action key set to  renderAfterAction
  • So after executing this action method it will execute testRenderMethod (who's value of action key is testRenderMethod)

Note:- setRenderParameter method is available only for object of type ActionResponse.

Deploy the folder and it will show defaultRender jsp. Give the name as "Nilang" and click on Submit button. You will see the render method renderAfterAction get execute as per below screen shot.

You also can notice the log to understand the flow as per below screenshot.

You can notice that after the action method get executed, control goes to renderAfterAction method.

Its done. you can try some more Actions and Render method combination to get more dipper knowledge.


NOTE :  Passing Objects, Collections from   @ActionMapping(params="action=addForm")     to
@RenderMapping(params="action=displayForm")         ;  

Set the Object or Collection in  actionRequest.setAttribute("pojo", pojo);
Now you can acess the object/collection in render as follows
Pojo pojoRender = (Pojo)renderRequest.getAttribute("pojo");

Similarly to redirect to custom render methos in @ActionMapping()
Use  : actionResponse.setRenderParameter("action", "displayForm");

2 comments:

  1. Hi

    i created a sample spring portlet using annotations, i am autowire the spring services but i am unable to use it in velocity template, as it is throwing bean-not-found exception.
    please help me .
    thanks in advance.

    ReplyDelete
  2. You should write your own content. This blog is complete copy of http://www.opensource-techblog.com/2012/09/spring-mvc-portlet-in-liferay.html. Please remove this copy blog or else ready for legal action.

    ReplyDelete