1.没有thymeleaf的情况下
dependencies {
// Test
testCompile 'junit:junit:4.12'
testCompile 'org.glassfish.web:javax.el:2.2.4'
testCompile 'org.springframework.boot:spring-boot-starter-test:1.4.1.RELEASE'
// Spring
compile 'org.springframework.boot:spring-boot-starter-aop:1.4.1.RELEASE'
compile 'org.springframework.boot:spring-boot-devtools:1.4.1.RELEASE'
// web
compile 'org.springframework.boot:spring-boot-starter-web'
}
默认配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources)
html放到其中可以自动访问
2.thymeleaf的情况下
dependencies {
// Test
testCompile 'junit:junit:4.12'
testCompile 'org.glassfish.web:javax.el:2.2.4'
testCompile 'org.springframework.boot:spring-boot-starter-test:1.4.1.RELEASE'
// Spring
compile 'org.springframework.boot:spring-boot-starter-aop:1.4.1.RELEASE'
compile 'org.springframework.boot:spring-boot-devtools:1.4.1.RELEASE'
// web
//compile 'org.springframework.boot:spring-boot-starter-web'
// Thymeleaf
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
}
ext['thymeleaf.version'] = '3.0.9.RELEASE'
ext['thymeleaf-layout-dialect.version'] = '2.3.0'
除了html之外的静态资源位置不变,html 放到/templates下
@RequestMapping("t1")
public String thymeleaf1(ModelMap m) {
System.out.println("t1");
User u = new User();
u.setName("zhangsan");
u.setPassword("123456");
m.addAttribute("user", u);
return "thymeTest";
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head >
<meta charset="UTF-8">
<head th:replace="layout/base :: common_header(~{::title},~{::link})">
<title>Awesome - Main</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
<link rel="stylesheet" th:href="@{/themes/smoothness/jquery-ui.css}">
</head>
<body>
hello thymeleaf<br>
<img src="/Tulips.jpg" style="width:100px;">
<span th:text="${user.name}"></span>
<span lang="zh_CN" th:text="#{title.hello}"></span>
<a th:href="@{/home/{username}(username=${user.name})}">a1</a>
<a th:href="|/home/${user.name}|">a2</a>
</body>
</html>