版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dahaiaaaqe/article/details/81638599
1 模板引擎的选择
- FreeMarker
- Thymeleaf
- Velocity (1.4版本之后弃用,Spring Framework 4.3版本之后弃用)
- Groovy
- Mustache
-
注:jsp应该尽量避免使用,原因如下:
- jsp只能打包为:war格式,不支持jar格式,只能在标准的容器里面跑(tomcat,jetty都可以)
- 内嵌的Jetty目前不支持JSP
- Undertow不支持jsp
- jsp自定义错误页面不能覆盖spring boot 默认的错误页面
2 freeMarket模板
- 依赖的jar(pom.xml引入)
<!-- Freemarket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2. 默认加载类路径templates
目录下的文件,templates文件夹下存放ftl文件,可以在application.properties
中通过属 性spring.freemarker.template-loader-path
自定义模版文件所在的目录
spring.freemarker.template-loader-path=classpath:/activiti/
//默认前缀为空
spring.freemarker.prefix=
//默认后缀为.ftl
spring.freemarker.suffix=.ftl
#使用缓存
cache: false
#编码设置
charset: UTF-8
check-template-location: true
content-type: text/html
expose-request-attributes: true
expose-session-attributes: true
request-context-attribute: request
3 代码示范
WebController.java
package com.roncoo.education.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* spring-boot-demo-6-1
*
* @author wujing
*/
@Controller
@RequestMapping(value = "/web")
public class WebController {
@RequestMapping(value = "index")
public String index(ModelMap map) {
map.put("title", "freemarker hello word");
return "index"; // 开头不要加上/,linux下面会出错
}
}
SpringBootDemo61Application.java
package com.roncoo.education;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan
@SpringBootApplication
public class SpringBootDemo61Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemo61Application.class, args);
}
}
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/2.1.4/jquery.min.js"></script>
<script>
$(function(){
$('#title').click(function(){
alert('点击了');
});
})
</script>
</body>
</html>
index.css
h1{color: blue;}
pom.xml
<?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>com.roncoo.education</groupId>
<artifactId>spring-boot-demo-6-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-demo-6-1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.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.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>