All,
I'm playing with changing a functioning Spring 2.0 SimpleFormController to the fancy new style of annotated controller. I'm very closely following the patterns outlined in the examples that ship with 2.5rc2. Everything works fine as long as there are no validation problems. The bounce back doesn't work.
I get this:
"Neither BindingResult nor plain target object for bean name 'command' available as request attribute"
Here's the code:
Code:@Controller @RequestMapping(value = "/user.html") public class UserFormController { public static final String PASSWORD2_PARAM = "password2"; public static final int MIN_PASSWORD_LENGTH = 8; private UserService userService; private DomainObjectValidator domainObjectValidator; @Autowired public void setUserService(UserService userService) { this.userService = userService; } @Autowired public void setDomainObjectValidator(DomainObjectValidator domainObjectValidator) { this.domainObjectValidator = domainObjectValidator; } @RequestMapping(method = RequestMethod.GET) public String setupForm(ModelMap model) { User user; user = new User(); model.addAttribute("command", user); return "user"; } @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String.class, "username", new StringTrimmerEditor(true)); binder.registerCustomEditor(String.class, "password", new StringTrimmerEditor(true)); binder.setAllowedFields(new String[] { "username", "password", }); } @RequestMapping(method = RequestMethod.POST) public String processSubmit(@RequestParam(PASSWORD2_PARAM) String password2, @ModelAttribute("user") User user, BindingResult result, SessionStatus status) { String password1 = user.getPassword(); if(password1 == null || password1.length() < MIN_PASSWORD_LENGTH ) { result.addError(new ObjectError("password2", new String[] {"error.length.password"}, new Object[] {"Password Confirmation"}, "Password length must be at least "+MIN_PASSWORD_LENGTH+ ".")); } if(password2 == null || password2.length() == 0) { result.addError(new ObjectError("password2", new String[] {"error.notnull.password2"}, new Object[] {"Password Confirmation"}, "Must have a value")); } else if(! password2.equals(password1)) { result.addError(new ObjectError("password2", new String[] {"error.mustmatch.password2"}, new Object[] {"Password Confirmation"}, "Must Match")); } domainObjectValidator.validate(user, result); if(result.hasErrors()) { return "user"; } else { userService.save(user); return "redirect:/index.html"; } } }


Reply With Quote