Announcement Announcement Module
Collapse
No announcement yet.
Validation Errors Not Showing in JSP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validation Errors Not Showing in JSP

    Hi everyone, I'm new to Spring and I know this question has been asked all over the place, but I can't get any answers to work for me. I'm using Spring 3.1.1 and I'm trying to do simple validation on my form. When my form is submitted, my validation class gets called correctly and populates the BindingResult. So I can see the errors in my controller in the BindingResult variable, but I can't see them when I reload the view.

    Here is my JSP:
    Code:
    <form:form commandName="formControls" method="POST">
    ...
    <div class="field" id="payer_name">
      <label>Number:</label>
      <span class="input">
        <form:input path="payerName" autocomplete="off"/>
        <form:errors path="payerName"/>
      <span class="help-text"></span>
      </span>						
    </div>
    Form Controller:
    Code:
    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("FormControls") FormControls formControls, BindingResult validationResult,   ModelMap model) {
    		
        formValidator.validate(formControls, validationResult);
        if(validationResult.hasErrors()) {
            model.addAttribute(formControls);
    	return "manualEntry";
        }
    Spring Config:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
           
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
        default-autowire="byName">       
                  
     	<!-- Use annotations (@Autowired) for property injection -->
        <context:annotation-config />
        
        <!--
            Spring will find all of our service and DAO components because they
            have @Service, @Repository, and @Controller annotations. We do not
            need to configure them in XML -->
        <context:component-scan base-package="com.cerner.edi.billpay.**" />   
        
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        
        <bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
    	<property name="basename" value="messages" />
        </bean>
        
    </beans>
    So my validation class is working fine because I can see the errors in the validationResult variable. My view is reloading when I return it. I just must have something set up wrong to see the errors in my view. Any help would be great!

  • #2
    The problem is that you have both a BingingResult and ModelMap. You don't need the ModelMap as the model attribute is already part of the Model, also putting @Validatated on the @ModelAttribute will also trigger automatic validation which saves you another line.

    Code:
    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(@Validated @ModelAttribute("FormControls") FormControls formControls, BindingResult validationResult) {
    		
        if(validationResult.hasErrors()) {
    	return "manualEntry";
        }

    Comment


    • #3
      Thank you very much! That was my problem. I guess I had tried different things, just not the right one. Thanks again.

      Comment

      Working...
      X