Java Spring主题和应用程序上下文
我正在为现有的Spring3.0.5项目添加主题。我正在使用CookieThemeResolver。虽然我可以获得一个要加载的主题,但它不是指定的默认值(如theResolver bean中所指定的),并且themeChangeInterceptor似乎不起作用 我知道我添加的三个主题配置是有效的,因为我将它们(以及CSS资源)添加到SpringMVCBasic项目中。他们工作得很好。此外,SpringMVC基本项目也不像我的项目那样需要web.xml中的ContextLoaderListener 最初我没有应用程序上下文(我不需要),但是将主题配置添加到我的项目中会导致包含spring:theme标记的页面在该页面上出错,并抱怨java.lang.IllegalStateException:找不到WebApplicationContext:在spring:theme标记上没有注册ContextLoaderListener。因此,我添加了一个ContextLoaderListener,并将spring mvc basic应用程序中不包含的所有内容放入application-context.xml中 由于spring mvc基本项目中的相同spring标记和主题配置不会出现此错误(并且mvc项目没有侦听器或上下文参数属性),因此我必须得出结论,现在我的应用程序上下文中的一个元素就是问题所在 当我的应用程序在下面的配置中运行时,它会加载theme.properties文件(与theme-day.properties和theme-night.properties一起位于类路径中)。当发出?主题=白天或?主题=夜间请求时,不设置cookie。但是,即使将“我的日志”设置打开以进行跟踪,它也不会抛出任何错误 希望有人能指出世跆拳道正在发生。我已将配置重新配对到最小值。剩余的一个或多个元件是问题的原因。如果你有问题,请提问 我已经尝试将所有内容移动到应用程序上下文中,但这并没有解决问题。我已经尝试删除元素,但这里发布的内容是最低限度的,不会破坏整个应用程序。在上,重新检查了我的JAR的正确版本和相同版本的POM文件 更多信息:由于区域设置解析器遵循相同的模式(不需要标记),我从JSP中删除了所有主题配置和标记,并将所有声明移回servlet.xml,并删除了ContextLoaderListener及其配置。结果与主题类似,因为spring默认值(也称为浏览器区域设置的默认值)可以工作,但无法设置cookie,而且与区域设置功能相关的其他功能似乎都无法工作。中断每个回application.xml并将ContextLoaderListener放回具有相同的结果。所以至少这是一致的,由于我的配置的某些部分,区域设置和主题都被破坏了。此外,对ContextLoaderListener的需求不是主题标记的直接功能,而是其他元素之一的副作用 更新2 我升级到3.0.6,在跟踪级别运行时发现了以下项目Java Spring主题和应用程序上下文,java,spring,spring-mvc,themes,Java,Spring,Spring Mvc,Themes,我正在为现有的Spring3.0.5项目添加主题。我正在使用CookieThemeResolver。虽然我可以获得一个要加载的主题,但它不是指定的默认值(如theResolver bean中所指定的),并且themeChangeInterceptor似乎不起作用 我知道我添加的三个主题配置是有效的,因为我将它们(以及CSS资源)添加到SpringMVCBasic项目中。他们工作得很好。此外,SpringMVC基本项目也不像我的项目那样需要web.xml中的ContextLoaderListene
DEBUG ResourceBundleThemeSource,"http-bio-8080"-exec-5: 109 - Theme created: name 'theme',basename [theme]
这条线显示在渲染每个视图之前。它(对我)表明ResourceBundleThemeSource只是在默认情况下运行
TRACE DefaultListableBeanFactory,Thread-7:201 Ignoring Constructor [public.org.springframework.web.servlet.handler.MappedInterceptor(java.lang.String[],org.springframework.web.context.request.WebRequestInterceptor)] of bean 'org.springframework.web.servlet.handler.MappedInterceptor#1' :org.springframework.beans.factoryUnsatisfiedDependancyException: Error creating bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#1':Unsatisfied dependnacy expressed through contructor argument with index 1 of type [org.springframework.web.context.request.WebRequestInterceptor]: Could not convert constructor argument value of type [org.springframework.web.servlet.theme.ThemeChangeInterceptor] to required type [org.springframeworkweb.context.request.WebRequestInterceptor]: Failed to convert value of type 'org.springframework.web.servlet.theme.ThemeChangeInterceptor' to required type 'org.springframeworkweb.context.request.WebRequestInterceptor';nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.servlet.theme.ThemeChangeInterceptor] to required type [org.springframeworkweb.context.request.WebRequestInterceptor]: no matching editors or conversion strategy found
应用程序初始化时出现此错误。由于MVC Basic项目中使用了完全相同的配置,我不确定为什么会出现这种情况。我明白它在说什么,我只是不知道出了什么问题
设置如下
servlet-context.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:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="org.myproject.test" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
</mvc:interceptors>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="0" />
</bean>
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Theme source and Resolver definition -->
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="theme-" />
</bean>
<bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="day" />
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Read the config.properties file-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<bean id="myService" class="com.stuff.generated.service1">
<constructor-arg value="${service1UrlPrefix}?wsdl"/>
</bean>
<bean id="myServiceFactory" factory-bean="myService1" factory-method="getServicePort"/>
<bean id="myOtherService" class="com.stuff.generated.service2">
<constructor-arg value="${service2UrlPrefix}?wsdl"/>
</bean>
<bean id="myOtherServiceFactory" factory-bean="myService2" factory-method="getServicePort2"/>
<!-- assembles some variables used widely which are autowired into other classes
contains getter and setters and some code running in afterPropertiesSet() method -->
<bean id="myStartVars" class="com.myClass.myStartVars"/>
<bean id="autowiredAnnotationBeanPostProcessor" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
application-context.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:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="org.myproject.test" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
</mvc:interceptors>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="0" />
</bean>
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Theme source and Resolver definition -->
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="theme-" />
</bean>
<bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="day" />
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Read the config.properties file-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<bean id="myService" class="com.stuff.generated.service1">
<constructor-arg value="${service1UrlPrefix}?wsdl"/>
</bean>
<bean id="myServiceFactory" factory-bean="myService1" factory-method="getServicePort"/>
<bean id="myOtherService" class="com.stuff.generated.service2">
<constructor-arg value="${service2UrlPrefix}?wsdl"/>
</bean>
<bean id="myOtherServiceFactory" factory-bean="myService2" factory-method="getServicePort2"/>
<!-- assembles some variables used widely which are autowired into other classes
contains getter and setters and some code running in afterPropertiesSet() method -->
<bean id="myStartVars" class="com.myClass.myStartVars"/>
<bean id="autowiredAnnotationBeanPostProcessor" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
classpath:config.properties
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Reads request input using UTF-8 encoding -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Handles all requests into the application -->
<servlet>
<servlet-name>myTest Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value> /WEB-INF/spring/servlet-context.xml </param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myTest Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/application-context.xml</param-value>
</context-param>
字符编码滤波器
org.springframework.web.filter.CharacterEncodingFilter
编码
UTF-8
强制编码
符合事实的
字符编码滤波器
/*
mytestservlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/servlet-context.xml
1.
mytestservlet
/
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/spring/application-context.xml
此问题的解决方案 事实证明,区域设置和主题系统无法应用于WEB-INF之外的应用程序元素 因此,将试图使用区域设置和主题的spring标记放在应用程序中“松散”的JSP上将失败 因此,在上面的代码中,我将所有内容移回servlet context.xml.,从index.jsp文件(位于web应用程序的根目录)中取出标记,并将所有内容移回视图。现在一切都好了
我希望这对某人有帮助。如果您有不同的发现,请在此处发布我不确定您所说的“我没有应用程序上下文(我不需要应用程序上下文)”是什么意思,因为每个Spring应用程序都有应用程序上下文。仅供参考,
元素用于整个web应用程序,而contextConfigLocation尤其用于一个servlet。首先,我会将这些配置文件合并成一个(不管是哪一个),以确保没有奇怪的交互。另外,您是否真的发布了错误发生时使用的真正最小配置文件?XML似乎无效。如spring示例所示,可能有一个应用程序只有一个servlet上下文。spring然后将其向上推。它在文档中。最初它们位于一个文件servlet-conext.xml中,出于未知原因,我不得不创建一个应用程序上下文。我之所以说unknown,是因为我能够将主题标记和配置添加到springmvcbasic中,并且它只在servlet上下文中运行良好。是的,这些是我剪切粘贴的真实文件。堆栈溢出解释器破坏了注释的外观,但是