一、簡介
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);
}
}
六、項目結構圖
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICdzFWRoRXdvN1LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90zdORzYq50dnpWT4FEVkZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TO0AjM1gTM5ADNwITM3EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
曆史文章
SpringBoot實戰之入門
springboot實戰之文章彙總
springboot實戰之讀取配置檔案
springboot實戰之整合jsp模版引擎