当我们想要将模板返回给客户端时,GetMapping (Spring) 的位置重要吗?
我正在编写一个 Spring Boot 应用程序,每当客户端请求获取“registration.html”模板时,服务器应该返回它
我首先将 GetMapping 放入我的 RegistrationController 中,并且它起作用了,也就是说,当我访问 localhost:8080/ 时注册后,它确实返回了该页面
@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 ,所以我认为这与这个类无关
日志文件的摘要是:
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor
我认为下面这一行可能是问题
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
我的注册。 html 如下,但是“user”对象仅用于 POST 请求,而不用于返回页面的 GET 请求
<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>
I was writing a Spring Boot application, and whenever the client requested to get "registration.html" template, the server should return it
I first put the GetMapping in my RegistrationController, and it worked, that is, when I access localhost:8080/registration, it does return me that page
@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";
}
}
Then I decided to create a template controller, and put all GetMapping for templates there. But then when I try to access localhost:8080/registration again, it gives me an error (type=Internal Server Error, status=500)
@Controller
@RequestMapping
public class TemplateController {
@GetMapping("/")
public String home() {
return "index";
}
@GetMapping("/registration")
public String registration() {
return "registration";
}
}
So my question is, does it matter where I put the GetMapping? Or is there are some configurations I needed to fix, so the app knows where to find the GetMapping? But the thing is, the home() method in this TemplateController does return me /index successfully, so I thought it's not to do with this class
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢@Ahmet 提供的解决方案。 TemplateController 下的注册方法现在正在运行,并且在我将模型添加到方法中后,正在返回 Registration.html 模板。完整的代码是:
并且此方法仅存在于 TemplateController 类中,而不存在于 UserRegistrationController 类中(如果我将该方法放在这里,它一直在工作,如问题中所述)
Thank you @Ahmet for the solution. The registration method under TemplateController is working now, and registration.html template is being returned, after I add a Model into the method. The complete code is:
And this method is only present in TemplateController class, not in UserRegistrationController class (if I put the method here, it has always been working, as described in the question)