spring mvc中添加对Thymeleaf的支持
1、下载Thymeleaf
官方下载地址:https://dl.bintray.com/thymeleaf/downloads/thymeleaf/
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。我下载的是最新的3.0.11版本
把包里的jar包丢到项目中去:
dist/thymeleaf-3.0.11.RELEASE.jar
dist/thymeleaf-spring5-3.0.11.RELEASE.jar
lib/attoparser-2.0.5.RELEASE.jar
lib/slf4j-api-1.7.25.jar
lib/unbescape-1.1.6.RELEASE.jar
如果是基于maven的项目,在pom.xml中添加如下内容:
基于spring5的配置:
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.11.RELEASE</version> </dependency>
基于spring4的配置:
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.11.RELEASE</version> </dependency>
2、项目中添加对Thymeleaf的支持
修改springmvc的配置文件springMVC-servlet.xml,添加如下内容:
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/WEB-INF/templates/"/> <property name="suffix" value=".html"/> <property name="characterEncoding" value="UTF-8"/> <property name="order" value="1"/> <property name="templateMode" value="HTML5"/> <property name="cacheable" value="false"/> </bean> <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"/> </bean> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine"/> <property name="characterEncoding" value="UTF-8"/> </bean>
在项目的/WEB-INF/templates/添加对应的模板文件,这个跟上面配置文件的内容是对应的,要在模板文件html标签添加xmlns:th属性:
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
在模板中引用变量:
${x}
用于返回request作用域里的x值,或者Thymeleaf作用域里的x值${param.x}
用于返回页面请求参数的x值(有可能会有多值)${session.x}
用于返回session作用域里的x值${application.x}
用于返回application作用域里的x值
待完善。。。

更多精彩