hien thi danh sach user ra page

This commit is contained in:
2026-05-26 19:26:07 +07:00
parent 2b7b190103
commit 7350e3ebb2
5 changed files with 72 additions and 3 deletions

View File

@@ -1,7 +1,10 @@
package com.example.server_site_api.controllers;
import com.example.server_site_api.models.Users;
import com.example.server_site_api.servies.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -11,8 +14,14 @@ import java.util.List;
@Controller
@RequestMapping("users")
public class UserController {
@Autowired
private UserService us;
@GetMapping
private String loadUserManagerPage() {
private String loadUserManagerPage(Model model) {
model.addAttribute("users", us.getAllUsers());
System.out.println(us.getAllUsers());
return "UserManager";
}
}

View File

@@ -69,4 +69,15 @@ public class Users {
public Long getId() {
return id;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='[PROTECTED]'" + // Better not to print actual passwords in logs
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}

View File

@@ -0,0 +1,9 @@
package com.example.server_site_api.repositories;
import com.example.server_site_api.models.Users;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.ResponseBody;
@ResponseBody
public interface UserRepository extends JpaRepository<Users, Long> {
}

View File

@@ -0,0 +1,18 @@
package com.example.server_site_api.servies;
import com.example.server_site_api.models.Users;
import com.example.server_site_api.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserRepository ur;
public List<Users> getAllUsers() {
return ur.findAll();
}
}

View File

@@ -1,10 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>User Manager</title>
</head>
<body>
<h1>Hello</h1>
<h1>User Manager</h1>
<table>
<thead>
<tr>
<th>STT</th>
<th>ID</th>
<th>username</th>
<th>password</th>
<th>gender</th>
<th>age</th>
</tr>
</thead>
<tbody>
<tr th:each="u: ${users}">
<td>1</td>
<td th:text="${u.getId()}"></td>
<td th:text="${u.getUsername()}"></td>
<td th:text="${u.getPassword()}"></td>
<td th:text="${u.getGender().equals('male')} ? 'nam' : 'nu'"></td>
<td th:text="${u.getAge()}"></td>
</tr>
</tbody>
</table>
</body>
</html>