一茁帽、FreeMarker模板引擎
Spring Boot支持FreeMarker模板引擎十嘿,它是在Spring MVC基礎(chǔ)上加入了自動(dòng)配置的特性因惭,在使用FreeMarker模板引擎的時(shí)候,需要在resources文件夾內(nèi)建立static文件夾存放靜態(tài)資源(一般包括css绩衷、images蹦魔、js),還要建立一個(gè)templates文件唇聘,用于存放FreeMarker模板版姑。
1)依賴配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot.example</groupId>
<artifactId>springboot-freemarker</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-freemarker</name>
<description>Spring Boot整合FreeMarker</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Spring Boot對(duì)靜態(tài)資源友好z支持,包括對(duì)WebJars的支持也非常友好迟郎,這里也使用了WebJars剥险。
2)index.ftl模板
<!DOCTYPE html>
<html>
<head lang="en">
<title>Spring Boot Demo - FreeMarker</title>
<link href="/css/index.css" rel="stylesheet"/>
</head>
<body>
<center>
<img src="/images/logo.png"/>
<h1 id="title">${title}</h1>
</center>
<script type="text/javascript" src="/webjars/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function () {
$('#title').click(function () {
alert('點(diǎn)擊了');
});
})
</script>
</body>
</html>
上面jquery的資源就來自于WebJars,WebJars將常見的web靜態(tài)資源封裝到了jar包中宪肖,這樣更加方便管理和版本控制表制。
3)controller代碼
package com.lemon.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @author lemon
*/
@Controller
@RequestMapping("/web")
public class WebController {
@RequestMapping("/index")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("title", "Hello World");
modelAndView.setViewName("index");
return modelAndView;
}
@RequestMapping("/index1")
public String index1(ModelMap modelMap) {
modelMap.addAttribute("title", "Hello World!!!");
return "index";
}
}
注意:注解@Controller支持Model健爬、ModelMap以及ModelAndView,如果是@RestController么介,將僅僅支持ModelAndView娜遵。
二、Thymeleaf引擎模板
Spring Boot默認(rèn)使用的是Thymeleaf引擎模板壤短,它的模板是HTML格式的设拟,里面使用的th命名空間。使用方法和FreeMarker一模一樣久脯,只需要將FreeMarker的依賴改成Thymeleaf的依賴纳胧,將Thymeleaf的模板替換FreeMarker的模板即可。
1)依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2)模板
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
<title>Spring Boot Demo - Thymeleaf</title>
<link href="/css/index.css" rel="stylesheet" />
</head>
<body>
<center>
<img src="/images/logo.png" />
<h1 id="title" th:text="${title}"></h1>
</center>
<script type="text/javascript" src="/webjars/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
$('#title').click(function(){
alert('點(diǎn)擊了');
});
})
</script>
</body>
</html>