Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 如何在GET表单和提交POST表单中管理验证?_Spring Mvc_Validation - Fatal编程技术网

Spring mvc 如何在GET表单和提交POST表单中管理验证?

Spring mvc 如何在GET表单和提交POST表单中管理验证?,spring-mvc,validation,Spring Mvc,Validation,环境是Spring3.0,具有新功能Vallidation。 我创建了一个带注释的控制器(ResetUserPasswordController),它管理HTTP.GET上的showForm和HTTP.POST上的submit表单。该函数是电子邮件请求的重置用户密码:用户访问另一个表单之前,我填写的是电子邮件地址和recaptcha控件,如果recaptcha正确,用户将收到一封带有包含参数的链接的邮件。这两个方法(在HTTP.GET和HTTP.POST上)有两个不同的命令bean和不同的参数(

环境是Spring3.0,具有新功能Vallidation。 我创建了一个带注释的控制器(ResetUserPasswordController),它管理HTTP.GET上的showForm和HTTP.POST上的submit表单。该函数是电子邮件请求的重置用户密码:用户访问另一个表单之前,我填写的是电子邮件地址和recaptcha控件,如果recaptcha正确,用户将收到一封带有包含参数的链接的邮件。这两个方法(在HTTP.GET和HTTP.POST上)有两个不同的命令bean和不同的参数(我选择两个不同的bean在两个不同的验证器类中管理验证过程)。您可能在问:为什么要定义两个不同的命令?我定义了以下角色:每个业务和基本(如notnull验证等)验证过程必须由支持特定命令bean的验证程序类管理

我想在GET方法中创建POST管理的命令bean的istance,但在一些测试中,我意识到这可能是不正确的,因为如果验证过程出错,我在输入命令上有所有错误,这与我要在返回的ModelAndView中输入的命令不同

有人提出了一些正确管理此场景的建议吗

@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);
    }
}
@RequestMapping(method=RequestMethod.POST)
public ModelAndView processSubmit(@Valid@modeldattribute(“command”)ResetUserPasswordCommand命令、BindingResult结果、HttpServletRequest请求、HttpServletResponse响应){
getValidator().validate(命令、结果);
if(result.hasErrors()){
//TODO:实现错误页。
返回新的ModelAndView();
}否则{
映射模型=新的HashMap();
试一试{
PasswordChangeRequest PasswordChangeRequest=getUserService().findPasswordChangeRequest(command.getUuid());
getUserService().updateUserPassword(command.getUuid(),command.getPassword());
autoLogin(请求、响应、passwordChangeRequest.getAccount()、command.getPassword());
}捕获(应用程序可移动aex){
返回新模型和视图(“responseKO”,模型);
}
返回新模型和视图(“主页”,模型);
}
}
@RequestMapping(method=RequestMethod.GET)
公共模型和视图设置窗体(@Valid@modeldattribute(“命令”)ResetUserPasswordFormCommand命令,BindingResult){
getFormValidator().validate(命令、结果);
if(result.hasErrors()){
//TODO:实现错误页。
返回新的ModelAndView();
}否则{
映射模型=新的HashMap();
ResetUserPasswordCommand ResetUserPasswordCommand=新的ResetUserPasswordCommand();
resetUserPasswordCommand.setUuid(command.getUuid());
model.put(“命令”,resetUserPasswordCommand);
put(“reCaptchaHTML”,GetReCAPTChasService().GetReCAPTChaoObjectNossl().createRecaptchaHtml(null,null));
返回新模型和视图(“用户/重置用户密码”,模型);
}
}