[英]Spring themes and application context
我將主題添加到現有的Spring 3.0.5項目中。 我正在使用CookieThemeResolver。 雖然我可以加載主題,但它不是指定的默認值(如theResolver bean中指定的那樣),並且themeChangeInterceptor似乎無法正常工作。
我知道我添加了工作的三個主題配置,因為我將它們(和CSS資源)添加到了春季MVC基礎項目中。 他們工作得很好。 另外,Spring mvc-basic項目不需要web.xml中的ContextLoaderListener,就像我的項目一樣。
最初我沒有一個應用程序上下文(我不需要一個),但是向我的項目中添加主題配置導致該頁面上包含spring:theme標簽的頁面出錯,並抱怨java.lang.IllegalStateException:未找到WebApplicationContext:在spring:theme標簽上沒有注冊ContextLoaderListener。 因此,我添加了一個ContextLoaderListener,並將春季mvc-basic應用程序中沒有的所有內容都放入application-context.xml中。
由於在Spring mvc-basic項目中使用相同的spring標記和主題配置不會發生此錯誤(並且mvc項目沒有偵聽器或上下文參數屬性),因此我必須得出結論,現在我的內容中有一個元素應用程序上下文是問題。
當我的應用程序在下面的配置中運行時,它會加載theme.properties文件(與theme-day.properties和theme-night.properties一起在類路徑中)。 提出?theme = day或?theme = night請求時未設置cookie。 但是,即使將我的日志設置設置為要跟蹤,它也不會引發任何錯誤。
希望有人能夠指出WTF正在發生。 我已經將配置配對到最低限度。 剩余元素中的一個或多個是問題的原因。 如有疑問,請詢問。
我嘗試將所有內容移到應用程序上下文中,但並沒有解決問題。 我試圖刪除元素,但此處發布的內容是最低要求,不會浪費整個應用程序。 在重新檢查我的jar的正確版本和相同版本的POM文件。
更多信息:由於語言環境解析器遵循相同的模式(不需要標簽),我從JSP刪除了所有主題配置和標簽,並將所有聲明移回servlet.xml,並殺死了ContextLoaderListener及其配置。 結果與主題相似,因為春季默認設置(也稱為瀏覽器區域設置的默認設置)可以工作,但是無法設置cookie,並且其他與區域設置功能無關的功能似乎也沒有作用。 將所有內容重新分配給application.xml並放回ContextLoaderListener具有相同的結果。 因此至少這是一致的,由於我的配置的某些部分,區域設置和主題都被破壞了。 同樣,對ContextLoaderListener的需求不是主題標簽的直接功能,而是其他元素之一的副作用。
更新2我已升級到3.0.6,並在跟蹤級別運行時發現以下項目。
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>
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"
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"/>
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>
解決這個問題。
事實證明,語言環境和主題系統無法應用於WEB-INF之外的應用程序元素。
因此,將spring標記嘗試在應用中“松散”的JSP上嘗試使用語言環境和主題會失敗。
因此,在上面的代碼中,我將所有內容移回servlet-context.xml。,將我的標簽從index.jsp文件(位於web-app的根目錄)中移出,並將所有內容移回視圖。 現在一切正常。
我希望這可以幫助別人。 如果您發現不同,請在此處發布
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.