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

1、代码结构

这里写图片描述
###2、Bean类

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

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

<!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

<!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

<!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

<!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>

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值