天天看點

SpringBoot實戰之9 整合freemarker模版引擎

一、簡介

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);
    }
}
           

六、項目結構圖

SpringBoot實戰之9 整合freemarker模版引擎

曆史文章

SpringBoot實戰之入門

springboot實戰之文章彙總

springboot實戰之讀取配置檔案

springboot實戰之整合jsp模版引擎

繼續閱讀