java - 尝试执行表单验证时出错
问题描述
我正在为网站制作注册页面,但是当我转到该页面时出现错误:执行处理器“org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor”时出错
控制器
@RequestMapping(value = { "/signup" }, method = RequestMethod.POST)
public ModelAndView createUser(@Valid AppUser appUser, BindingResult bindingResult) {
ModelAndView model = new ModelAndView();
AppUser appUserExists = appUserService.findByEmail(appUser.getEmail());
if (appUserExists != null) {
bindingResult.rejectValue("email", "error.user", "This email already exists!");
}
if (bindingResult.hasErrors()) {
model.setViewName("user/signup");
} else {
appUserService.saveUser(appUser);
model.addObject("msg", "User has been registered succesfully!");
model.addObject("appuser", new AppUser());
model.setViewName("user/login");
}
return model;
}
形式
<form class="form-horizontal" role="form" th:action="@{/signup}" th:object="${appUser}" method="post" style="border: 1px solid #ccc">
<label for="name">First Name</label>
<input type="text" th:field="*{firstname}" class="form-control" id="firstname" placeholder="First Name" required autofocus />
<label for="name">Last Name</label>
<input type="text" th:field="*{lastname}" class="form-control" id="lastname" placeholder="Last name" required autofocus />
<label for="email"><b>Email</b></label>
<input type="email" th:field="*{email}" class="form-control" id="email" placeholder="email@domain.com" required autofocus />
<label for="psw"><b>Password</b></label>
<input type="password" th:field="*{password}" class="form-control" id="password" placeholder="Password" required />
</form>
应用用户类
@Entity
@Table(name = "user")
public class AppUser {
@Id
@Column(name = "id_user", length = 10, nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "firstname", length = 30, nullable = false, unique = true)
private String firstName;
@Column(name = "lastname", length = 30, nullable = false, unique = true)
private String lastName;
@Column(name = "email", length = 30, nullable = false, unique = true)
private String email;
@Column(name = "password", length = 500, nullable = false, unique = false)
private String password;
@Column(name = "active", nullable = false)
private int active;
public AppUser() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
....
}
据我所知,这里没有任何问题,所以我被卡住了。有任何想法吗?
解决方案
控制器中的 appuser 变量应该是 appUser。并且模板中的姓和名必须是类中的姓和名。
推荐阅读
- appium - Appium / Python的云测试服务?
- matlab - Matlab:标准化后直方图下降?
- google-cloud-platform - 使用服务帐户登录 Google 平台的 REST API
- machine-learning - Word2Vec 如何确保反义词在向量空间中相距甚远
- c++ - 使用 void 函数检查 gtest 中的错误
- python - 对象列表的自定义排序
- javascript - 如果 else 语句在 jquery 中不起作用
- azure-devops - 根据 Azure Devops 中的文件路径类型动态填充选项列表?
- android - RecyclerView 只滚动 1 个项目
- fullcalendar - 全日历编辑