freemarker
首先看项目结构
![](https://img.haomeiwen.com/i4714843/54dc8dab93ee3d69.png)
pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- jdk版本号,angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
@SpringBootApplication
public class Main {
@RequestMapping("/")
public String hello(Map<String,Object> map){
map.put("name","lijia");
return "hello";
}
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
hello.ftl
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Hello World ${name}!!!!
</body>
</html>
运行启动类,在浏览器中输入:
http://localhost:8080/
![](https://img.haomeiwen.com/i4714843/6676e19d23cd06cd.png)
thymeleaf
项目结构
![](https://img.haomeiwen.com/i4714843/4197adf48b4e576f.png)
pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- jdk版本号,angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
@SpringBootApplication
public class Main {
@RequestMapping("/")
public String hello(Map<String,Object> map){
map.put("name","lijia");
return "hello";
}
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello World!</title>
</head>
<body>
Hello World <p th:text="${name}"></p>
</body>
</html>
运行启动类,在浏览器中输入:
http://localhost:8080/
![](https://img.haomeiwen.com/i4714843/6676e19d23cd06cd.png)
在thymeleaf中,首先加入下面这句话
![](https://img.haomeiwen.com/i4714843/9d539053000646ea.png)
然后才能使用thymeleaf的语法。
网友评论