在配置好SSM框架后,在pom.xml中添加以下依赖html
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
复制代码
<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>
复制代码
@Configuration
@EnableWebMvc
@ComponentScan({"com.example.controller", "com.example.api"})
public class WebConfig implements WebMvcConfigurer {
/** * 模板解析器 * * @return */
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix(TEMPLATE_PREFIX);
templateResolver.setSuffix(TEMPLATE_SUFFIX);
templateResolver.setCacheable(TEMPLATE_CACHEABLE);
templateResolver.setCharacterEncoding(CHARACTER_ENCODING);
templateResolver.setTemplateMode(TEMPLATE_MODE);
templateResolver.setOrder(TEMPLATE_ORDER);
return templateResolver;
}
/** * 模板引擎 * * @return */
@Bean
public SpringTemplateEngine springTemplateEngine(SpringResourceTemplateResolver templateResolver) {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
/** * 视图解析器 * * @return */
@Bean
public ThymeleafViewResolver viewResolver(SpringTemplateEngine springTemplateEngine) {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(springTemplateEngine);
viewResolver.setCharacterEncoding(CHARACTER_ENCODING);
return viewResolver;
}
//.......其余配置请自行配置
}
复制代码
因为我使用了CONSTANTS类里面的static变量,因此附上Constants类相关的参数java
public final static String CHARACTER_ENCODING = "UTF-8";
/** * thymeleaf模板引擎参数 */
public final static String TEMPLATE_PREFIX = "/WEB-INF/templates/";
public final static String TEMPLATE_SUFFIX = ".html";
public final static Boolean TEMPLATE_CACHEABLE = false;
public final static String TEMPLATE_MODE = "HTML5";
public final static Integer TEMPLATE_ORDER = 1;
复制代码
此配置须要注意如下几点:web
controller:spring
在ModelMap里面随便设置一点值api
@RequestMapping("/test")
public String test(ModelMap map) {
map.put("thText", "设置文本内容");
map.put("thUText", "设置文本内容");
map.put("thValue", "设置当前元素的value值");
map.put("thEach", Arrays.asList("列表", "遍历列表"));
map.put("thIf", "msg is not null");
map.put("thObject", new UserEntity("sadfa","asfasfd","asfsaf","asdfasf","saf","asfd","sadf",1));
return "test";
}
复制代码
test.htmltomcat
<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>TEST</h1>
<h2>Thymeleaf</h2>
<!--th:text 设置当前元素的文本内容,经常使用,优先级不高-->
<p th:text="${thText}" />
<p th:utext="${thUText}" />
<!--th:value 设置当前元素的value值,经常使用,优先级仅比th:text高-->
<input type="text" th:value="${thValue}" />
<!--th:each 遍历列表,经常使用,优先级很高,仅此于代码块的插入-->
<!--th:each 修饰在div上,则div层重复出现,若只想p标签遍历,则修饰在p标签上-->
<div th:each="message : ${thEach}"> <!-- 遍历整个div-p,不推荐-->
<p th:text="${message}" />
</div>
<div> <!--只遍历p,推荐使用-->
<p th:text="${message}" th:each="message : ${thEach}" />
</div>
<!--th:if 条件判断,相似的有th:switch,th:case,优先级仅次于th:each, 其中#strings是变量表达式的内置方法-->
<p th:text="${thIf}" th:if="${not #strings.isEmpty(thIf)}"></p>
<!--th:insert 把代码块插入当前div中,优先级最高,相似的有th:replace,th:include,~{} :代码块表达式 -->
<div th:insert="~{grammar/common::thCommon}"></div>
<!--th:object 声明变量,和*{} 一块儿使用-->
<div th:object="${thObject}">
<p>ID: <span th:text="*{id}" /></p><!--th:text="${thObject.id}"-->
<p>TH: <span th:text="*{username}" /></p><!--${thObject.thName}-->
<p>DE: <span th:text="*{password}" /></p><!--${thObject.desc}-->
</div>
</body>
</html>
复制代码
几点注意:mvc
在html首标签里面加上xmlnsapp
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
复制代码
一样把head的meta设置一个charset=“UTF-8”框架
至此,你就能够去配置tomcat运行项目了。jsp