Announcement Announcement Module
Collapse
No announcement yet.
Validation errors not displaying in JSP?? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validation errors not displaying in JSP??

    Hi there

    I am having a problem displaying error messages in my JSP that I've set in my Validator class. I've trawled forums, spring sites and found examples which seem to match what I'm doing but have yet to find the problem. I'm not seeing any errors thrown on the server and there are no parsing errors happening, the error messages I've set just arent displayed.

    I've set up a resourcebundle but I have tried just using rejectValue with the default error message parameter and that hasnt displayed so I've not gone onto the next step of trying to use the resourceBundle.

    Any ideas would be greatly appreciated...I'm completely stumped!!

    controllers-servlet.XML
    Code:
    <bean id="portStatusController"  class="<package>.controller.PortStatusController" >
           <property name="validators">			
               <list>   
                   <ref bean="portStatusValidator"/>
               </list>                                
           </property>		
    </bean>		
    <bean id="portStatusValidator" class="<package>.validation.PortStatusValidator" />
    my validator class:

    Code:
    public class PortStatusValidator implements Validator {
    	
       public boolean supports(Class clazz) {					return CommonSearchCriteria.class.isAssignableFrom(clazz);
        }	
      	
    	public void validate(Object target, Errors errors) {
    	   System.out.println("*IN PortStatusValidator.validate********* ");
                   errors.reject("msisdn","the error message");
                   //errors.rejectValue("msisdn","test.error.message",null, "the error message");		  
                  //ValidationUtils.rejectIfEmptyOrWhitespace(errors, "msisdn", "required", "Field is required.");
    	     	     
    	      List errs=errors.getAllErrors();
    	      int count=0;
    	      while (!errs.isEmpty()&& count < 5){
    	    	  Iterator iter=errs.iterator();
    	    	  System.out.println("***********error: " + iter.next());
    	    	  count++;
    	      }	     
    
    	   }
    }
    And my JSP:

    Code:
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    
    <form:errors path="*" />
    			
    <form:errors path="msisdn"/>
    			
    <spring:bind path="portStatus.msisdn">         			
             <c:if test="${status.error}">
    	<p>These were your errors:</p>
                 <ul>
    	<c:forEach var="errorMessage" items="${status.errorMessages}">
    	     <li>${errorMessage}</li>
    	</c:forEach>
    	</ul>
             </c:if>
    </spring:bind>

  • #2
    To simplify it remove everything from your validator except something like:

    Code:
    errors.rejectValue("fieldNameIsFoo", "msgFromMessage.properties", "Please Fill in a value");

    then in your jsp:

    Code:
    <form:errors path="fieldNameIsFoo" />

    Comment


    • #3
      Also, I think you could really replace "msgFromMessage.properties" with "foo!" for your test.

      Comment


      • #4
        Code:
        errors.reject("msisdn","the error message");
        Will register a global error and not a field error.

        Code:
        errors.rejectValue("msisdn", "required", "the error message");
        Will register a field error.

        Code:
        <form:errors path="*" />
        			
        <form:errors path="msisdn"/>
        The form tags only work INSIDE a form:form element not standalone.

        Code:
        <form:form>
           <form:errors path="msisdn"/>
        <form:form>
        Should work in combination with registring a FIELD error instead of a global error.

        Code:
        <spring:bind path="portStatus.msisdn">         			
                 <c:if test="${status.error}">
        	<p>These were your errors:</p>
                     <ul>
        	<c:forEach var="errorMessage" items="${status.errorMessages}">
        	     <li>${errorMessage}</li>
        	</c:forEach>
        	</ul>
                 </c:if>
        </spring:bind>
        Doesn't work in your example because your register a GLOBAL error and not a FIELD error.

        Comment


        • #5
          Hey

          Thank you for your replies. I was confused about the field vs global errors alright but I had tried the different combinations with rejectValue.

          My <form:errors> tags are within <form:form> tags, I just didnt want to post the whole page here.

          I have tried the following in my Validator:

          Code:
           errors.rejectValue("msisdn", "required", "Please Fill in a value");
          and this in my JSP to try the various ways to print out the errors, but still no errors displayed:

          Code:
          <form:form name="myform" action="portStatus.htm" method="post" commandName="portStatus">
          
            <form:errors path="*" />
          
            <form:errors path="msisdn"/>
          			
            <spring:bind path="portStatus.msisdn">
               <c:if test="${status.error}">
                    <p>These were your errors:</p>
                    <c:forEach var="errorMessage" items="${status.errorMessages}">
                         <li>${errorMessage}</li>
                     </c:forEach>
               </c:if>
            </spring:bind> 
          <form:form>
          I've used a System.out in my Validator to print out some of the errors to see if they have been set after the call to rejectValue :

          Code:
          List errs=errors.getAllErrors();
                 int count=0;
                 while (!errs.isEmpty()&& count < 5){
          	Iterator iter=errs.iterator();
          	System.out.println("***********error: " + iter.next());
          	count++;
          }
          and it prints out the following:


          Code:
          ***********error: 
          Field error in object 'portStatus'on field 'msisdn': 
          rejected value []; 
          codes [required.portStatus.msisdn,required.msisdn,required.java.lang.String,required]; 
          arguments []; 
          default message [Please Fill in a value]
          If anyone has any more ideas, that'd be great
          thanks

          Comment


          • #6
            Is 'msisdn' an object or property of an object?

            Why not trim down the jsp like I suggested to give it a test?

            Comment


            • #7
              ok, I've stripped everything down like you said but still no joy

              Controller:
              Code:
              public class PortStatusController extends AbstractCommandController {
              
                  public PortStatusController(){
                      setCommandClass(CommonSearchCriteria.class);
                      setCommandName("portStatus");
                  }
                  
                  @Override
                  public ModelAndView handle(HttpServletRequest request,
                          HttpServletResponse response, Object command, BindException   exceptions)
                          throws Exception {
                  	
                      return new ModelAndView("portStatus","portStatus",command);
                  }
              }
              Validator:
              Code:
              public class PortStatusValidator implements Validator {
                   public boolean supports(Class clazz) {		
              	return CommonSearchCriteria.class.isAssignableFrom(clazz);
                  }	
                	
                  public void validate(Object target, Errors errors) {
                          errors.rejectValue("msisdn", "required", "Please Fill in a value");
                  }
              }
              JSP:
              Code:
              <form:form name="myform" action="portStatus.htm" method="post" commandName="portStatus">
              
              <form:errors path="msisdn"/>
              and to answer your question, 'msisdn' is a property of the object type thats being used (CommonSearchCriteria)

              Code:
              public class CommonSearchCriteria {
                  ...
                  private String msisdn;
                  ...
                }
              I should point out that I'm not creating a messageBundle, and am relying on the default error message being displayed when its not found..

              Comment


              • #8
                Most of the cool stuff with propagating errors to jsp forms is found in the AbstractFormController, so I would recommend extending from that as opposed to the AbstractCommandController (any reason not to?). If you look into it, you will see they have a bunch of code for exposing the errors object to the model in the resulting ModelAndView.

                Comment


                • #9
                  I actually prefer to extend from SimpleFormController in most simple cases, overriding

                  Code:
                  protected void doSubmitAction(Object command)
                  to perform operations on the form backing bean.

                  Comment


                  • #10
                    Woohoo! I changed the controller to implement SimpleFormController instead of AbstractCommandController and I'm now seeing the error message!

                    Thank you so much for your patience and help!

                    Comment

                    Working...
                    X