Problems when splitting up applicationContext to multiple file for portlets

1.2k Views Asked by At

I have a plugin project containing two portlets. For each portlet I defined its own applicatioContext file which works well but I have to put some definitions in each applicationContext.xml redundantly which I want to avoid.

I'd prefer to put this code

<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

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

into a parent applicationContext.xml which holds definitions for all portlets and only the specific configurations into the portlet specific applicationContext.xml s. But this doesn't work. If I don't define the jspResolver in each applicationContext.xml it can't be found resulting in an error.

In my portlet.xml I have defined this init-param for each portlet:

<init-param>
    <name>contextConfigLocation</name>
    <value>/WEB-INF/spring-mvc-portlet.xml 
           /WEB-INF/first-portlet.xml</value>
</init-param>

...

<init-param>
    <name>contextConfigLocation</name>
    <value>/WEB-INF/spring-mvc-portlet.xml 
           /WEB-INF/second-portlet.xml</value>
</init-param>

whereby spring-mvc-portlet.xml holds the definitions used from all portlets.



The web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Circular-portlet</display-name>
        <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-portlet.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>view-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring-mvc-servlet.xml
            </param-value>
        </init-param>
        <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>
    <jsp-config>
        <taglib>
            <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
            <taglib-location>
                /WEB-INF/tld/liferay-portlet.tld
            </taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>http://liferay.com/tld/aui</taglib-uri>
            <taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
        </taglib>
    </jsp-config>

</web-app>

The spring-mvc-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:component-scan base-package="com.foo.bar" />

    <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

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

<!--    <context:property-placeholder location="classpath:Config.properties" /> -->
<!-- With Spring 3.1 it should be org.springframework.context.support.PropertySourcesPlaceholderConfigurer -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:Config.properties</value>
        </property>
    </bean>
</beans>

The portlet1-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">

<!-- <import resource="spring-mvc-portlet.xml"/> -->

    <context:component-scan base-package="com.foo.bar" 
                            use-default-filters="true">
        <context:exclude-filter type="assignable" expression="com.foo.bar.portlet2.YyyController"/>
    </context:component-scan>

<!--    <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> -->

<!--    <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> -->
</beans>

The portlet2-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">

<!-- <import resource="spring-mvc-portlet.xml"/> -->

    <bean id="xmlConverter" class="com.foo.bar.utils.XMLConverter">
        <property name="marshaller" ref="castorMarshaller" />
        <property name="unmarshaller" ref="castorMarshaller" />
    </bean>
    <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
        <property name="mappingLocation" value="WEB-INF/res/mappings.xml" />
    </bean>

    <context:component-scan base-package="com.foo.bar" 
                            use-default-filters="true">
        <context:exclude-filter type="assignable" expression="com.foo.bar.portlet.YyyController"/>
    </context:component-scan>

<!--    <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> -->

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

<!--    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> -->
<!--         <property name="location"> -->
<!--             <value>classpath:Config.properties</value> -->
<!--         </property> -->
<!--     </bean> -->
</beans>

The portlet.xml:

<portlet>
    <portlet-name>Portlet1</portlet-name>
    <display-name>Portlet 1</display-name>
    <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
    <init-param>
        <name>contextConfigLocation</name>
        <value>/WEB-INF/portlet1-portlet.xml</value>
    </init-param>
...
1

There are 1 best solutions below

3
On

I think you are half way through. Try to put yoour spring-mvc-portlet.xml into web.xml file like this:

<web-app ...>
    ...
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/path/to/your/spring-mvc-portlet.xml</param-value>
    </context-param>
    ...
</web-app>

It serves as definition of common portlet application context. Then put following lines into portlet.xml:

    <portlet>
        ...
        <init-param>
            <name>contextConfigLocation</name>
            <value>/WEB-INF/path/to/your/first-portlet.xml</value>
        </init-param>
        ...
    </portlet>
    <portlet>
        ...
        <init-param>
            <name>contextConfigLocation</name>
            <value>/WEB-INF/path/to/your/second-portlet.xml</value>
        </init-param>
        ...
    </portlet>

It works for me, so I hope it helps you...