SSM框架下集成thymeleaf主要分爲三步:html
第一步:引入thymeleafspring
本項目利用maven搭建,因此是用的maven的方式引入的thymeleafmvc
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>${thymeleaf.version}</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>${thymeleaf.version}</version> </dependency>
裏面的版本號能夠本身配置,這裏用的是框架
<!--thymeleaf版本號--> <thymeleaf.version>2.1.2.RELEASE</thymeleaf.version>
第二步:springmvc的xml文件中進行配置maven
在引入thymeleaf成功以後須要在springmvc的xml文件中對其進行配置,配置的話之前的視圖解析器就沒用要,須要註釋掉,而後進行thymeleaf的配置xml
<!-- 使用thymeleaf解析 --> <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value="" /> <property name="templateMode" value="HTML5" /> <property name="cacheable" value="false" /> <property name="characterEncoding" value="UTF-8"/><!--不加會亂碼--> </bean> <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver" /> </bean> <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine" /> <!--解決中文亂碼--> <property name="characterEncoding" value="UTF-8"/> </bean>
第三步:html頁面引入thymeleafhtm
在以上兩步完成以後須要在html中引入thymeleafio
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">