开发者

How to manage validation in GET form and submit POST form?

the enviroment is Spring 3.0 with new function Vallidation. I create an annotated controller (ResetUserPasswordController) which manages a showForm on HTTP.GET and the submit form on HTTP.POST. The function is a reset user password requested by email : the user access previously to another form, where i fill is email address and a recaptcha control, if recaptcha is correct, the user receive a mail with a link which contains a paramter. The two methods (on HTTP.GET, and HTTP.POST) have two different command bean have different paramters(i choice two differents beans to manage the validation process in two diffent validators classes). Probably you are questioning : why do you define two differents commands? I have defined the following role : Every bussiness and basic (like notnull validation etc) validation process must be managed by a validator class which supports a specific command bean

I want to create the istance of command bean managed by the POST, in the GET method, but during some tests I realized that this can be not correct beacuse if the validation process goes bad, I have all errors on the input command which is different from which i'm gogin to put in the returned ModelAndView.

Someone has some suggestion to manage correctly this scenario?

@RequestMapping(method = RequestMethod.POST)
public ModelAndView processSubmit(@Valid @ModelAttribute("command") ResetUserPasswordCommand command, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
    getValidator().validate(command, result);

    if (result.hasErrors()) {
        // TODO : implements error page.
        return new ModelAndView();
    } else {
        Map<String, Object> model = new HashMap<String, Object>();

        try {
            PasswordChangeRequest passwordChangeRequest = getUserService().findPasswordChangeRequest(command.getUuid());
            getUserService().updateUserPassword(command.getUuid(), command.getPassword());
            autoLogin(request, response, passwordChangeRequest.getAccount(), command.getPassword());
        } catch (ApplicationThrowable aex) {
            return new ModelAndView("responseKO", model);
        }

        return new ModelAndView("Home", model);
    }

}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView setupForm(@Valid @ModelAttribute("command") ResetUserPasswordFormCommand command, BindingResult result) {
    getFormValidator().validate(command, result);

    if (result.hasErrors()) {
        // TODO : implements error page.
        return new ModelAndView();
    } else {
        Map<String, Object> model = new HashMap<String, O开发者_JS百科bject>();
        ResetUserPasswordCommand resetUserPasswordCommand = new ResetUserPasswordCommand();
        resetUserPasswordCommand.setUuid(command.getUuid());

        model.put("command", resetUserPasswordCommand);
        model.put("reCaptchaHTML", getReCaptchaService().getReCaptchaObjectNoSSL().createRecaptchaHtml(null, null));

        return new ModelAndView("user/ResetUserPassword", model);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜