首先, 首先, 的首先, 就是 主题 和静态资源的显示大多数是在JSP页面中显示(springMVC有多种视图),
但是今天我学习的时候,犯了一个错, 就是不通过Controller, 直接访问 .jsp文件, 然后里面有 spring:theme标签就一直报错:
javax.servlet.jsp.JspTagException: Theme 'theme': No message found under code 'theme.css' for locale 'zh_CN'.
而且直接访问.jsp文件, 在使用<mvc:resources 映射的静态资源时,只能使用图片文件, css和js文件是不能访问的
so, 切记, 一定要经过Controller后定向到jsp,经过spring渲染,才能正常显示,
其中 forward:/xx.jsp 功能正常,
redirect:/xx.jsp 和直接访问.jsp文件一样, 会报错,或不能访问
言归正传, 直接看配置:
文件结构:
springMVC.xml:
<!-- 专用拦截器 -->
<mvc:interceptors>
<!-- 主题改变拦截器 -->
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"></bean>
<!-- 本地化拦截器 依赖 localeResolver -->
<bean class=" org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>
<!-- 主题 -->
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="conf.theme."/>
</bean>
<bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="default"/>
</bean>
<!-- 本地化-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="language"/>
<property name="cookieMaxAge" value="3600"/>
<property name="defaultLocale" value="zh_CN"/>
</bean>
<mvc:resources location="/resource/theme/" mapping="/res/**"/>
default.properties: 默认主题配置文件
theme.css=res/default/css/
theme.img=res/default/img/
theme.js=res/default/js/
red.properties:
theme.css=res/red/css/
theme.img=res/red/img/
theme.js=res/red/js/
jsp中使用:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link type="text/css" rel="stylesheet" href='./<spring:theme code="theme.css"/>/main.css'> <title>Insert title here</title> </head> <body> <h1>主题测试</h1> <img alt="" src="./<spring:theme code='theme.img'/>/logo.png"> </body> </html>