繁体   English   中英

春季主题和应用程序上下文

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM