Announcement Announcement Module
Collapse
No announcement yet.
Spring:message and error codes resolving question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring:message and error codes resolving question

    Hi,

    I have this code in my JSP that displays all bind and validation errors :

    Code:
    You have <c&#58;out value="$&#123;errors.errorCount&#125;" /> errors&#58;
    <c&#58;forEach var="error" items="$&#123;errors.allErrors&#125;">
      -<spring&#58;message code="$&#123;error.codes&#91;0&#93;&#125;" />
    </c&#58;forEach>
    Is there a command/method to have spring try to resolves all the codes (if many) for an error until it finds one? like:

    1. try "typeMismatch.groups.name"
    2. try "typeMismatch.name"
    3. try "typeMismatch.java.lang.String"
    4. try "typeMismatch"

    thanks in advance,

    Uze

  • #2
    The org.springframework.validation.DefaultMessageCodes Resolver class does this for you. You can implement a different algorithm by implementing the MessageCodesResolver interface and having a bean with the name "messageCodesResolver" in your spring configuration files. The JavaDocs are very good on this.

    Cheers, Stefaan.

    Comment


    • #3
      I agree since I've read the doc more than once, but my problem is how to display that processing in a JSP? Is there a tag or something like
      Code:
      <spring&#58;message code="$&#123;error.resolvedCode&#125;"/>
      that would go through each codes in order in the ressource bundle until it finds one? Am i missing something?

      Comment


      • #4
        I'm reviving this topic since I have the same question and there is no real reply just yet...

        There seems to be no way to get to the DefaultMessageCodesResolver logic using the spring:message tag?

        Now the below JSP fragment doesn't given the expected result even though ${error} is an ObjectError which is MessageSourceResolvable:

        Code:
        <spring&#58;hasBindErrors name="problemReport">
          There are errors&#58;<BR>
          <UL>
            <c&#58;forEach items="$&#123;errors.allErrors&#125;" var="error">
              <LI><spring&#58;message code="$&#123;error&#125;"/></LI>
            </c&#58;forEach>
          </UL>
        </spring&#58;hasBindErrors>

        Maybe this should be posted in JIRA as a RFE?

        Erwin

        Comment


        • #5
          Why don't you just bind the form object and show the errorMessages ? These are the translations of the error codes.

          This is how I do it in velocity, most likely there'll be something similar in JSP:
          Code:
          #springBind&#40;"formObject.*"&#41;
          #if &#40;$status.error&#41;
          <font color="red">
          #foreach &#40;$error in $status.errorMessages&#41;
          	$&#123;error&#125;<br/>
          #end
          </font>
          #end
          where formObject is the name of your form object. The hole thing I put in a macro so I can change the look & feel of the errors line for all pages at any given time.

          In this case your error messages are already translated by the message code resolver.

          However, the spring message tag only takes one string (code) as argument. It might make sense to provide an extra argument to accept a list of codes too. That would be a fair RFE.

          Comment


          • #6
            The answer, which I just found, is to use

            <spring:message message="${error}"/>

            Unfortunately, if you want to say "The field Blah Blah contains an invalid date", you'll need to be creative. The field name is passed along and can be substituted in for {0}, but that may not be what you want it to.

            This sort of leads to wanting to highlight the field in trouble using the field name to detect when to change the style class to some error-like style and just use a generic message like "There is an invalid date".


            Dan

            Comment

            Working...
            X