1、创建项目
2、修改pom文件,添加坐标
3、编写视图
注意:springBoot要求模板形式的视图层技术的文件必须要放到src/main/resources目录下必须要一个名称为templates
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" align="center" width="50%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<#list list as user>
<tr>
<td>${user.userId}</td>
<td>${user.userName}</td>
<td>${user.age}</td>
</tr>
</#list>
</table>
</body>
</html>
4、创建Controller
package com.lxp.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lxp.pojo.User;
@Controller
public class ShowUserController {
@RequestMapping("/showUser")
public String showUser(Model model) {
List<User> list = new ArrayList<User>();
list.add(new User(1, "张三", 20));
list.add(new User(2, "李四", 22));
list.add(new User(3, "王五", 24));
model.addAttribute("list", list);
return "userList";
}
}
pojo类
package com.lxp.pojo;
public class User {
private Integer userId;
private String userName;
private Integer age;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(int id, String name, int age) {
this.userId = id;
this.userName = name;
this.age = age;
}
}
6,创建启动类
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}