一、简介
springboot支持多种模版引擎包括:
1. FreeMarker
2. Groovy
3. Thymeleaf (Spring 官网使用这个)
4. Velocity
5. JSP (SpringBoot官方不推荐使用)
下面练习freemarker的使用。
二、导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
三、controller层
@Controller
public class ViewController {
/**
* @description <p></p>
* @return
* @author heshiyuan
* @date 2017/12/2 20:56
*/
@RequestMapping(value = {"/","/index"})
public String index(HttpServletRequest request){
request.setAttribute("content","this is index");
return "index" ;
}
}
四、配置页面
index.ftl
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>this is index</title>
</head>
<body>
<h1>${content}</h1>
</body>
</html>
五、启动
@SpringBootApplication
public class SpringBootFreemarkerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootFreemarkerApplication.class,args);
}
}