第五章 整合视图层技术
使用Vue.js 的前后端分离的化,不需要整合。
一、SpringBoot整合jsp
重点:
注意:创建SpringBoot整合JSP,一定要为war类型,否则会找不到页面。

1、引入依赖
在maven的dependencies的依赖中除了springBoot启动器还要添加对jstl和jsp的依赖。
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
2、在application.properties中修改jsp全局访问设置。
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
接下来就只需要在controller中返回spring类型的视图名称即可访问到WEB-INF/jsp/中相应的jsp文件
3、测试demo
书写 controller
@Controller
public class UserController {
@RequestMapping("/showUser")
public String showUser(Model model) {
List<User> userList = new ArrayList<>();
userList.add(new User("鬼魅传说"));
userList.add(new User("魑魅魍魉"));
userList.add(new User("妖魔"));
model.addAttribute("userList", userList);
return users;
}
}
书写WEB-INF/jsp/users.jsp
<%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@tagbib url=" " prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title></title>
</head>
<body>
<table border="1" width="50%">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${ }</td>
<td>${ }</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
测试页面。
二、springboot整合Thymeleaf
在maven中添加对thymeleaf的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
视图存放的位置为src/main/resources/templates,该目录是安全的,意味着该目录下的内容不允许被外界直接访问。
书写 user类,拥有 id,name,age三个属性。
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; //@Data @ApiModel(description = "用户模型") public class UserPO { public UserPO(String Name) { this.name = Name; } @ApiModelProperty(value = "name") public String name; @ApiModelProperty(value = "id") public String id; @ApiModelProperty(value = "age") public String age;
书写 controller,在model中添加attributeute的userList即可
@Controller
public class UserController {
@RequestMapping("/showUser")
public String showUser(Model model) {
List<User> userList = new ArrayList<>();
userList.add(new User("鬼魅传说"));
userList.add(new User("魑魅魍魉"));
userList.add(new User("妖魔"));
model.addAttribute("userList", userList);
return users;
}
}
编写前端视图src/main/resources/templates/users.html
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<table>
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
</tr>
<tr th:each="user : ${userList}">
<td th:text="${ }"></td>
<td th:text="${ }"></td>
<td th:text="${user.age}"></td>
</tr>
</table>
</body>
</html>
三、SpringBoot整合Freemarker
在maven中除了添加springboot启动器外,还要添加对freemarker的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
书写 controller。
@RequestMapping("/indextwo") public String indextwo(Map<String, Object> map) { map.put("name","美丽的天使..."); return "indextwo"; }
在 src/main/resources/templates/ 创建 indextwo.ftl
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title></title> </head> <body> ${name} </body> </html>
Freemarker 的配置
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=
#comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved
启动项目,访问ip;端口号 /indextwo ,启动成功。

四、springboot整合Velocity
SprintBoot 在2.0之后放弃了对于velocity的支持,原因是该项目已经很长时间没有更新了。
但是目前框架项目使用,velocity当作代码生成器使用。
五、关于Freemarker 和 Thymeleaf 的使用选择。
thymeleaf优点:
静态html嵌入标签属性,浏览器可以直接打开模板文件,便于前后端联调。
springboot官方推荐方案。
thymeleaf缺点
1、模板必须符合xml规范,就这一点就可以判死刑!太不方便了!js脚本必须加入/*<![CDATA[*/标识,否则一个&符号就会导致后台模板合成抛异常,而且错误信息巨不友好,害得我调试了好几个小时才明白是怎么回事。js里面还好办,这样是在html里面含有&等符号,还需要转义?忒麻烦了!
Freemarker 优点:
1、通用目标。能够生成各种文本:Html、Xml、RTF、JAVA源代码等。
2、强大的模版语言。所有常用的指令:include、if/elseif/else。
3、通用数据类型。FreeMaker不是直接反射到java对象,java对象通过插件式对象封装,以变量方式在模版中显示。
Freemarker 缺点:
thymeleaf由于使用了标签属性做为语法,模版页面直接用浏览器渲染,使得前端和后端可以并行开发。freemarket使用</>这样的语法,就无法直接使浏览器渲染出原本页面的样子。
使用场景
1、如果你只有简答的几个html的页面,或者你是初学者html应用很少,网页较少,建议使用 thymeleaf 。
2、如果你确定要使用视图层的架构,来开发产品级的功能,建议使用 Freemarker 。
3、如果你是初学者,建议直接使用 Vue.js 。
六、静态资源访问。
在我们开发Web应用的时候,需要引用大量的js、css、图片等静态资源。
默认配置
Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
/static
/public
/resources
/META-INF/resources
举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件。启动程序后,尝试访问
重要提示: 关于使用vue前后端分离开发项目,也可以将vue的打包后的文件,放置在static文件目录下进行静态资源访问。这样使用的优点是简化了部署,部署的时候不需要再进行前后端分离的部署。
缺点在于程序部署之后,前端无法修改访问后台的数据库端口,或者修改时,比较麻烦。