将applicationContext拆分为portlet的多个文件时出现问题

时间:2014-01-08 11:57:33

标签: configuration applicationcontext spring-portlet-mvc

我有一个包含两个portlet的插件项目。对于每个portlet,我定义了自己的applicatioContext文件,该文件运行良好但我必须在每个 applicationContext.xml 中冗余地放置一些我想要避免的定义。

我更愿意把这段代码

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

到一个父applicationContext.xml中,它将所有portlet的定义和特定配置保存到特定于portlet的applicationContext.xml中。 但这不起作用。如果我没有在每个applicationContext.xml中定义jspResolver,则找不到它会导致错误。

在我的 portlet.xml 中,我为每个portlet定义了这个init-param:

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

其中 spring-mvc-portlet.xml 包含从所有portlet使用的定义。

<小时/>
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>

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>

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>

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>

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 个答案:

答案 0 :(得分:0)

我认为你已经过了一半。 尝试将 spring-mvc-portlet.xml 放入web.xml文件中,如下所示:

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

它用作公共portlet应用程序上下文的定义。然后将以下行放入 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>

它对我有用,所以我希望它可以帮助你...