Announcement Announcement Module
Collapse
No announcement yet.
Errors, but no Errors displayed Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Errors, but no Errors displayed

    Hi Folks!

    I do have a quite strange problem. I try to get my first SpringApplication run and in a Form there's an InputField, where a number should be entered. The JSP looks like this:
    Code:
    	  <spring&#58;bind path="book.price">
    		<td valign="top" class="normtext">Preis &#40;&euro;&#41;
    				<c&#58;out value="$&#123;status.errorMessage&#125;"/>
    		</td>
    		<td valign="top">
    			<input class="bigfunc" name="<c&#58;out value="$&#123;status.expression&#125;"/>" value="<c&#58;out value="$&#123;status.value&#125;"/>">
    		</td>
    	  </spring&#58;bind>
    To go for sure, I added some kind of "ErrorOverview" at the top of the same Page:
    Code:
    	<spring&#58;bind path="book.*">
    		<c&#58;forEach items="$&#123;status.errorMessages&#125;" var="errorMessage">
    			<c&#58;out value="$&#123;errorMessage&#125;"/><br>
    		</c&#58;forEach>
    	</spring&#58;bind>
    Now, when the form is being submitted, my logger tells me, that there was an Error, because "book.price" can not be mapped from Spring to BigDecimal. This occures whenever the field is left empty, so the mapping from "" to '0' is not possible.

    This is the Problem to solve, but the strange thing on this is, that no Error is displayed. When I click 'submit' I get to the same page, like it was just reloaded. The only hint on there was something wrong is the logging:
    Code:
    Field error in object 'book' on field 'price'&#58; rejectedValue=&#91;&#93;; codes=&#91;typeMismatch.book.price,typeMismatch.price,typeMismatch.java.math.BigDecimal,typeMismatch&#93;; arguments=&#91;&#40;org.springframework.context.support.DefaultMessageSourceResolvable&#41;&#91;codes=&#91;book.price,price&#93;; arguments=&#91;null&#93;; defaultMessage=&#91;price&#93;&#93;&#93;; defaultMessage=&#91;Failed to convert property value of type &#91;java.lang.String&#93; to required type &#91;java.math.BigDecimal&#93; for property 'price'; nested exception is java.lang.NumberFormatException&#58; null&#93;;
    Thanks in advance for any help,
    Karsten

  • #2
    The way it works for me is when binding to "book.price", ${status.errorMessages} will have the field errors rejected on "price", and when binding to "book", ${status.errorMessages} will have all the global errors rejected on the object itself. I did try to bind to "book.*" once but it didn't work for me either.

    Comment


    • #3
      I tried that. Doesn't work either...

      Comment


      • #4
        Now I'm just wild guessing here - do you have any custom validation or other processing code that might have accidentally dropped the errors from before?

        Comment


        • #5
          Guessing is good!

          Well, I do have a Validator. Quite easy, it looks like this:
          Code:
          public class BookValidator implements Validator &#123;
              private Logger logger = Logger.getLogger&#40;this.getClass&#40;&#41;&#41;;
              
              public boolean supports&#40;Class clazz&#41; &#123;
                  return clazz.equals&#40;Book.class&#41;;
              &#125;
          
              public void validate&#40;Object command, Errors errors&#41; &#123;
                  Book book = &#40;Book&#41; command;
                  System.out.println&#40;"Validating " + book&#41;;
                  
                  if &#40;book.getTitle&#40;&#41; == null || &#40;book.getTitle&#40;&#41;.equals&#40;""&#41;&#41;&#41; &#123;
                      errors.rejectValue&#40;"title", "required.title", "Title is required."&#41;;
                  &#125;
                  if &#40;&#40;book.getPrice&#40;&#41; == null&#41; || &#40;book.getPrice&#40;&#41;.equals&#40;""&#41;&#41;&#41; &#123;
                      errors.rejectValue&#40;"title", "required.price", "Price is required."&#41;;
                  &#125;
              &#125;
          &#125;
          And the xml-entries are these:
          Code:
            <bean id="editBookController" class="de.valtech.eta.vlm.controller.EditBookController">
              <property name="bookService"><ref bean="bookService" /></property>
              <property name="personService"><ref bean="personService" /></property>
              <property name="statusService"><ref bean="statusService" /></property>
              <property name="formView"><value>bookDetails</value></property>
              <property name="successView"><value>entered</value></property>
              <property name="sessionForm"><value>true</value></property>
              <property name="commandClass"><value>de.valtech.eta.vlm.model.Book</value></property>
              <property name="commandName"><value>book</value></property>
              <property name="validator"><bean class="de.valtech.eta.vlm.validation.BookValidator"/></property>
            </bean>
          Did I missunderstand something or doesn't Spring like me?! :shock:

          Comment


          • #6
            Originally posted by KarstenTS
            Code:
            public class BookValidator implements Validator &#123;
                private Logger logger = Logger.getLogger&#40;this.getClass&#40;&#41;&#41;;
                
                public boolean supports&#40;Class clazz&#41; &#123;
                    return clazz.equals&#40;Book.class&#41;;
                &#125;
            
                public void validate&#40;Object command, Errors errors&#41; &#123;
                    Book book = &#40;Book&#41; command;
                    System.out.println&#40;"Validating " + book&#41;;
                    
                    if &#40;book.getTitle&#40;&#41; == null || &#40;book.getTitle&#40;&#41;.equals&#40;""&#41;&#41;&#41; &#123;
                        errors.rejectValue&#40;"title", "required.title", "Title is required."&#41;;
                    &#125;
                    if &#40;&#40;book.getPrice&#40;&#41; == null&#41; || &#40;book.getPrice&#40;&#41;.equals&#40;""&#41;&#41;&#41; &#123;
                        errors.rejectValue&#40;"title", "required.price", "Price is required."&#41;;
                    &#125;
                &#125;
            &#125;
            I'm confused. In the OP the error was caused by 'price' not being able to convert to BigDecimal, but here your code compares it with an empty string?

            Comment


            • #7
              Yes... um, I did that because I thought I could catch the Problem, that String cannot be mapped to BigDecimal. However, when I take away that part, the problem is the same:
              Code:
                  public void validate&#40;Object command, Errors errors&#41; &#123;
                      Book book = &#40;Book&#41; command;
                      System.out.println&#40;"Validating " + book&#41;;
                      
                      if &#40;book.getTitle&#40;&#41; == null || &#40;book.getTitle&#40;&#41;.equals&#40;""&#41;&#41;&#41; &#123;
                          errors.rejectValue&#40;"title", "required.title", "Title is required."&#41;;
                      &#125;
                      if &#40;book.getPrice&#40;&#41; == null&#41; &#123;
                          errors.rejectValue&#40;"title", "required.price", "Price is required."&#41;;
                      &#125;
                  &#125;
              And logger shows me this.
              2005-04-08 09:17:07,065 INFO [de.valtech.eta.vlm.controller.EditBookController] - BindEx: org.springframework.validation.BindException: BindException: 3 errors; Field error in object 'book' on field 'price': rejectedValue=[]; codes=[typeMismatch.book.price,typeMismatch.price,typeMis match.java.math.BigDecimal,typeMismatch]; arguments=[(org.springframework.context.support.DefaultMessag eSourceResolvable)[codes=[book.price,price]; arguments=[null]; defaultMessage=[price]]]; defaultMessage=[Failed to convert property value of type [java.lang.String] to required type [java.math.BigDecimal] for property 'price'; nested exception is java.lang.NumberFormatException: null]; Field error in object 'book' on field 'title': rejectedValue=[]; codes=[required.title.book.title,required.title.title,req uired.title.java.lang.String,required.title]; arguments=[null]; defaultMessage=[Title is required.]; Field error in object 'book' on field 'title': rejectedValue=[]; codes=[required.price.book.title,required.price.title,req uired.price.java.lang.String,required.price]; arguments=[null]; defaultMessage=[Price is required.]

              Comment


              • #8
                Hi,

                If you want to display the error message for type mismatch, then you have to add the following line in your message.properties file

                typeMismatch.fieldname= error message

                Regards,
                Kamal

                Comment


                • #9
                  I never heard of a message.properties file before. And I didn't find much about it. Where can I get some information?

                  Another thing, if I get the error displayed, I don't even want it to occure. And the other Errors should've been shown yet, right?!
                  So: How do I tell Spring that if the field is empty a zero or a null should be made persistent?

                  Comment

                  Working...
                  X