您的位置:首页 > 编程语言 > Java开发

SpringBoot入门——模板引擎的应用

2017-07-31 00:00 633 查看

SpringBoot入门——模板引擎的应用

总结而已,不喜勿喷~

模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档。

模板引擎可以让(网站)程序实现界面与数据分离,业务代码与逻辑代码的分离,这就大大提升了开发效率,良好的设计也使得代码重用变得更加容易。

1、引用thymeleaf

(性能稍差,不看性能情况下可用)

thymeleaf具体用法请看:

http://www.cnblogs.com/hjwublog/p/5051732.html#_label3

(1)、配置pom.xml

<!-- 引用thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


(2)、配置application.properties

# 是否启用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=


(3)、创建hello.html

在src/main/resources中创建templates文件夹,然后在该文件夹下创建hello.html

<!DOCTYPE html>
<html>
<head>
<!-- 此处meta要闭合 -->
<meta charset="UTF-8"/>
<title>Hello world</title>
</head>
<body>
<p th:text="'Hello!' + ${name}" ></p>
</body>
</html>

在thymeleaf,如果遇到标签没有闭合,可能会报异常

(4)、编写Controller类

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

}


(5)、测试

输入地址: http://localhost:8080/hello 测试是否成功

2、引用freemarker

freemarker具体用法请看:

http://blog.csdn.net/tang9140/article/details/39695653

(1)、配置pom.xml

<!-- 引用freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>


(2)、配置application.properties

#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/


(3)、创建hello.ftl文件

创建一个后缀名为ftl的文件,文件书写格式按照html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Hello world</title>
</head>
<body>
<span>名字:${user}</span>
</body>
</html>


(4)、编写Controller类

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


(5)、测试

输入地址: http://localhost:8080/hello 测试是否成功

3、引用jsp

(1)、配置pom.xml

<!-- 对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>


(2)、配置application.properties

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp


(3)、创建hello.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>


(4)、编写Controller类

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

}


(5)、测试

输入地址: http://localhost:8080/hello 测试是否成功
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thymeleaf freemarker