Search code examples
javaspring-mvcliferay-6spring-portlet-mvc

Liferay : Configuring Multiple Spring MVC Portlets in one liferay plugin project


I am developing Spring MVC based portlet in liferay. Basically I want to configure and maintain 2 or 3 portlets in a single liferay project itself. Can some on guide me with the configuration required for the same. Like config code for portlet.xml, spring config and web config (if its required). I just need to configure a default controller for all my portlets separately so each will land in different landing page.

Does anybody know how to config these portlets ? Any suggestions would be helpful :D

Thanks in advance.


Solution

  • Yes, its possible to configure multiple spring portlets in a single plugin project in such a way that single .war file contains multiple portlets.

    In web.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <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>
    

    In applicationContext.xml

    You can specify common bean configuration for all portlets here.

    <context:annotation-config />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="cache" value="false"/>
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
        <property name="contentType" value="text/html; charset=UTF-8" />
    </bean>
    

    In portlet.xml

    You can specify multiple entries of <portlet> in this file. For spring portlet, you should specify <portlet-class> and <init-param> as below.

        <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
        <init-param>
            <name>contextConfigLocation</name>
            <value>classpath:myportlet-context.xml</value>
        </init-param>
    

    In myportlet-context.xml

    Place your portlet controller classes in my.portlet.package and specify it in this file.

    <context:component-scan base-package="my.portlet.package" />

    In liferay-portlet.xml

    Even this file contains multiple <portlet> tags.

    In Your portlet controller class

    Add annotations to specify controller and to map with portlet mode. You can see here various other mappings available in spring documentation.

    @Controller

    @RequestMapping(value = PortletModeVal.VIEW)

    public class MyPortletControll implements PortletConfigAware