前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot 2.x 学习笔记(3):Web开发之Thymeleaf

SpringBoot 2.x 学习笔记(3):Web开发之Thymeleaf

作者头像
程裕强
发布2022-05-06 20:46:39
1900
发布2022-05-06 20:46:39
举报

1、代码结构

这里写图片描述
这里写图片描述

###2、Bean类

代码语言:javascript
复制
package cn.hadron.springboot.bean;

import java.io.Serializable;

public class UserBean implements Serializable{
    private Integer id;
    private String username;
    private String password;
    private String birthday;

    public UserBean(){}
    public UserBean(String username, String password, String birthday) {
        this.username = username;
        this.password = password;
        this.birthday = birthday;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
}

3、Controller

代码语言:javascript
复制
package cn.hadron.springboot.controller;

import cn.hadron.springboot.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {
    @RequestMapping("/")
    public String index(Model model){
        System.out.println("LoginController.index");
        //根据Thymeleaf默认模版,映射到resources/templates/index.html
        return "index";
    }

    @RequestMapping("/test")
    public String test(HttpServletRequest request, HttpSession  session){
        System.out.println("IndexController.test");
        request.setAttribute("msg","Hello,World!");
        session.setAttribute("tip","Hello,Java!");
        request.getServletContext().setAttribute("name","Hello,SpringBoot 2.x!");
        return "test";
    }
    @RequestMapping("/testIf")
    public String testIf(WebRequest web){
        System.out.println("IndexController.testIf");
        web.setAttribute("username","Hadron Cheng",WebRequest.SCOPE_REQUEST);
        web.setAttribute("password","123456",WebRequest.SCOPE_REQUEST);
        return "testIf";
    }

    @RequestMapping("/testEach")
    public String testEach(WebRequest web){
        System.out.println("IndexController.testEach");
        List<UserBean> list=new ArrayList<>();
        list.add(new UserBean("abc","123","1990-09-10"));
        list.add(new UserBean("test","123","1991-09-10"));
        list.add(new UserBean("hadron","123","1992-09-10"));
        web.setAttribute("users",list,WebRequest.SCOPE_REQUEST);
        return "list";
    }
}

4、页面

(1)index.html

代码语言:javascript
复制
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="main">
    <h4>Thymeleaf</h4>
    <a th:href="@{test?username=hadron}">测试数据访问</a><br>
    <a th:href="@{testIf}">if条件判断</a><br>
    <a th:href="@{testEach}">循环测试</a><br>
</div>
</body>
</html>

提示:

  • thymeleaf提供的相对上下文动态路径
  • thymeleaf的th:src或者th:href属性改变标签的链接路径
这里写图片描述
这里写图片描述

(2)test.html

代码语言:javascript
复制
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="main">
    param:<span th:text="${param.username}">动态模版</span><br>
    request:<span th:text="${msg}">动态模版</span><br>
    session:<span th:text="${session.tip}">动态模版</span><br>
    application:<span th:text="${application.name}">动态模版</span>
</div>
</body>
</html>
这里写图片描述
这里写图片描述

(3)testIf.html

代码语言:javascript
复制
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="main">
    <span th:if="${username!=null}">username字段非空</span><br>
    <span th:if="${password==null}">age字段空</span><br>
    <span th:unless="${role!=null}">与th:if相反</span>
</div>
</body>
</html>

(4)list.html

代码语言:javascript
复制
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="main">
    <table border="1">
        <tr>
            <th>序号</th>
            <th>账号</th>
            <th>口令</th>
            <th>出生日期</th>
        </tr>
        <tr th:each="user,stat:${users}">
            <td th:text="${stat.index}">状态变量</td>
            <td th:text="${user.username}"></td>
            <td th:text="${user.password}"></td>
            <td th:text="${user.birthday}"></td>
        </tr>
    </table>
</div>
</body>
</html>
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-07-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、代码结构
  • 3、Controller
  • 4、页面
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档