Spring MVC: Where to place validation and how to validation entity references?

I suppose your User is as follows public class User implements Serializable { private String userName; private String email; private Occupation occupation; private Country country; } Usually, a reference to another Entity when displaying your form is by using some A occupaation Other occupaation Another occupaation Spring uses data-dinding to bind your select HTML element to your property. The method used is called initBind public class UserController extends BaseCommandController { private OccupationRepository occupationRepository; private CountryRepository countryRepository; // getter's and setter's (retrieved by Dependency Injection supported by Spring) public UserController() { setCommandClass(User. Class); setValidator(new UserValidator()); } public void initBind(HttpServletRequest request, ServletRequestDataBinder binder) { binder.

RegisterCustomPropertyEditor(Occupation. Class, new PropertyEditorSupport() { public void setAsText(String occupationId) { // StringUtils belongs to jakarta-commons lang if(StringUtils. IsBlank(occupationId)) { setValue(null); return; } setValue(occupationRepository.

GetById(Integer. ValueOf(occupationId))); } public String getAsText() { if(getValue() == null) return; return String. ValueOf(((Occupation) getValue()).getId()); } }); // Same approach when binding Country } } Notice you can replace initBind method by assigning a WebBindingInitializer object public class UserBindingInitializer implements WebBindingInitializer { private OccupationRepository occupationRepository; private CountryRepository countryRepository; // getter's and setter's (retrieved by Dependency Injection supported by Spring) public void initBinder(WebDataBinder binder, WebRequest request) { binder.

RegisterCustomPropertyEditor(Occupation. Class, new PropertyEditorSupport() { public void setAsText(String occupationId) { // StringUtils belongs to jakarta-commons lang if(StringUtils. IsBlank(occupationId)) { setValue(null); return; } setValue(occupationRepository.

GetById(Integer. ValueOf(occupationId))); } public String getAsText() { if(getValue() == null) return; return String. ValueOf(((Occupation) getValue()).getId()); } }); } } ... public class UserController extends BaseCommandController { private OccupationRepository occupationRepository; private CountryRepository countryRepository; // getter's and setter's (retrieved by Dependency Injection supported by Spring) public void setUserBindingInitializer(UserBindingInitializer bindingInitializer) { setWebBindingInitializer(bindingInitializer); } And your UserValidator (Notice your Validator does not know anything else about any repository) public class UserValidator implements Validator { public boolean supports(Class clazz) { return clazz.

IsAssignableFrom(User. Class); } public void validate(Object command, Errors errors) { User user = (User) command; if(user.getOccupation() == null) errors. RejectValue("occupation", "errors.

Required", null); if(user.getCountry() == null) errors. RejectValue("country", "errors. Required", null); } }.

Thanks for the reply. Well the reason for the DTO is that the domain (hibernate) object is pretty complex with a lot of relationships, methods, etc. The user example here is for illustration only. That is why I would like to shield the presentation layer from the domain.

In this case I would need to perform the validation described above because I don't want to expose the entities. I had planned on using Validator although that would place the validation logic on the web tier which seems dubious to me. Unless validator should not only make simple field checks?

– arrages Apr 14 '10 at 14:41 @arrages Validator has been designed to perform simple validation. Something like isInteger, isNotBlank, isNotNull, nothing else. When using complex validation, You had better to place it inside your business logic.

Notice I also use, as said by yourself, pretty complex with a lot of relationships, methods, without no problem. – Arthur Ronald F D Garcia Apr 14 '10 at 15:00.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions