主题解析器不与Thymeleaf一起使用

时间:2015-06-23 07:03:39

标签: java spring themes thymeleaf resolver

我想在百里香中使用主题解析器,所以我可以在运行时选择我想要的主题。我已尝试过此onethis

这是我的代码..

# src/main/resources/themes/theme_dark.properties
theme_css=/store/css/dark.css
# src/main/resources/themes/theme_light.properties
theme_css=/store/css/light.css

mvc.xml

<mvc:resources location="/store/img/" mapping="/store/img/**" /> 
<mvc:resources location="/store/css/" mapping="/store/css/**" />

<mvc:annotation-driven />
<context:component-scan base-package="com.javaproject.sls" />

<!-- Internationalization Source, Interceptor and Resolver -->
<bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:label/message" />
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

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

<bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="eng" />
</bean>

<!-- Theme Source, Interceptor and Resolver -->
<bean id="themeSource" 
      class="org.springframework.ui.context.support.ResourceBundleThemeSource">
    <property name="basenamePrefix" value="classpath:themes/theme_" />
</bean>

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

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

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

<bean id="securityInterceptor" class="com.javaproject.sls.security.SecurityInterceptor"/>

<bean id="loginController" class="com.javaproject.sls.controller.LoginController"/>

<bean id="contentNegotiatingResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="html" value="text/html"/>
            <entry key="pdf" value="application/pdf"/>
            <entry key="xsl" value="application/vnd.ms-excel"/>
            <entry key="xml" value="application/xml"/>
            <entry key="json" value="application/json"/>
        </map>
    </property>
</bean>

<!-- Thymeleaf Template and View Resolver -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/" /> 
    <property name="suffix" value=".html" /> 
    <property name="templateMode" value="HTML5" /> 
    <property name="cacheable" value="false"/>
</bean>

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"> 
    <property name="templateResolver" ref="templateResolver" /> 
</bean>

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver"> 
    <property name="templateEngine" ref="templateEngine" /> 
    <property name="characterEncoding" value="UTF-8"/>
</bean>

的login.html

<head >
    <meta charset="utf-8"/>
    <link rel="stylesheet" th:href="#{themes.code('theme_css')}"/>
    <title th:text="#{label.title}"></title>
</head>

<body>
     <a href="?lang=eng">English</a>|<a href="?lang=jpn">Japan</a>
     |<a href="?lang=chn">China</a>

     <a href="?theme=light">Light</a> | <a href="?theme=dark">Dark</a>
</body>

的web.xml

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-security.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>config</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:mvc.xml</param-value>
    </init-param>   
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>JavaProject</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<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>/pages/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <servlet-name>JavaProject</servlet-name>
</filter-mapping>

#themes.code(...)语法不起作用。我的代码有什么问题?有什么我想念的吗?谢谢你的帮助。

注意:我已更新代码,可以部署项目。 #{themes.code('theme_css')}但我遇到的另一个问题是当我点击它时主题不起作用。

0 个答案:

没有答案