Thursday, February 26, 2015

Spring MVC Portlet in liferay

Spring MVC Portlet in Liferay 


Today we will discuss how to create Spring Portlet in Liferay.For doing this first we create a simple Portlet and then convert it into Spring Portlet.By using Spring Portlet we can handle multiple action request ,multiple resource request in seperate methods.We can have multiple action methods in single class.
Lets Start this step by step:-


Step 1:-Create liferay Plugin Project
Open eclipse click on :-
file->New->Liferay Plugin Project.
Give project name as FirstSpringMVC and finish.








Step 2:-Create MVC Portlet in project
Right Click on project->New->Liferay Portlet



This will open a new window provide portlet class name, package name and select superclass and Click Finish.



This will create a basic portlet .Now we have to convert it into Spring portlet.

Step 3:-Provide Spring jars to classpath
For using Spring we have to provide Spring dependency jars to class path.Following are the list of jars:-

  • 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
  • spring-aspects.jar
  • spring-context-support.jar
  • spring-jms.jar
  • spring-orm.jar
  • spring-oxm.jar
  • spring-web-struts.jar
  • commons-beanutils.jar
  • commons-collections.jar
  • commons-fileupload.jar
  • commons-io.jar
  • commons-lang.jar
  • jstl-api.jar
  • jstl-impl.jar 
So open liferay-plugin-package.properties from web-inf and add the following jars.

liferay-plugin-package.properties


Step 4:-Provide ViewRendererServlet in web.xml
ViewRendererServlet is a bridge servlet, mainly for the Portlet MVC support.For usage with Portlets, this Servlet is necessary to force the portlet container to convert the PortletRequest to a ServletRequest, which it has to do when including a resource via the PortletRequestDispatcher. This allows for reuse of the entire Servlet-based View support even in a Portlet environment.

web.xml

Note=>These are hard code entries and same for every project






Step 5:-Provide entry of Front Controller
Front Controller handle all request/response flow it is mention in portlet.xml as:-

<portlet-class> 
org.springframework.web.portlet.DispatcherPortlet
</portlet-class>

Step 6:-Create Spring Application Context File
Front Controller after receiving request take help from some helper classes these classes are mention in a seperate file whose name is :-
<portlet-name>-portlet.xml 
Ex- first-controller-portlet.xml.

Any Spring Project contain atleast one application context file where all beans and helper classes are defined.So create a folder inside WEB-INF named spring-portlet-config and inside this folder create first-controller-portlet.xml.

first-controller-portlet.xml


Explanation:-


1)DefaultAnnotationHandlerMapping
The helper that help Front Controller that which controller is handled which request ie help to find the controller because there are many Controller but only one Front Controller.

2)<bean class="com.first.FirstController" />
Here we provide the full path of our controller class if we have more than one controller than we can use tag 
<context:component-scan base-package="com.first" />
and provide the package path where all controller resides.

3)<bean id="jspViewResolver" class = " ">
When controller return the name of jsp front controller take help from view Resolver to find the exact path of jsp by using prefix and suffix properties.

4)<context:annotation-config /> 
Because we are using annotation to register bean this tag is mandatory if we are using annotation.

Step 7:-Provide entry of ApplicationContext file
We have to provide the entry of this context file in portlet.xml so that our Front Controller find this file to delegate the request to this file.

Now our portlet.xml look like :-

portlet.xml
Step 8:-Create Controller Class
Open class FirstController and paste this :-

FirstController.java
Explanation:-


1)@Controller(value = "FirstController")
This annotation is class level annotation and register this class as a controller the value is name of class.

2)@RequestMapping("VIEW")
This annotation tell that this controller handle view mode for edit mode you have to create a new controller.

3)@RenderMapping
This annotation is method level annotation and tell that this method is treated as render method.

4)return "hello"
Name of the jsp file which is to be render.





Step 9:-Create View
Here our view is jsp so create hello.jsp (/docroot/html/firstcontroller/hello.jsp)

hello.jsp

Step 8:-Deploy Your Project
Right Click on build.xml->Run As->Ant Build.

Thats it final project Structure is:-



You can download source code from Spring MVC Portlet in Liferay

Hope this will help....

 Related Post:-

Form Handling in Spring MVC Portlet

Ajax in Spring Portlet

Custom Sql in Liferay

Many To Many Relationship mapping in Liferay Services

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven









No comments:

Post a Comment

Total Pageviews

Like Us on Facebook

Number Of Unique Visitor

Free counters!