一、springboot整合thymeleaf 

1、开发准备

a、引入thymeleaf的包

    <!-- thymeleaf模板 -->
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
     </dependency>

 

b、开发阶段把thymeleaf缓存去掉,在application.yml文件添加如下内容:cache默认为true

spring: 
 thymeleaf: 
  cache: false

 

c、html引入 

<html lang="en" xmlns:th="http://www.thymeleaf.org">

d、目录结构

2、代码编写

@Controller
public class HelloController {

    @GetMapping("/user/list")
    public String list(Model model){
        model.addAttribute("he", "user用户铭");
        return "user/list";
    }
    
    @ApiOperation(value="添加用户")
    @RequestMapping("/user/add")
    public String validate(Model model,UserDto userDto){
        model.addAttribute("he", "user用户铭");
        return "redirect:/user/list";
    }
}

 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>用户列表</title>
</head>
<body>
<span th:text="${he}">用户列表</span>
</body>
</html>

3、功能测试

浏览器测试:http://localhost:8080/user/list,输出

user用户铭

二、springboot整合jsp

1、开发准备

a、引入jsp的包

        <!-- 添加jstl标签库依赖模块 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

 

b、修改springboot默认的模板配置,application.yml文件添加如下内容:

## JSP配置
# 页面默认前缀
spring:
 mvc: 
  view: 
   prefix: /WEB-INF/jsp/
# 响应页面默认后缀
   suffix: .jsp

#springboot 2.x有这个功能,是修改jsp内容不用重启
server: 
 servlet: 
  jsp: 
   init-parameters: 
    development: true

 

c、目前结构

 

2、代码编写

同上,jsp使用el表达式即可

3、功能测试

同上

posted on 2019-12-23 17:11  欧欧专栏  阅读(929)  评论(0)    收藏  举报