总结而已,不喜勿喷~html
模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它能够生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档。java
模板引擎能够让(网站)程序实现界面与数据分离,业务代码与逻辑代码的分离,这就大大提高了开发效率,良好的设计也使得代码重用变得更加容易。web
(性能稍差,不看性能状况下可用)spring
thymeleaf具体用法请看:apache
<!-- 引用thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
# 是否启用thymeleaf模板解析 spring.thymeleaf.enabled=true # 是否开启模板缓存(建议:开发环境下设置为false,生产环境设置为true) spring.thymeleaf.cache=false # 检查本地是否存在对应模板 spring.thymeleaf.check-template-location=true # 模板的内容类型设置,默认为text/html spring.thymeleaf.content-type=text/html # 模板的编码设置,默认UTF-8 spring.thymeleaf.encoding=UTF-8 # 设置能够被解析的视图,以逗号,分隔 #spring.thymeleaf.view-names= # 排除不须要被解析视图,以逗号,分隔 #spring.thymeleaf.excluded-view-names= # 模板模式设置,默认为HTML5 #spring.thymeleaf.mode=HTML5 # 前缀设置,SpringBoot默认模板放置在classpath:/template/目录下 spring.thymeleaf.prefix=classpath:/templates/ # 后缀设置,默认为.html spring.thymeleaf.suffix=.html # 模板在模板链中被解析的顺序 #spring.thymeleaf.template-resolver-order=
在src/main/resources中建立templates文件夹,而后在该文件夹下建立hello.htmltomcat
<!DOCTYPE html> <html> <head> <!-- 此处meta要闭合 --> <meta charset="UTF-8"/> <title>Hello world</title> </head> <body> <p th:text="'Hello!' + ${name}" ></p> </body> </html>
在thymeleaf,若是遇到标签没有闭合,可能会报异常springboot
package com.springboot.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TemplateController { // 映射地址是:/templates/hello // 返回html模板 @RequestMapping("/hello") public String hello(Map<String,Object> map){ map.put("name", "TestName"); return "hello"; } }
输入地址: http://localhost:8080/hello 测试是否成功session
freemarker具体用法请看:mvc
<!-- 引用freemarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
#Freemark模板引擎 # 是否使用freemarker模板 spring.freemarker.enabled=true # 是否容许重复请求 spring.freemarker.allow-request-override=false # 是否缓存 spring.freemarker.cache=false # 检查模板位置是否存在 spring.freemarker.check-template-location=true # 设置编码格式 spring.freemarker.charset=UTF-8 # 模板的内容类型设置 spring.freemarker.content-type=text/html # 前缀设置 默认为 "" spring.freemarker.prefix= # 后缀设置 默认为 .ftl spring.freemarker.suffix=.ftl # 在合并以前设置是否应该将全部请求属性添加到模型中 spring.freemarker.expose-request-attributes=false # 在合并以前设置是否应该将全部HttpSession属性添加到模型中 spring.freemarker.expose-session-attributes=false # 是否设置公开使用在springMacroRequestContext下的宏设置 spring.freemarker.expose-spring-macro-helpers=false # 在构建URL时附加到视图名称的后缀。 spring.freemarker.template-loader-path=classpath:/templates/
建立一个后缀名为ftl的文件,文件书写格式按照html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>Hello world</title> </head> <body> <span>名字:${user}</span> </body> </html>
package com.springboot.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TemplateController { /** * 映射地址是:/templates/helloFtl * 返回ftl模板 */ @RequestMapping("/hello") public String hello(Map<String,Object> map){ map.put("name", "TestName"); return "hello"; } }
输入地址: http://localhost:8080/hello 测试是否成功
<!-- 对JSP的解析支持 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- 对JSTL的支持 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
# 页面默认前缀目录 spring.mvc.view.prefix=/WEB-INF/ # 响应页面默认后缀 spring.mvc.view.suffix=.jsp
建立hello.jsp放入src/main/webapp/WEB-INF目录下
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Test JSP</title> </head> <body> <span>JSP名字:${name}</span> </html>
package com.springboot.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TemplateController { @RequestMapping("/hello") public String hello(Map<String,Object> map){ map.put("name", "TestName"); return "hello"; } }
输入地址: http://localhost:8080/hello 测试是否成功