问题描述
我制作了一个原始应用程序,可以在其中手动添加用户(我正在使用SpringBoot,Java,thymleaf和Postgresql。添加用户时,数据已添加到Postgressql,但后备页面不起作用。因此,我看不到用户列表,既不是已经存在的用户列表,还是我创建的用户列表。
控制器:
@Controller
public class JustTesting {
private final UserRepository userRepository;
@Autowired
public JustTesting(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping("/signup")
public String showSignUpForm(AppUser appUser) {
return "add-user";
}
@PostMapping("/adduser")
public String addUser(@Valid AppUser appUser,BindingResult result,Model model) {
if (result.hasErrors()) {
return "add-user";
}
userRepository.save(appUser);
model.addAttribute("users",userRepository.findAll());
return "redirect:/view-user";
}
@GetMapping("/edit/{id}")
public String showUpdateForm(@PathVariable("id") long id,Model model) {
AppUser appUser = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
model.addAttribute("appUser",appUser);
return "update-user";
}
@PostMapping("/update/{id}")
public String updateUser(@PathVariable("id") long id,@Valid AppUser appUser,Model model) {
if (result.hasErrors()) {
appUser.setUserId(id);
return "update-user";
}
userRepository.save(appUser);
model.addAttribute("appUsers",userRepository.findAll());
return "redirect:/view-user";
}
@GetMapping("/delete/{id}")
public String deleteUser(@PathVariable("id") long id,Model model) {
AppUser appUser = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
userRepository.delete(appUser);
model.addAttribute("users",userRepository.findAll());
return "view-user";
}
}
我们拥有Thymeleaf页面:add-user
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<Meta charset="utf-8">
<Meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Add User</title>
<Meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
<link href="/css/shards.min.css" rel="stylesheet"/>
</head>
<body>
<div class="container my-5">
<h2 class="mb-5">New User</h2>
<div class="row">
<div class="col-md-6">
<form action="#" th:action="@{/adduser}" th:object="${appUser}" method="post">
<div class="row">
<div class="form-group col-md-6">
<label for="userId" class="col-form-label">userId</label>
<input type="text" th:field="*{userId}" class="form-control" id="userId" placeholder="userId">
<span th:if="${#fields.hasErrors('userId')}" th:errors="*{userId}" class="text-danger"></span>
</div>
<div class="form-group col-md-6">
<label for="userName" class="col-form-label">userName</label>
<input type="text" th:field="*{userName}" class="form-control" id="userName" placeholder="userName">
<span th:if="${#fields.hasErrors('userName')}" th:errors="*{userName}" class="text-danger"></span>
</div>
<div class="form-group col-md-6">
<label for="encrytedPassword" class="col-form-label">encrytedPassword</label>
<input type="text" th:field="*{encrytedPassword}" class="form-control" id="encrytedPassword" placeholder="encrytedPassword">
<span th:if="${#fields.hasErrors('encrytedPassword')}" th:errors="*{encrytedPassword}" class="text-danger"></span>
</div>
<div class="row">
<div class="col-md-6 mt-5">
<input type="submit" class="btn btn-primary" value="Add User">
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
视图用户:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<Meta charset="utf-8">
<Meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Users</title>
<Meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
<link href="/css/shards.min.css" rel="stylesheet"/></head>
<body>
<div th:switch="${appUsers}" class="container my-5">
<div class="row">
<div class="col-md-6">
<h2 th:case="null">No users yet!</h2>
<div th:case="*">
<h2 class="my-5">Users</h2>
<table class="table table-striped table-responsive-md">
<thead>
<tr>
<th>userId</th>
<th>userName</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="appUser : ${appUsers}">
<td th:text="${appUser.userId}"></td>
<td th:text="${appUser.userName}"></td>
<td><a th:href="@{/edit/{userId}(id=${appUser.userId})}" class="btn btn-primary"><i class="fas fa-appUser-edit ml-2"></i></a></td>
<td><a th:href="@{/delete/{userId}(id=${appUser.userId})}" class="btn btn-primary"><i class="fas fa-appUser-times ml-2"></i></a></td>
</tr>
</tbody>
</table>
</div>
<p class="my-5"><a href="/signup" class="btn btn-primary"><i class="fas fa-appUser-plus ml-2"></i></a></p>
</div>
</div>
</div>
</body>
</html>
我还有一个名为AppUser和存储库的模型页面。我是一个初学者,我只是设法进行了有效的登录,并且我想开始工作是,如果您以管理员身份登录,则可以添加也可以登录的用户。
也许有人可以帮助:)丹妮尔
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)