Announcement Announcement Module
Collapse
No announcement yet.
Error when validating form data from an annotated controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error when validating form data from an annotated controller

    I have looked all over the Internet for a solution to this problem, a forum post is normally my last recourse. I'm somewhat new to spring mvc so bear with me. I'm working on a spring mvc project which is highly annotated. I've followed several tutorials to get several key pieces up and running successfully. I've hit a wall getting my form validation to actually print out the message that is located in my resource bundle that I'm successfully using everywhere else in the application.

    I have a jsp form that uses the <spring:errors /> tag. This form posts to an annotated controller that then manually calls the validator that was autowired in and declared in the servlet config.xml. When the controlling function determines there are errors and sends to a different view, I get the following error: org.springframework.context.NoSuchMessageException : No message found under code 'error.required.email' for locale 'en_US'.

    this line appears in my messages.properties file, error.required.email=email is required

    I use the messages.properties successfully everywhere else in the app. I have even made copies to reflect the en_US locale, but it doesn't change anything. I'm assuming that something is not aware of the ResourceBundleMessageSource bean, but I have also defined that in both the applicationContext.xml and servlet context.xml.

    I'm assuming I've overlooked something very obvious because this has to be commonly used, and I haven't been able to find any information on it. If anyone has a working example of what I'm trying to do, or can explain why messages set in the errors object are not getting resolved in the properties files that would be helpful.

    Here is some of the code that is producing the errors, any help is greatly appreciated:

    servlet.xml
    HTML Code:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
       <property name="basename" value="messages" />
    </bean>
    
    <bean id="userValidator" class="com.liger.ui.validators.UserValidator" />
    createuser.jsp which is a tile:
    HTML Code:
    <form:form modelAttribute="user" action="" method="post">
    	<spring:message code="user.account.email" text="email" />: <form:input id="email" path="email"/><form:errors path="email" cssStyle="font-size: 8px; color: red;" /><br/>
    	<spring:message code="user.account.password" text="password" />: <form:password id="password" path="password"/><form:errors path="password" cssStyle="font-size: 8px; color: red;" /><br/>
    	<spring:message code="user.account.verifypassword" text="password" />: <form:password id="verifyPassword" path="verifyPassword"/><br/>
    	<spring:message code="user.account.firstname" text="first name" />: <form:input id="firstName" path="firstName"/><br/>
    	<spring:message code="user.account.lastname" text="last name" />: <form:input id="lastName" path="lastName"/><br/>
    	<input type="submit" value="create" />
    </form:form>
    The controller which uses class name and url mappings:
    @Controller
    Code:
    public class UserController {
    
    	@Autowired private UserService userService;
    	
    	@Autowired(required = false)
    	@Qualifier(value = "userValidator")
    	private Validator validator;
    	
    	public UserController() {
    		
    	}
    	
    	@RequestMapping(value = "/user/create.html", method = RequestMethod.GET)
    	public String createUserView(User user) {
    		
    		return "user/create";
    	}
    
    	@RequestMapping(value = "/user/create.html", method = RequestMethod.POST)
    	public ModelAndView createUserAction(@ModelAttribute(value = "user") User user, BindingResult bindingResult, SessionStatus sessionStatus) {
    		validator.validate(user, bindingResult);
    		
    		if(bindingResult.hasErrors()) {
    			return new ModelAndView("user/create");
    		}
    		else {
    			user = this.userService.saveUser(user);
    			ModelMap mm = new ModelMap();
    			mm.addAttribute("user", user.getId());
    			ModelAndView modelAndView = new ModelAndView("redirect:view.html", mm);
    			return modelAndView;
    		}
    	}
    	
    	@RequestMapping(method = RequestMethod.GET)
    	public ModelAndView view(@RequestParam("user") long userId) {
    		User user = this.userService.findById(userId);
    		
    		Set<Product> products = user.getProducts();
    		
    		ModelAndView modelAndView = new ModelAndView("user/intro", "user", user);
    		return modelAndView;
    	}
    	}

    The validator class:
    Code:
    public class UserValidator implements Validator {
    
    	@Override
    	public boolean supports(Class clazz) {
    		return User.class.isAssignableFrom(clazz);
    	}
    
    	@Override
    	public void validate(Object target, Errors errors) {
    		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "error.required.email");
    		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "error.required.password");
    		
    		if(!((User)target).arePasswordEqual()) {
    			errors.rejectValue("password", "1", "error.verify.password");
    		}
    	}
    
    }

  • #2
    As I left work to go buy a doughnut, it donned on me as to what it could be, and I figured it out.

    The Resource Bundle bean had to have the fully qualified path in order for the validation to work. For example, I had the following in:

    HTML Code:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basename" value="messages" />
    	</bean>
    With the bean setup like this, jsp's could easily resolve the messages but the validation couldn't


    HTML Code:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basename" value="com.project.ui.bundle.messages" />
    	</bean>
    As soon as I put in the full classpath the validator began working. This does seem like a bug since the validator reuired the full classpath, but the normal message resolving in jsps didn't

    Comment

    Working...
    X