Blog Post

...

Liferay Spring MVC Portlet (+JPA, Maven) part.1

Recently we’ve received a new request by our client to implement a Liferay Portlet that uses JPA as a primary ORM-system. Sure, Liferay provides Service Builders that enable ORM functionality in portlets, but I was always wondering why some companies prefer to develop their own frameworks instead of using already existing good solutions. So in this task we had to move from Liferay ORM + Liferay Transactions to well known and mature frameworks: Java JPA + Spring.

First of all we need to clarify some terms:

Servlet – is a Java implementation to support HTTP request/response protocol. So every servlet includes some code to handle client requests (POST/GET/…) and usually maintained by a servlet container (Tomcat, JBoss,…).

Portlets – are pluggable managed my portal web applications that define user interface look. These can be observed as small applications (email, user management, calendar) combined in a single portal view. Portals like servlets handle requests/responses, but in contrast to servlets portlets support only 3 request types: action, resource, render.

Spring Web MVC – is the implementation of Model-View-Controller pattern, build around Spring’s DispatcherServlet, where Controller is a Java Old Plain Object (POJO) that maps request urls to corresponding methods. The concept of Spring MVC is the same as in Servlets (handle HTTP requests), and Servlets can be substituted by Spring MVC providing easier setup and reducing development time. POJO Controller example:

@Controller
public class HelloWorldController {

    @RequestMapping("/helloWorld")
    public String helloWorld(Model model) {
        model.addAttribute("message", "Hello World!");
        return "helloWorld";
    }
}

Spring Portlet MVC - developed to support Portlet specification (JSR-168), the concepts of it are the same as in Spring Web MVC with differences created by the unique workflow of portlets.

The main way in which portlet workflow differs from servlet workflow is that the request to the portlet can have two distinct phases: the action phase and the render phase. The action phase is executed only once and is where any 'backend' changes or actions occur, such as making changes in a database. The render phase then produces what is displayed to the user each time the display is refreshed.

Like in ordinary portlets, the DispatcherPortlet should be declared in the portlet.xml of the web application:

<portlet>
    <portlet-name>sample</portlet-name>
    <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
    <supports>
        <mime-type>text/html</mime-type>
        <portlet-mode>view</portlet-mode>
    </supports>
    <portlet-info>
        <title>Sample Portlet</title>
    </portlet-info>
</portlet>

 

The rendering in Portlet MVC is an extension of Spring Web MVC that reuses all view abilities of Spring. This rendering converts Portlet Requests/Responses to HttpServletRequests/Responses and then calls the render() method of the View interface. To make this work, DispatcherPortlet should use a ViewRendererServlet which must be declared in web.xml. Here is the final web.xml file for given application that will have only 2 entries: 

<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>

As we can see from the upper snippet – ViewRendererServlet mapped to /WEB-INF/servlet/view URL. One more thing we should add to use Spring view rendering mechanism is to define a PortletModeHandlerMapping, which will bind incoming requests based on the current mode of the portlet (“view”, “edit”, “help”). Here is spring context file snippet:

<bean id="portletModeHandlerMapping"
    class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
    <property name="portletModeMap">
        <map>
            <entry key="view">
                <ref bean="requestController" />
            </entry>
        </map>
    </property>     
</bean>

Request controller for Portlet MVC must implement *.portlet.mvc.Controller interface and appropriate to Portlet workflow methods handleActionRequest(), handleRenderRequest():

public interface Controller {

    ModelAndView handleRenderRequest(RenderRequest request, RenderResponse response)
        throws Exception;

    void handleActionRequest(ActionRequest request, ActionResponse response)
        throws Exception;

}

As mentioned before, Portlet MVC uses all Spring view functions, that allows us to inject ViewResolver implementation to current portlet. There are different out-of-the-box ViewResolver can be used (JSP, Freemarker, Velocity), we will use JSP resolver that is also defined in spring context file:

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.InternalResourceView" />
    <property name="prefix" value="/" />
    <property name="suffix" value=".jsp" />
</bean>

In order not to mess up the Portlet configuration with the Maven definitions I put maven dependency list to the end of this article:

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>x.x.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>x.x.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>x.x.RELEASE</version>
        </dependency>       
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc-portlet</artifactId>
            <version>x.x.RELEASE</version>
        </dependency>   

Actually you will need spring-tx and spring-orm libraries for JPA implementation only, so you can omit these 2 dependencies.

At this point our Liferay Spring Portlet is ready. In next article I’ll show how to inject JPA to this portlet.

Comments (44)

Tags: liferay


Comments:

...

Credo Systemz Mar 01, 2017 at 05:50 #

great blog to read... thanks a lot for sharing

...

blue prism Training Aug 21, 2018 at 15:51 #

This is a great article. It gave me a lot of useful information. I use it everyday to check my ranking and keep me focused on what I need to do. Thank you for making the honest effort to discuss this. I feel very strong about it and want to learn more. It could be extremely useful and helpful for me and my friends.

...

KISHORE Nov 21, 2018 at 11:40 #

quite informative, thanks for sharing with us.Thanks for posting this useful content, Good to know about new things here, Your post is really awesome. Your blog is really helpful for me to develop my skills in a right way. Thanks for sharing this unique information with us.

...

Saranraj Nov 28, 2018 at 13:11 #

Thanks for this kind of worthy information. this was really very helpful to me. keep continuing.

...

Sam Daniel Jan 28, 2019 at 08:59 #

Wonderful Blog. Keep Posting.

...

Advanced Excel Training Jan 28, 2019 at 08:59 #

Keep Blogging

...

Shanthi Feb 16, 2019 at 06:04 #

Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live

...

rijaz May 07, 2019 at 12:34 #

Hey, would you mind if I share your blog with my twitter group

...

Microsoft azure training in bangalore Jun 15, 2019 at 12:12 #

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

...

python training in bangalore Jun 15, 2019 at 13:13 #

The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.

...

Tableau online training Jun 20, 2019 at 08:06 #

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

...

MSBI online training Jun 27, 2019 at 07:46 #

Really great post, Thank you for sharing This knowledge.Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!

...

jerline jho Jul 20, 2019 at 12:45 #

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.

...

jothikumar Aug 17, 2019 at 10:55 #

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

...

Selenium training in Chennai Aug 28, 2019 at 08:50 #

Very informative and nice article

...

RPA Training in Chennai Aug 28, 2019 at 08:52 #

Nice information

...

RPA Course in Chennai Aug 28, 2019 at 08:54 #

good article

...

Data Science Training in Chennai Aug 28, 2019 at 08:55 #

Nice information

...

Digital Marketing Course Training in Chennai Aug 30, 2019 at 14:41 #

This is the first & best article to make me satisfied by presenting good content. I feel so happy and delighted. Thank you so much for this article.

...

Software testing online training Aug 31, 2019 at 15:43 #

I am sure this post has helped me save many hours of browsing other related posts just to find what I was looking for. Many thanks!

...

anvitha Sep 09, 2019 at 13:55 #

Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog.. If you are looking for any Data science Related information please visit our website !

...

tech guy Sep 11, 2019 at 11:28 #

Nice Post For Data Science training in Bangalore, Visit: Data Science training in Bangalore

...

techy Sep 11, 2019 at 14:43 #

Good article. For Blockchain training in Bangalore, Visit: Blockchain training in Bangalore

...

tech guy Sep 14, 2019 at 13:08 #

Nice Post For Data Science training in Bangalore, Visit: Data Science training in Bangalore

...

pragyachitra Sep 21, 2019 at 15:05 #

Good Post, I am a big believer in posting comments on sites to let the blog writers know that they ve added something advantageous to the world wide web.

...

jithukutty Oct 04, 2019 at 11:22 #

DOT NET TRAINING IN CHENNAI

...

jithukutty Oct 04, 2019 at 11:26 #

DOT NET TRAINING IN CHENNAI

...

jithukutty Oct 04, 2019 at 13:59 #

INTERVIEW QUESTIONS

...

anushiya Oct 15, 2019 at 09:31 #

Very informative. Thanks for the post I have book marked this blog big data training in chennai inplant training in chennai for it students summer internships for mca students

...

meenu kutty Oct 20, 2019 at 08:36 #

Really Very Informative...Glad to find your blog...Keep Sharing... TESTING & TRAINING ON SELENIUM ORACLE TRAINING IN CHENNAI PYTHON TRAINING IN CHENNAI

...

ANANDHI Nov 03, 2019 at 13:29 #

Excellent information. Very useful to everyone and thanks for sharing this. https://www.wikitechy.com/hosting/kenya/shared-web-hosting

...

ANU Nov 03, 2019 at 13:30 #

Very informative. Thanks for the post I have book marked this blog Kenya Shared Web Hosting Dominican Republic Web Hosting Dominican Republic Jordan Web Hosting

Leave a Comment