如何在springmvc和eleaf中进行验证和异常

g2ieeal7  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(271)

我使用的是spring mvc和thymeleaf,我读过以下内容:https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvchttpshttp://spring.io/guides/gs/validating-form-input/
1--我仍然不知道如何在没有 @Valid 注解,比如在这种情况下如何进行验证(没有表单匹配bean)

@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String registrationSubmit(@RequestParam(required = false) String phone,
                                 @RequestParam(required = false) String passwd,
                                 @RequestParam(required = false) String passwdRepeat,
                                 @RequestParam(required = false) String smsCode, BindingResult bindingResult) {
    //validation
    if (phone.isEmpty()) {
        FieldError fieldError = new FieldError("phone","phone","phone error");
        bindingResult.addError(fieldError);
        bindingResult.rejectValue("phone", "error.user", "phone error");
        return "register_form";
    }

    return "";
}

2——以及如何处理异常,如 UserNotFoundException 并以表单形式通知用户,而不是以新视图进行响应。

ugmeyewa

ugmeyewa1#

如果有错误,抛出异常,然后使用@exceptionhandler注解另一个方法,该方法将处理异常并呈现适当的响应。

ubof19bj

ubof19bj2#

试试这个。。。我正在研究更好的解决方案

@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;
    }
}
vltsax25

vltsax253#

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>

注:
表单验证失败时在头中追加公共消息
根据窗体类中设置的消息追加窗体字段错误消息

相关问题