将 applicationContext 拆分为 portlet 的多个文件时出现问题
Posted
技术标签:
【中文标题】将 applicationContext 拆分为 portlet 的多个文件时出现问题【英文标题】:Problems when splitting up applicationContext to multiple file for portlets 【发布时间】:2014-01-08 11:57:33 【问题描述】:我有一个包含两个 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】:我认为你已经完成了一半。 尝试将您的 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>
它对我有用,所以我希望它可以帮助你......
【讨论】:
我试过了,但是当我在我的两个特定上下文文件中注释掉上面提到的行并将它们放入 spring-mv-portlet.xml 时,我得到一个异常,即我在渲染中返回的 JSP找不到作为简单字符串的方法。 您能否提供您获得的异常和堆栈跟踪以及您在渲染方法中使用的代码? 我无法重现错误,但现在两个 portlet 都包含 portlet2 的内容。实际上,一个 portlet 显示了一个开始导入的按钮,而另一个 portlet 显示了一个列表。现在只显示两次导入按钮。以上是关于将 applicationContext 拆分为 portlet 的多个文件时出现问题的主要内容,如果未能解决你的问题,请参考以下文章
spring源码学习之:spring容器的applicationContext启动过程
为啥将拆分为 wav 文件的旋律转换为拆分的 mp3 会在片段边界处产生不好的声音?