当我们想要 return 客户端模板时,GetMapping (Spring) 的位置是否重要?
Does the place of GetMapping (Spring) matter when we want to return a template to client?
我正在编写一个 Spring 引导应用程序,每当客户端请求获取“registration.html”模板时,服务器应该 return 它
我首先将 GetMapping 放在我的 RegistrationController 中,它起作用了,也就是说,当我访问 localhost:8080/registration 时,它 return 我那个页面
@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 时,它给了我一个错误 (type=Internal Server Error, status=500)
@Controller
@RequestMapping
public class TemplateController {
@GetMapping("/")
public String home() {
return "index";
}
@GetMapping("/registration")
public String registration() {
return "registration";
}
}
所以我的问题是,我把 GetMapping 放在哪里有关系吗?或者是否有一些我需要修复的配置,以便应用程序知道在哪里可以找到 GetMapping?但问题是,这个 TemplateController 中的 home() 方法成功地 return me /index,所以我认为这与 class
无关
日志文件的摘要是:
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
我的 registration.html 如下所示,但是“用户”对象仅用于 POST 请求,而不用于 return 页面
的 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>
感谢@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";
// }
...
}
而且这个方法只存在于TemplateControllerclass,不存在于UserRegistrationControllerclass(如果我把这个方法放在这里,它一直有效,如问题中所述)
我正在编写一个 Spring 引导应用程序,每当客户端请求获取“registration.html”模板时,服务器应该 return 它
我首先将 GetMapping 放在我的 RegistrationController 中,它起作用了,也就是说,当我访问 localhost:8080/registration 时,它 return 我那个页面
@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 时,它给了我一个错误 (type=Internal Server Error, status=500)
@Controller
@RequestMapping
public class TemplateController {
@GetMapping("/")
public String home() {
return "index";
}
@GetMapping("/registration")
public String registration() {
return "registration";
}
}
所以我的问题是,我把 GetMapping 放在哪里有关系吗?或者是否有一些我需要修复的配置,以便应用程序知道在哪里可以找到 GetMapping?但问题是,这个 TemplateController 中的 home() 方法成功地 return me /index,所以我认为这与 class
无关日志文件的摘要是:
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
我的 registration.html 如下所示,但是“用户”对象仅用于 POST 请求,而不用于 return 页面
的 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>
感谢@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";
// }
...
}
而且这个方法只存在于TemplateControllerclass,不存在于UserRegistrationControllerclass(如果我把这个方法放在这里,它一直有效,如问题中所述)