目录
■其他(@RestController) 没有使用【Thymeleaf】
■相关知识
SpringBoot + MyBatis 之 Hello World_sun0322-优快云博客
SpringBoot + Thymeleaf 之 HelloWorld_sun0322-优快云博客
■代码
UserController2
package com.sxz.test.one.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sxz.test.one.entity.User;
import com.sxz.test.one.service.UserService;
// @RestController // 它是无效的。不支持模板引擎
@Controller
@RequestMapping("/user")
public class UserController2 {
@Autowired
UserService userService;
@RequestMapping("/findAll2")
public String findAll(Model model){
List<User> userList = userService.findAll();
model.addAttribute("userList",userList);
return "helloThymeleafMyBatis.html";
}
}
helloThymeleafMyBatis.html
<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<meta charset="UTF-8">
<title>helloThymeleafMyBatis页面</title>
<style type="text/css">
table { background:#DCDFE6; width: 100%; }
table th { background:#DDEBF7; }
table tbody td { background:#FFFFFF; }
</style>
</head>
<body bgcolor="#FFFF99">
<table border="0" cellspacing="1" cellpadding="0">
<tr>
<th>id</th>
<th>password</th>
<th>权限</th>
</tr>
<tbody th:unless="${userList.isEmpty()}" >
<tr th:each="userInfo : ${userList}">
<td th:text="${userInfo.luId}"></td>
<td th:text="${userInfo.luPass}"></td>
<td th:text="${userInfo.luPermission}"></td>
</tr>
</tbody>
</table>
</body>
</html>
■效果
https://10.10.10.194/user/findAll2
■DB数据
---
■其他(@RestController) 没有使用【Thymeleaf】
---
package com.sxz.test.one.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sxz.test.one.entity.User;
import com.sxz.test.one.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
}
---
https://10.10.10.194/user/findAll
@RestController 返回 JSON 数据
[{"luId":"admin","luPass":"admin","luPermission":"3"},{"luId":"test001","luPass":"123456","luPermission":"1"},{"luId":"test002","luPass":"123456","luPermission":"1"},{"luId":"test003","luPass":"123456","luPermission":"1"},{"luId":"test005","luPass":"123456","luPermission":"1"},{"luId":"test006","luPass":"123456","luPermission":"1"},{"luId":"123456","luPass":"123456","luPermission":"1"}]
---