@Test public void modelAndView() throws Exception { BindException bindException = new BindException(new Object(), "target"); bindException.reject("errorCode"); ModelAndView mav = new ModelAndView("viewName"); mav.addObject("attrName", "attrValue"); mav.addObject(BindingResult.MODEL_KEY_PREFIX + "attrName", bindException); this.mvcResult.setMav(mav); this.handler.handle(this.mvcResult); assertValue("ModelAndView", "View name", "viewName"); assertValue("ModelAndView", "View", null); assertValue("ModelAndView", "Attribute", "attrName"); assertValue("ModelAndView", "value", "attrValue"); assertValue("ModelAndView", "errors", bindException.getAllErrors()); }
@Test public void testBindExceptionSerializable() throws Exception { SerializablePerson tb = new SerializablePerson(); tb.setName("myName"); tb.setAge(99); BindException ex = new BindException(tb, "tb"); ex.reject("invalid", "someMessage"); ex.rejectValue("age", "invalidField", "someMessage"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(ex); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); BindException ex2 = (BindException) ois.readObject(); assertTrue(ex2.hasGlobalErrors()); assertEquals("invalid", ex2.getGlobalError().getCode()); assertTrue(ex2.hasFieldErrors("age")); assertEquals("invalidField", ex2.getFieldError("age").getCode()); assertEquals(new Integer(99), ex2.getFieldValue("age")); ex2.rejectValue("name", "invalidField", "someMessage"); assertTrue(ex2.hasFieldErrors("name")); assertEquals("invalidField", ex2.getFieldError("name").getCode()); assertEquals("myName", ex2.getFieldValue("name")); }
errors.reject("repository.error");
public static void reject(BindException errors, String errorCode, Object... args) { errors.reject(errorCode, args, "???" + errorCode + "(10)???"); }
@RequestMapping(method=RequestMethod.GET) public String initForm(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("login") LoginForm loginForm, BindingResult result) { BindException errors = new BindException(result); HttpSession session = request.getSession(false); if (session != null) { AuthenticationException ex = (AuthenticationException) session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION); if (ex != null) { TranslatableMessage message; if (ex instanceof AuthenticationRateException) { message = messageForAuthenticationRateException((AuthenticationRateException) ex); } else { message = messageForAuthenticationFailedException(ex); } errors.reject(message.getKey(), message.getArgs(), ex.getMessage()); } String username = (String)session.getAttribute("username"); if (username != null && !username.isEmpty()) { loginForm.setUsername(username); } } //TODO What if this is a forwarded request? There shan't be a session.... // display errors on the form or next to inputs like so // errors.reject("translation.key", "Fall back text"); // errors.rejectValue("password", "translation.key", "Fall back text"); return "/WEB-INF/jsp/login.jsp"; }