0

我在我的 spring 项目中配置了基于 cookie 的主题解析器和语言环境解析器。本地化在以下配置下正常工作。

<mvc:interceptors>
        <ref bean="localeChangeInterceptor"/>
        <ref bean="themeChangeInterceptor" />
    </mvc:interceptors>

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="language"/>
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
        <property name="cookieName" value="locale"/>
        <property name="cookieMaxAge" value="86400"/>
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>/WEB-INF/i18n/labels</value>
                <value>/WEB-INF/i18n/messages</value>
            </list>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

我还使用以下配置配置了主题解析器。

<bean id="themeSource"
    class="org.springframework.ui.context.support.ResourceBundleThemeSource">
        <property name="basenamePrefix"  value="resources.theme-"/>
    </bean>

    <bean id="themeChangeInterceptor"
        class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
        <property name="paramName" value="theme" />
    </bean>

    <bean id="themeResolver"
        class="org.springframework.web.servlet.theme.CookieThemeResolver">
        <property name="defaultThemeName" value="orange" />
        <property name="cookieName" value="theme"/>
        <property name="cookieMaxAge" value="86400"/>
    </bean>

在我的 JSP 中,我使用以下代码行来加载正确的 CSS。

<link href='${pageContext.servletContext.contextPath}<spring:theme code="css_theme_style"/>' rel="stylesheet" type="text/css"/>

文件theme-orange的内容如下。

css_theme_style=/resources/template/template1/css/style.css

而且我无法在 themeResolver bean 中加载默认主题设置为橙色的主题。当我请求我的主页时,它会尝试使用语言环境 en 查找主题文件。但据我说,它不应该使用语言环境加载主题的属性文件。

我收到以下异常。

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Theme 'orange': No message found under code 'css_theme_style' for locale 'en'.
    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:690)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:479)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:690)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:479)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:457)
    at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:328)
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:419)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:191)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

当我将配置更改为 SessionThemeResolver 时,一切正常。但我只想用 cookie 来做。

请帮我。提前致谢。

4

1 回答 1

2

看起来您的 theme.properties 文件由于某种原因无法访问。尝试将您的 theme.properties 文件放入文件夹中,/src/main/webapp/WEB-INF/classes即如果您使用 Maven src/main/resources(在构建项目时它们将被复制到那里)。

于 2013-08-18T22:49:24.573 回答