freemarker
首先看項(xiàng)目結(jié)構(gòu)
Paste_Image.png
pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- jdk版本號(hào)桶现,angel在這里使用1.8,大家修改為大家本地配置的jdk版本號(hào)即可 -->
<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>
啟動(dòng)類:
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>
運(yùn)行啟動(dòng)類鼎姊,在瀏覽器中輸入:
http://localhost:8080/
thymeleaf
項(xiàng)目結(jié)構(gòu)
Paste_Image.png
pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- jdk版本號(hào)骡和,angel在這里使用1.8,大家修改為大家本地配置的jdk版本號(hào)即可 -->
<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>
啟動(dòng)類:
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>
運(yùn)行啟動(dòng)類相寇,在瀏覽器中輸入:
http://localhost:8080/
在thymeleaf中慰于,首先加入下面這句話
然后才能使用thymeleaf的語(yǔ)法。