在处理模板时,一般情况都是使用变量表达式 ${...} 来显示变量,还可以使用选定对象表达式 *{...},它也称为星号表达式。
如果在模板中先选定了对象,则需要使用星号表达式。thymeleaf的内置对象#object效果等同于星号表达式。
开发环境:intellij idea 2019.2.2
spring boot版本:2.1.8
新建一个名称为demo的spring boot项目。
1、pom.xml
加入thymeleaf依赖
org.springframework.boot
spring-boot-starter-thymeleaf
2、src/main/java/com/example/demo/user.java
package com.example.demo;
public class user {
integer id;
string name;
public user(integer id, string name) {
this.id = id;
this.name = name;
}
public integer getid() {
return id;
}
public void setid(integer id) {
this.id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
}
3、src/main/java/com/example/demo/testcontroller.java
package com.example.demo;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;
import java.util.arraylist;
import java.util.list;
@controller
public class testcontroller {
@requestmapping("/")
public string test(model model){
list users = queryusers();
model.addattribute("user", users.get(0));
model.addattribute("users", users);
return "test";
}
private list queryusers(){
list users = new arraylist();
users.add(new user(1,"张三"));
users.add(new user(2,"李四"));
users.add(new user(3,"王五"));
return users;
}
}
4、src/main/resources/templates/test.html
titletable { border-collapse:collapse;}
td { border: 1px solid #c1dad7;}
浏览器访问:http://localhost:8080
显示如下截图所示:
希望与广大网友互动??
点此进行留言吧!