thymeleaf 對象輸出

對象輸出

在實際開發之中經常面對頁面要顯示對象內容的處理操作 模擬一下

1.0 準備一個VO 類

package com.zw.demo9.vo;
import java.io.Serializable;
import java.util.Date;
@SuppressWarnings("serial")


public class User implements Serializable{
    private  Long mid;
    private String name;
    private Integer age;
    private Date   birthday;
    private Double salary;

    public Long getMid() {
        return mid;
    }


    public void setMid(Long mid) {
        this.mid = mid;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public Integer getAge() {
        return age;
    }


    public void setAge(Integer age) {
        this.age = age;
    }


    public Date getBirthday() {
        return birthday;
    }


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


    public Double getSalary() {
        return salary;
    }


    public void setSalary(Double salary) {
        this.salary = salary;
    }
}

2.0 而後編寫一個控制器將對象內容進行屬性傳遞

@RequestMapping(value = "/user_show",method = RequestMethod.GET)
public String userShow(Model model){
    User user=new User();
    user.setMid(102L);
    user.setName("微微");
    user.setAge(18);
    user.setBirthday(new Date());
    user.setSalary(9999969.96);
    model.addAttribute("user",user);
    return  "message/message_user";
}
3.0 編寫一個具體的頁面進行輸出 
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" >
    <title>Title</title>
</head>
<body>
    <p th:text="'用戶編號'+${user.mid}"></p>
    <p th:text="'用戶姓名'+${user.name}"></p>
    <p th:text="'用戶年齡'+${user.age}"></p>
    <p th:text="'用戶工資'+${user.salary}"></p>
    <p th:text="'出生日期'+${user.birthday}"></p>
    <p th:text="'出生日期'+${#dates.format(user.birthday,'yyyy-MM-dd')}"></p>

</body>
</html>
 4.0 按照上面的方式進行輸出 太過麻煩了 下面看看這種方式
<div th:object="${user}">
    <p th:text="'用戶編號'+*{mid}"></p>
    <p th:text="'用戶姓名'+*{name}"></p>
    <p th:text="'用戶年齡'+*{age}"></p>
    <p th:text="'用戶工資'+*{salary}"></p>
    <p th:text="'出生日期'+*{birthday}"></p>
    <p th:text="'出生日期'+*{#dates.format(birthday,'yyyy-MM-dd')}"></p>
</div>

區別 關於 ${屬性} 和 *{屬性} 區別?

          $ 訪問完整信息,而*訪問指定對象中的屬性內容 如果訪問的只是普通的內容兩者沒有任何區別;

githua  代碼如下   https://github.com/zhouwei520/SpringbootDemo/tree/master/demo9

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章