thymeleaf简单使用

spring boot + gradle + idea 

结构


1.首先创建spring boot项目 导入依赖

dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
   runtime('org.springframework.boot:spring-boot-devtools')
   testCompile('org.springframework.boot:spring-boot-starter-test')
   // 添加 Thymeleaf 的依赖
   compile('org.springframework.boot:spring-boot-starter-thymeleaf')

}

2.配置yml文件

spring:
  thymeleaf:
    cache: false # 热部署
    encoding: UTF-8 # 字符编码
    mode: HTML5 # 使用H5标准

3.写controller

@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping
    public ModelAndView userAll(Model map) {
        User user = new User("1", "张三", "45@qq.com");
        // userRepository.saveUpdateUser(user);
        map.addAttribute("userList", userRepository.selectAll());
        map.addAttribute("title","用户管理");
        return new ModelAndView("users/list", "userModel", map);
    }

    /**
     * 查看用户信息
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("{id}")
    public ModelAndView view(@PathVariable("id") String id, Model model) {
        User user = userRepository.selectUserById(id);
        model.addAttribute("user",user);
        model.addAttribute("title","查看用户");
        return new ModelAndView("users/view","userModel",model);
    }


    /**
     * 创建或修改
     * @param model
     * @return
     */
    @RequestMapping("/form")
    public ModelAndView createUser(Model model) {
        User user = new User();
        model.addAttribute("user",user);
        model.addAttribute("title","创建用户");
        return new ModelAndView("users/form","userModel",model);
    }

    @RequestMapping
    public ModelAndView saveUpdateUser(User user, Model model) {
        user = userRepository.saveUpdateUser(user);
        return new ModelAndView("redirect:/users");
    }
}

4. 页面

list.html

<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection -->
<html xmlns:th="http://www.thymeleaf.org"
       xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:replace="~{view/heand :: header}"></div>
<h3 th:text="${userModel.title}"></h3>
<a href="list.html" th:href="@{/users/form}">创建用户</a>
<table border="1">
    <thead>
    <tr>
        <td>id</td>
        <td>emile</td>
        <td>name</td>
    </tr>
    </thead>
    <tbody>
    <tr th:if="${userModel.userList.size()} eq 0">
        <td colspan="3">没有有户信息!!!</td>
    </tr>
    <tr th:each="user : ${userModel.userList}">
        <td th:text="${user.id}"></td>
        <td th:text="${user.emile}"></td>
        <td th:text="${user.name}"></td>
        <td></td>
        <td></td>
    </tr>
    </tbody>

</table>
<div th:replace="~{view/fooder :: fooder}"></div>
</body>
</html>

form.html

<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection -->
<html xmlns:th="http://www.thymeleaf.org"
       xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:replace="~{view/heand :: header}"></div>
<h3 th:text="${userModel.title}"></h3>
<form action="/users" th:action="@{/users}" method="post">
    <input type="hidden" name="id">
    姓名:<input type="text" name="name"><br/>
    邮箱:<input type="text" name="emile"><br/>
    <input type="submit" value="提交">
</form>
<div th:replace="~{view/fooder :: fooder}"></div>
</body>
</html>

fooder.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layuot">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:fragment="fooder">
    <a href="www.baidu.com">百度一下</a>
</div>
</body>
</html>

heand.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layuot">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:fragment="header">
    <h1>首页哦</h1>
    <a href="/users" th:href="@{~/users}">首页</a>
</div>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值