我正在撰寫一個 Spring Boot 應用程式,每當客戶端請求獲取“registration.html”模板時,服務器應該回傳它
我首先將 GetMapping 放在我的 RegistrationController 中,它起作用了,也就是說,當我訪問 localhost:8080/registration 時,它確實回傳了那個頁面
@Controller
@RequestMapping("/registration")
public class UserRegistrationController {
private UserService userService;
public UserRegistrationController(UserService userService) {
super();
this.userService = userService;
}
@ModelAttribute("user")
public UserRegistrationDto userRegistrationDto() {
return new UserRegistrationDto();
}
@GetMapping
public String registration() {
return "registration";
}
@PostMapping
public String registerUserAccount(@ModelAttribute("user") UserRegistrationDto registrationDto) {
userService.save(registrationDto);
return "redirect:/registration?success";
}
}
然后我決定創建一個模板控制器,并將所有模板的 GetMapping 放在那里。但是當我再次嘗試訪問 localhost:8080/registration 時,它給了我一個錯誤(型別=內部服務器錯誤,狀態=500)
@Controller
@RequestMapping
public class TemplateController {
@GetMapping("/")
public String home() {
return "index";
}
@GetMapping("/registration")
public String registration() {
return "registration";
}
}
所以我的問題是,我把GetMapping放在哪里重要嗎?還是我需要修復一些配置,以便應用知道在哪里可以找到 GetMapping?但問題是,這個 TemplateController 中的 home() 方法確實成功回傳了我 /index,所以我認為它與這個類無關
The summary of the log file is:
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor
I think this line below might be the issue
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
My registration.html is as below, however the "user" object is only used for the POST request, not GET request for returning the page
<html lang="en" dir="ltr" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form th:action="@{/registration}" method="post" th:object="${user}">
<div class="form-group">
<label class="control-label" for="name"> Name </label>
<input id="name" class="form-control" type="text" th:field="*{name}"
required autofocus="autofocus" />
</div>
<div class="form-group">
<label class="control-label" for="email"> Email </label>
<input id="email" class="form-control" type="text" th:field="*{email}" required
autofocus="autofocus" />
</div>
<div class="form-group">
<label class="control-label" for="password"> Password </label>
<input id="password" class="form-control" type="password"
th:field="*{password}" required autofocus="autofocus" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Register</button>
<span>Already registered? <a href="/" th:href="@{/login}">Login
here</a></span>
</div>
</form>
</body>
uj5u.com熱心網友回復:
謝謝@Ahmet 的解決方案。TemplateController 下的注冊方法現在正在作業,并且在我將模型添加到方法中后,正在回傳 registration.html 模板。完整的代碼是:
@Controller
@RequestMapping("/")
public class TemplateController {
...
@GetMapping("/registration")
public String registration(Model model) {
User user = new User();
model.addAttribute(user);
return "registration";
}
// below is the old version, which didn't work.
// @GetMapping("/registration")
// public String registration() {
// return "registration";
// }
...
}
而且這個方法只存在于 TemplateController 類中,而不存在于 UserRegistrationController 類中(如果我把方法放在這里,它一直在作業,如問題中所述)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/438720.html
標籤:spring spring-mvc thymeleaf backend
上一篇:將時間序列中的間隔設定為1秒
