Thursday, September 16, 2010

reference data is lost when form fails validation in spring3 annotation based controller

Programmer Question

I'm doing the spring 3 annotation based controller thing. Problem is, when it fails validation, the reference data is lost, namely the countryDivisions stuff. I didn't put it in the form because its not user editable data, and the orthodoxy here is that only user-editable data goes in the form. Do I have any other choice?



@Controller
public class MyInfoController {

@Autowired
private MyInfoFormValidator validator;

private void loadReferenceData(ModelMap model) {
model.put("countryDivisions",countryDivisionService.getCountryDivisionOrderedByCode());
}

@ModelAttribute
private MyInfoForm loadMyInfo() {
MyInfoForm form = new MyInfoForm();
//load it up
return form;
}


@RequestMapping(value="/editMyInfo", method = RequestMethod.GET)
public String editMyInfo(ModelMap model ) {
loadReferenceData(model);
return "contactEdit";
}

@RequestMapping(value="/editMyInfo", method = RequestMethod.POST)
public String saveMyInfo(ModelMap model, MyInfoForm form,BindingResult result ) {
validator.validate (form,result);
if (result.hasErrors()) {
model.put("commandName", "myInfoForm");
return "contactEdit";
}
//save some stuff
return "redirect:viewMyInfo";
}

}


Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails