Spring Theme簡單應用
阿新 • • 發佈:2018-12-31
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