Thymeleaf + Spring:bean 名称 'User' 的 BindingResult 和普通目标对象都不能用作请求属性
Thymeleaf + Spring: Neither BindingResult nor plain target object for bean name 'User' available as request attribute
当我想转到 myweb/register.html 时出现该错误
有人可以给我建议我做错了什么吗?
在 register.html 中,我突出显示了
${User}、*{firstName}、*{lastName}、*{email} 和 *{password}
Servlet.service() for servlet [dispatcherServlet] in context with path
[] threw exception [Request processing failed; nested exception is
org.thymeleaf.exceptions.TemplateInputException: An error happened
during template parsing (template: "class path resource
[templates/register.html]")] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain
target object for bean name 'User' available as request attribute
控制器:
@RequestMapping(value = { "/register" }, method = RequestMethod.GET)
public ModelAndView register(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("register"); // resources/templates/register.html
return modelAndView;
Register.html:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Registration Form</title>
<!-- link rel="stylesheet" type="text/css" th:href="@{/css/registration.css}" /-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body style="background-color: #ededed;">
<div style="background-color: #337ab7; height: 50px;"></div>
<div class="container-fluid" style="margin-top: 30px;">
<div class="row col-lg-4 col-lg-offset-4" style="margin-top: 40px; background-color: #fff; padding: 20px; border: solid 1px #ddd;">
<form autocomplete="off" action="#" th:action="@{/register}" th:object="${User}" method="POST" class="form-signin" role="form">
<h3 class="form-signin-heading">Registration Form</h3>
<div class="form-group">
<div class="">
<input type="text" th:field="*{firstName}" placeholder="Name" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="">
<input type="text" th:field="*{lastName}" placeholder="Last Name" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="">
<input type="text" th:field="*{email}" placeholder="Email" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="">
<input type="password" th:field="*{password}" placeholder="Password" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="">
<button type="submit" class="btn btn-primary btn-block">Register User</button>
</div>
</div>
<span th:utext="${successMessage}"></span>
</form>
</div>
</div>
</body>
</html>
User.java:
package roster.schedule.user;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import roster.schedule.role.Role;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name = "auth_user")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class User
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "auth_user_id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@Column(name = "technical")
private byte technical;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "auth_user_role", joinColumns = @JoinColumn(name = "auth_user_id"), inverseJoinColumns = @JoinColumn(name = "auth_role_id"))
private Set<Role> roles;
}
您必须向模型添加空的用户对象。
尝试以下操作:
@RequestMapping(value = { "/register" }, method = RequestMethod.GET)
public ModelAndView register(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("User", new User());
modelAndView.setViewName("register"); // resources/templates/register.html
return modelAndView;
当我想转到 myweb/register.html 时出现该错误 有人可以给我建议我做错了什么吗? 在 register.html 中,我突出显示了 ${User}、*{firstName}、*{lastName}、*{email} 和 *{password}
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/register.html]")] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'User' available as request attribute
控制器:
@RequestMapping(value = { "/register" }, method = RequestMethod.GET)
public ModelAndView register(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("register"); // resources/templates/register.html
return modelAndView;
Register.html:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Registration Form</title>
<!-- link rel="stylesheet" type="text/css" th:href="@{/css/registration.css}" /-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body style="background-color: #ededed;">
<div style="background-color: #337ab7; height: 50px;"></div>
<div class="container-fluid" style="margin-top: 30px;">
<div class="row col-lg-4 col-lg-offset-4" style="margin-top: 40px; background-color: #fff; padding: 20px; border: solid 1px #ddd;">
<form autocomplete="off" action="#" th:action="@{/register}" th:object="${User}" method="POST" class="form-signin" role="form">
<h3 class="form-signin-heading">Registration Form</h3>
<div class="form-group">
<div class="">
<input type="text" th:field="*{firstName}" placeholder="Name" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="">
<input type="text" th:field="*{lastName}" placeholder="Last Name" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="">
<input type="text" th:field="*{email}" placeholder="Email" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="">
<input type="password" th:field="*{password}" placeholder="Password" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="">
<button type="submit" class="btn btn-primary btn-block">Register User</button>
</div>
</div>
<span th:utext="${successMessage}"></span>
</form>
</div>
</div>
</body>
</html>
User.java:
package roster.schedule.user;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import roster.schedule.role.Role;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name = "auth_user")
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class User
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "auth_user_id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@Column(name = "technical")
private byte technical;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "auth_user_role", joinColumns = @JoinColumn(name = "auth_user_id"), inverseJoinColumns = @JoinColumn(name = "auth_role_id"))
private Set<Role> roles;
}
您必须向模型添加空的用户对象。
尝试以下操作:
@RequestMapping(value = { "/register" }, method = RequestMethod.GET)
public ModelAndView register(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("User", new User());
modelAndView.setViewName("register"); // resources/templates/register.html
return modelAndView;