1. 程式人生 > >spring mvc中添加對Thymeleaf的支持

spring mvc中添加對Thymeleaf的支持

www 如果 odi 最新 character tee api ray nco

1、下載Thymeleaf

官方下載地址:https://dl.bintray.com/thymeleaf/downloads/thymeleaf/

我下載的是最新的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">

待完善。。。

spring mvc中添加對Thymeleaf的支持