似乎@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
,或者我是唯一选择在错误的情况下在请求处理程序中返回表单视图名称?示例解决方案是否被视为反模式?