Spring @ExceptionHandler,如何保存模型属性

时间:2016-04-15 11:24:55

标签: spring forms spring-mvc exception model

似乎@ExceptionHandler清除由抛出异常的请求处理程序填充的模型。请考虑以下情形:

@Controller
public class GreetController {

    @RequestMapping(value = "form", method = RequestMethod.GET)
    public String showForm(@ModelAttribute("userInfoFormObject") UserInfoForm form) {
        return "form";
    }

    @RequestMapping(value = "processform", method = RequestMethod.POST)
    public String processForm(@Valid @ModelAttribute("userInfoFormObject")   UserInfoForm form,
            BindingResult errors)
            throws RegisterFormException {

        if (errors.hasErrors())
            throw new RegisterFormException();

        return "greet";
    }

    @ExceptionHandler(RegisterFormException.class)
    public String registerFormException() {
        return "form";
    }

}

用户将无效数据输入到寄存器表单中,RegisterFormException被抛出,exception handler将用户带回注册表单。 Spring jstl标记库期望UserInfoForm对象为model attribute。但是,exception handler会创建新的empty model。有没有办法在populated model之间保留exception handler,或者我是唯一选择在错误的情况下在请求处理程序中返回表单视图名称?示例解决方案是否被视为反模式?

0 个答案:

没有答案