Spring MVC特性里由一个是关于Spring Theme主题的应用,所以写了个Demo
1.这里先看项目结构(Meven项目)
2.所需的POM依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.5.RELEASE</version> </dependency>
3.Properties Theme文件配置
4.编写HTML文件
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="<spring:theme code="color" />" type="text/css" /> </head> <body> <h1 class="red">你好,Hello Word</h1> </body> </html>
5.SpringMVC 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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="controller" /> <context:annotation-config /> <mvc:annotation-driven /> <mvc:resources mapping="/statis/**" location="/statis/" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean> <!--实际这里切换的是Properties的文件,而非Properties值,这里是指Properties的前缀--> <bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource"> <property name="basenamePrefix" value="theme-" /> </bean> <!--实际这里切换的是Properties的文件,而非Properties值,这里是指前缀余下的部分,SPring使用时,会通过前缀和名称进行组合来获取指定的Properties文件--> <bean id="themeResolver" class="org.springframework.web.servlet.theme.SessionThemeResolver"> <property name="defaultThemeName" value="style1" /> </bean> <mvc:interceptors> <bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"> <property name="paramName" value="theme"/> </bean> </mvc:interceptors> </beans>
6.编写Controller
package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class FormController { @RequestMapping(value = "/form") public ModelAndView user() { return new ModelAndView("form", "user","你好"); } }
备注:这样进行切换的时候,实际切换的是使用的文件,而JSP页面使用的Spring:Theme的Code属性实际是Theme Properties文件里面的Key值。
附录:
Spring Framework官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-themeresolver
W3C Spring MVC 4.2.4中文文档:https://www.w3cschool.cn/spring_mvc_documentation_linesh_translation/spring_mvc_documentation_linesh_translation-i79x27rz.html