1.新建SpringBoot工程,pom.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zaakman.mvc</groupId>
<artifactId>mvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>mvc Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
</dependencies>
<build>
<finalName>mvc</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.MainController代码
@Controller
@EnableAutoConfiguration
@ComponentScan
public class MainController {
@RequestMapping("/")
public String home(){
return "index";
}
public static void main(String argv[]){
SpringApplication.run(MainController.class,argv);
}
}
3.新建个HelloController
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("name","Hello world Thymeleaf");
// model.addAttribute("path","http://res.itkeyword.com/images/itkeyword/no-avatar.png");
return "hello";
}
@RequestMapping("/person")
public String person(ModelMap model){
List<Person> persons = new ArrayList<>();
persons.add(new Person("tom",23,173));
persons.add(new Person("david",26,183));
persons.add(new Person("lion",22,178));
model.put("persons",persons);
return "person";
}
@RequestMapping(value = "/person/{username}",method=RequestMethod.GET)
public String personByName(@PathVariable String username, ModelMap model){
if ("tom".equals(username)){
model.put("info","tom is a handsome boy!");
}else{
model.put("info","he is not tom!");
}
return "personInfo";
}
public class Person{
public String name;
public int age;
public int heigt;
public Person(String name,int age, int heigt){
this.name = name;
this.age = age;
this.heigt = heigt;
}
public String getName(){
return name;
}
}
}
3.person.html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr th:each="person:${persons}">
<td>
<a th:text="${person.name}" th:href="${'/person/'+person.name}"></a>
<p th:text="${person.name}"></p>
<p th:text="${person.age}"></p>
<p th:text="${person.heigt}"></p>
</td>
</tr>
</table>
</body>
</html>
主要实践一下如何展示列表,以及跳转页面
4.personInfo.html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#info{
color: #ff4930;
}
.person{
color: #67eeff;
}
</style>
</head>
<body>
<p id="info" th:text="${info}">message</p>
<p class="person" th:text="${info}">message</p>
</body>
</html>
加入了css代码
5.application.properties
server.port=8081
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=LEGACYHTML5