目录
一.SpringBoot整合JSP
二.SpringBoot整合FreeMarker
三.SpringBoot整合ThymeLeaf
①创建工程设置配置文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
com.bjsxt
08-spring-boot-view-jsp
0.0.1-SNAPSHOT
1.7
org.springframework.boot
spring-boot-starter-web
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
provided
②创建 springBoot 的全局配置文件 application.properties
设置视图的前缀和后缀:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
③创建 实体和Controller
public class Users {
private Integer userid;
private String username;
private Integer userage;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getUserage() {
return userage;
}
public void setUserage(Integer userage) {
this.userage = userage;
}
public Users(Integer userid, String username, Integer userage) {
super();
this.userid = userid;
this.username = username;
this.userage = userage;
}
public Users() {
super();
}
}
/**
* SpringBoot整合jsp
*
*
*/
@Controller
public class UserController {
/*
* 处理请求,产生数据
*/
@RequestMapping("/showUser")
public String showUser(Model model){
List list = new ArrayList<>();
list.add(new Users(1,"张三",20));
list.add(new Users(2,"李四",22));
list.add(new Users(3,"王五",24));
//需要一个Model对象
model.addAttribute("list", list);
//跳转视图
return "userList";
}
}
④创建 jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Insert title here
ID
Name
Age
${user.userid }
${user.username }
${user.userage }
⑤创建启动类
/**
* SpringBoot启动类
*
*
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
运行测试:http://localhost:8080/showUser
①创建项目配置文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
com.bjsxt
09-spring-boot-view-freemarker
0.0.1-SNAPSHOT
1.7
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-freemarker
②编写视图
注意:springBoot 要求模板形式的视图层技术的文件必须要放到 src/main/resources 目录下必须要一个名称为 templates
展示用户数据
ID
Name
Age
<#list list as user >
${user.userid}
${user.username}
${user.userage}
#list>
③创建User和Controller 同上(SpringBoot整合JSP)
④创建启动器 同上
运行测试结果同上
①环境搭建
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
com.bjsxt
11-spring-boot-view-thymeleaf-RUL
0.0.1-SNAPSHOT
1.7
3.0.2.RELEASE
2.0.4
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
②创建存放视图的目录
目录位置:src/main/resources/templates
templates:该目录是安全的。意味着该目录下的内容是不允许外界直接访问的
Thymelaef 是通过他特定语法对 html 的标记做渲染
③编写Controller
/**
* Thymeleaf 入门案例
*
*
*/
@Controller
public class DemoController {
@RequestMapping("/show")
public String showInfo(Model model){
model.addAttribute("msg", "Thymeleaf 第一个案例");
return "index";
}
}
④创建视图
Thymeleaf 入门
⑤创建启动类 同上
运行测试:http://localhost:8080/show
⑥解决异常
方式一:
严格遵守html语法
方式二:
修改pom文件,设置Thymeleaf.jar:更新为 3.0 以上的版本,thymeleaf-layout-dialect.jar:更新为 2.0 以上的版本
1.7
3.0.2.RELEASE
2.0.4
重新运行结果: