Results 1 to 10 of 10

Thread: Displaying Errors/Messages from MessageContext in JSP/JSTL

  1. #1
    Join Date
    Oct 2008
    Posts
    2

    Default Displaying Errors/Messages from MessageContext in JSP/JSTL

    Hy,

    I have a Webflow with a model that gets validated after submit.
    The validation is correctly called and I add an errormessage to the MessageContext.
    When i put the MessageContext in the requestScope i can display all errors (${messageContext.allMessages} ).

    But I couldn't find out how to display the errormessages in the form with the Spring form:errors Tag.. the result is always empty.

    jsp code looks like this :
    Code:
    <form:form modelAttribute="registerForm" action="${pageContext.request.scheme }://${pageContext.request.serverName }:${pageContext.request.serverPort }${flowExecutionUrl}">
    <form:errors path="*" ></form:errors>
    <spring:bind path="firstName">
    <input name="${status.expression}" type="text" value="${status.value}" >
    </spring:bind>
    
    								<input type="submit" name="_eventId_submitRegistration" value="Submit" />
    </form:form>
    and my validate Function:
    Code:
      public boolean validateRegister(MessageContext context) {
        MessageResolver msg = new MessageBuilder().error().source("registerForm.firstName").code("register.Field.missing").build();
        context.addMessage(msg);
        context.addMessage(new MessageBuilder().error().source("firstName").code("register.Field.missing").build());
     }

  2. #2
    Join Date
    Nov 2008
    Posts
    10

    Default

    Same question, and I assume that there has to be some binding that is done to Spring Errors, and that this binding happens automatically for 'valicate${state-id} methods.

  3. #3
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    You might want to review the web flow sample booking-mvc which shows use of the errors tag.

    Keith
    Keith Donald
    Core Spring Development Team

  4. #4
    Join Date
    Nov 2008
    Posts
    18

    Default

    Hi, Keith. I've got exactly the same problem as icewolf. I handle all my business exceptions and validation errors via MessageContext, e.g.

    Code:
    		new MessageBuilder().error().source("exists")
    				.code("album.create.error.exists").build());
    When I try to display errors with form:errors tag i can see nothing...
    Code:
    <form:errors path="*" />
    When I try to display them with this code - everything is OK.

    Code:
      <c:forEach items="${flowRequestContext.messageContext.allMessages}" var="message">
        <c:if test="${message.severity eq 'Error'}">
          <div class="error-msg">${message.text}</div>
        </c:if>
      </c:forEach>
    It's not clear fo me, how will spring know about webflow's error messages which are in messageContext?

  5. #5
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Well, take a close look at the ref manual and swf-booking-mvc sample in this area... I suspect you're either missing a model declaration on your view-state or you're not adding the messages with the right source, which should be the fieldName for property failures, null for global failures.

    Keith
    Keith Donald
    Core Spring Development Team

  6. #6
    Join Date
    Oct 2008
    Posts
    2

    Default Solved

    Hy,

    After some debugging and trial i found out that the path to our "model" was the problem.
    First i defined the model to be in the flowContext and also referenced it like this : flowContext.registerForm

    Spring WebFlow and the Error Tags or rather the Binding Code seem to have differing concepts about the use of dots.
    Dont remember which one exactly but one of those two uses dots like jstl (dots denote a property), the other one looks out for a bean named "flowContext.registerForm" (=one String)

    i just had to get rid of the flowContext prefix when not necessary and it worked as supposed.

    bye.

  7. #7
    Join Date
    Nov 2008
    Posts
    18

    Default solved

    Keith, icewolf thanks for help! path="registrationModel.*" instead of path="*" solved this problem

  8. #8
    Join Date
    Jul 2009
    Posts
    3

    Default

    I am able to display the message using the above tips for message context but the error message will not be retained if you refresh the page.

    Is
    requestContext.getMessageContext().addMessage(new MessageBuilder().error().defaultText("error msg").build());
    same as putting the error message in request scope?
    What should i be doing if i want to retaine the error message even after refreshing? Or how can i save MessageContext object in flow scope as its not Serializable (flow scope can store only Serializable objects) ?

    Thanks in advance.
    Last edited by reena; Jul 10th, 2009 at 12:17 AM.

  9. #9
    Join Date
    Jul 2008
    Posts
    21

    Default

    excuse me,
    i've the same problem of post 4 (dabasov) how to fix it?

    - i've just checked attribute model of view-state
    - i've just checked correct MessageContext use
    - i've tried:

    Code:
      <c:forEach items="${flowRequestContext.messageContext.allMessages}" var="message">
        <c:if test="${message.severity eq 'Error'}">
          <div class="error-msg">${message.text}</div>
        </c:if>
      </c:forEach>
    ALL WORK

    - i've tried

    Code:
    <form:errors path="*" />
    <form:errors path="mymodel.*" />
    <form:errors path="currentFormObject" />
    ... and no errors were watched...

    Anyone could help me?

    Thanks


    One other question... with spring 3 + swf 2 what is prefer to use : Errors or MessageContext? (Becouse in this moment wiht Errors everything work)
    Last edited by foxmarco; Nov 3rd, 2010 at 10:01 AM.

  10. #10
    Join Date
    Jul 2008
    Posts
    21

    Default

    any idea? or soluzion?

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •