我知道在之前的主题中已经回答了这个问题,但在完成所有解决方案之后,我一直无法解决这个问题。
所以我正在使用spring&用mySQL数据库在hibernate之上的thymeleaf。 我试图通过一个简单的表单将userlogin对象注册到数据库,但系统在运行时立即崩溃并继续显示错误:
BindingResult和bean名称'user'的普通目标对象都不可用作请求属性
注意*我可以使用相同的实现将俱乐部对象添加到数据库中,所以我想可能是与userlogin对象关联的额外变量?
首先发布,请放心!
这是我的控制器代码(后两种方法是userlogin相关的方法):
package com.FYP.Club.controller;
@Controller
public class HomeController {
@Autowired
UserLoginRepository userRepository;
@Autowired
ClubRepository clubRepository;
@RequestMapping(value="/registerclub", method=RequestMethod.GET)
public String index(Club club) {
return "clubindex";
}
@RequestMapping(value = "/club", method = RequestMethod.POST)
public String addNewPost(@Valid Club club, Model model) {
clubRepository.save(club);
model.addAttribute("clubName", club.getClubName());
return "clubresult";
}
@RequestMapping(value="/register", method=RequestMethod.GET)
public String index(UserLogin user) {
return "index";
}
@RequestMapping(value = "/", method = RequestMethod.POST)
public String addNewPost(@Valid UserLogin user, Model model) {
user.setUserStatus(true);
model.addAttribute("email", user.getEmail());
return "result";
}
}
以下是index.html页面,其中包含用户表单:
<h3>Register</h3>
<form action="#" th:action="@{/}" th:object="${user}" method="post">
<table>
<tr>
<td>First name:</td>
<td><input type="text" th:field="*{firstName}" /></td>
</tr>
<tr>
<td>Last name:</td>
<td><input type="text" th:field="*{lastName}" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="number" th:field="*{phone}" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" th:field="*{email}" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" th:field="*{address}" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" th:field="*{password}" /></td>
</tr>
<tr>
<td>UserType:</td>
<td><input type="text" th:field="*{userType}" /></td>
</tr>
<tr>
<td><button type="submit">Submit</button></td>
</tr>
</table>
</form>
</td>
以下是userlogin模型:
@Entity
public class UserLogin {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
private Long phone;
private String email;
private String address;
private String password;
private Boolean userStatus;
private String userType;
public UserLogin()
{
}
public UserLogin(Long id, String firstName, String lastName, Long phone,
String email, String address, String password, Boolean userStatus,
String userType) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.phone = phone;
this.email = email;
this.address = address;
this.password = password;
this.userStatus = userStatus;
this.userType = userType;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Long getPhone() {
return phone;
}
public void setPhone(Long phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Boolean getUserStatus() {
return userStatus;
}
public void setUserStatus(Boolean userStatus) {
this.userStatus = userStatus;
}
}
答案 0 :(得分:1)
您需要在使用之前添加UserLogin对象,如下所示。
@RequestMapping(value="/register", method=RequestMethod.GET)
public String index(Model model) {
model.addAttribute("user", new UserLogin()); //add model to view
return "index";
}
如果您正在使用@Valid Annotation,则需要捕获BindingResult变量中的错误,类似这样的
@RequestMapping(value = "/", method = RequestMethod.POST)
public String addNewPost(@Valid UserLogin user, Model model,
BindingResult errors) {
user.setUserStatus(true);
model.addAttribute("email", user.getEmail());
return "result";
}