Spring主题和应用程序上下文

时间:2011-08-21 21:49:26

标签: java spring spring-mvc themes

我正在为现有的Spring 3.0.5项目添加主题。我正在使用CookieThemeResolver。虽然我可以获得一个主题来加载它不是指定的默认值(在theResolver bean中指定)并且themeChangeInterceptor似乎没有工作。

我知道我添加的三个主题配置,因为我将它们(以及CSS资源)添加到spring MVC-basic项目中。他们工作得很好。另外,Spring mvc-basic项目在web.xml中不需要ContextLoaderListener,就像我的项目一样。

最初我没有应用程序上下文(我不需要)但是在我的项目中添加主题配置导致包含spring:theme标签的页面在该页面上出错并抱怨java.lang.IllegalStateException:否WebApplicationContext发现:没有在spring:theme标签上注册ContextLoaderListener。所以我添加了一个ContextLoaderListener,并将spring mvc-basic app中不存在的everthing放入application-context.xml中。

由于Spring mvc-basic项目中的相同spring标记和主题配置不会发生此错误(并且mvc项目没有侦听器或上下文param属性),我必须得出结论,其中一个元素是什么现在我的应用程序上下文就是问题。

当我的应用程序在下面的配置中运行时,它会加载theme.properties文件(它与class-day.properties和theme-night.properties一起位于类路径中)。当?theme = day或?theme = night请求时,不会设置cookie。但是,即使将我的日志设置设置为跟踪,它也不会抛出任何错误。

希望有人能够指出WTF正在发生。我把配置配对到了最低限度。一个或多个剩余元素是问题的原因。如果您有任何疑问,请询问。

我已经尝试将所有内容移动到应用程序上下文中,但这并没有解决问题。我试图删除元素,但这里发布的元素是最小的,不会破坏整个应用程序。重新检查我的罐子,找到相同版本的正确版本和POM文件。

更多信息:由于语言环境解析器遵循相同的模式(不需要标记),我从JSP中删除了所有主题配置和标记,并将所有声明移回servlet.xml并杀死了ContextLoaderListener及其配置。结果类似于主题,因为spring默认值(也就是默认为浏览器语言环境)可以工作,但是没有设置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>

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

1 个答案:

答案 0 :(得分:0)

解决此问题。

事实证明,语言环境和主题系统无法应用于WEB-INF之外的应用程序元素。

因此,将spring标记尝试在应用程序中“松散”的JSP上使用区域设置和主题将失败。

所以在上面的代码的情况下,我将所有内容都移回到servlet-context.xml中。从index.jsp文件(这是web-app的根目录)中取出我的标签并将所有内容都移回进入意见。现在一切正常。

我希望这有助于某人。如果您发现不同请在此处发布