views:

17

answers:

0

Hi all,

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, Object>();
        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);
    }
}