Java 如何在spring mvc中进行验证和异常&;百里香
我正在使用Spring MVC和Thymeleaf,我已经阅读了以下内容: 1--我仍然不知道在没有Java 如何在spring mvc中进行验证和异常&;百里香,java,spring,validation,spring-mvc,thymeleaf,Java,Spring,Validation,Spring Mvc,Thymeleaf,我正在使用Spring MVC和Thymeleaf,我已经阅读了以下内容: 1--我仍然不知道在没有@Valid注释的情况下如何验证表单,比如在这种情况下如何进行验证(没有表单匹配bean) 2--以及如何处理异常,如UserNotFoundException,并以表单形式通知用户,而不是以新视图响应。如果出现错误,则抛出异常,然后使用@ExceptionHandler注释另一个方法,该方法随后将处理异常并呈现适当的响应。尝试此。。。 我正在研究更好的解决方案 @RequestMapping(
@Valid
注释的情况下如何验证表单,比如在这种情况下如何进行验证(没有表单匹配bean)
2--以及如何处理异常,如
UserNotFoundException
,并以表单形式通知用户,而不是以新视图响应。如果出现错误,则抛出异常,然后使用@ExceptionHandler注释另一个方法,该方法随后将处理异常并呈现适当的响应。尝试此。。。
我正在研究更好的解决方案
@RequestMapping(method = POST)
@ResponseBody
public FooDto create(@Valid FooDTO fooDto, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return null; // what to do here?
// how to let the client know something has gone wrong?
} else {
fooDao.insertFoo(fooDto); // What to do if an exception gets thrown here?
// What to send back to the client?
return fooDto;
}
}
1) 您需要为数据创建一个表单类
public class UserForm implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@NotBlank(message = "Message Text")
private String userName;
@NotBlank(message = "Message Text")
private String passoword;
// your other filed with getter and seter
...........................
}
2) 您的控制器方法:
@RequestMapping(value = "/signup", method = RequestMethod.GET)
public String create(Model model) {
model.addAttribute("userForm" , new UserForm());
return "signup";
}
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute UserForm userForm,Errors errors, RedirectAttributes ra, Model model) {
if (errors.hasErrors()) {
return "signup";
}
userService.createUser(userForm);
return "signup";
}
您的HTML代码:
<form role="form" th:action="@{/signup}" method="post" th:object="${userForm}">
<div class="row">
<div class="col-lg-12">
<th:block th:if="${#fields.hasErrors('${userForm.*}')}">
<div th:utext="Common error message">Alert</div>
</th:block>
<div class="form-group input-group" th:classappend="${#fields.hasErrors('userName')}? 'has-error'">
<input type="text" th:field="*{userName}" class="form-control" placeholder="Username" />
<span class="help-block" th:if="${#fields.hasErrors('userName')}" th:errors="*{userName}">Incorrect title</span>
</div>
// your other filed with submit button
</div>
</div>
</form>
警觉的
标题不正确
//你的另一个提交按钮
注:
- 如果表单验证失败,则在标头中附加公共消息
- 表单字段错误消息根据表单类中设置的消息追加
<form role="form" th:action="@{/signup}" method="post" th:object="${userForm}">
<div class="row">
<div class="col-lg-12">
<th:block th:if="${#fields.hasErrors('${userForm.*}')}">
<div th:utext="Common error message">Alert</div>
</th:block>
<div class="form-group input-group" th:classappend="${#fields.hasErrors('userName')}? 'has-error'">
<input type="text" th:field="*{userName}" class="form-control" placeholder="Username" />
<span class="help-block" th:if="${#fields.hasErrors('userName')}" th:errors="*{userName}">Incorrect title</span>
</div>
// your other filed with submit button
</div>
</div>
</form>