在研究一个模板引擎,选了这个thymeleaf 。中间遇到很多的问题。现在在这里记录一下。
第一步:导入jar包。这里使用maven导入jar包
<!-- thymeleaf 核心包 begin -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity3</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependencies>
<!-- thymeleaf心包 end -->
第二步:spring-mvc-servlet.xml 中加入thymeleaf解析。
<?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:tx="http://www.springframework.org/schema/tx"
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-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!--配置自动扫描的包-->
<context:component-scan base-package="com.sun.controller"/> <!-- 默认servlet -->
<mvc:default-servlet-handler /> <mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven> <!-- 使用jsp解析 -->
<!--配置视图解析器:如何把handle方法返回值解析为实际的物理视图-->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean> --> <!-- 使用thymeleaf解析 -->
<bean id="templateResolver"
class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<!-- 页面缓存,开发时,可设置false,生产中可设置为true -->
<property name="cacheable" value="false" />
</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>
</beans>
这里注意了,templateMode 一定要设置为HTML5 ,不然会出问题。
第三步:控制器的方法
@RequestMapping(path="/thymeleaftest", method = RequestMethod.GET)
public String thymeleaftest(Model model){ System.out.println("thymeleaftest is start");
model.addAttribute("name", "你好,page name ====== thymeleaftest"); return "thymeleaftest";
}
第四步:页面thymeleaftest.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>hello world</title>
</head>
<body>
<B>hello world</B>
<B th:utext="${name}"></B>
<p th:utext="${name}"></p>
</body>
</html>
然后访问,这里的访问和普通的springmvc的地址访问有点区别,thymeleaftest 是action名 + html ,而不是直接就是html名。
http://localhost:8080/myblog/thymeleaftest.html
就能够看到页面的效果了。