Results 1 to 6 of 6

Thread: form:error not outputting messages

  1. #1

    Question form:error not outputting messages

    For some reason my form:errors tag isn't outputting anything, even if the Errors object is populated.

    I'm quite confused since other Controllers and views are able to output my errors just fine, and I can't for the life of me see what the difference is in this case.

    Here's what I have in the problematic Controller class:

    Code:
    @RequestMapping(value=URI_EDIT, method=RequestMethod.POST)
    public ModelAndView edit(ModelMap model, @ModelAttribute UpdateSessionCommand command, Errors errors) {
    
            // Validate the Session
            new UpdateSessionCommandValidator().validate(command, errors);
    
            // If there were errors or missing fields, return to the creation view
            if( errors.hasErrors() ) {
                logger.debug(errors.getAllErrors().toString());
                prepareEditViewModel(model, command);
                return new ModelAndView(VIEW_EDIT, model);
            }
    
            [...]
    
    
        }
    And here's what's in the JSP:

    Code:
    <c:url var="url" value="${formAction}"/>
    <form:form action="${url}" commandName="sessionCommand">
    
        <form:errors path="*" cssClass="error" />
    
        [...]
    Does anyone have any ideas? This is driving me batty.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    By returning a new modelandview with a new/empty model you loose all the previous data including your errors...

    I suggest instead of return a ModelAndView simply return the view name (a String) and then it will work. Spring will merge the existing model with everything available and thus does the heavy lifting for you.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Thanks for the quick reply, but I'm afraid that didn't resolve the issue.

    The ModelAndView I was creating was actually using the same ModelMap that was passed in by Spring as a parameter, so I guess I'm not too surprised. But, sadly, even when I return a String as a view, the errors are not being output.

    I'm completely stumped. Any other ideas?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    The ModelMap is something else. The ModelMap can only be used for inputting and adding data to the model it initially doesn't contain much... Basically it should work. What are you returning as a view?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    Thanks for the clarification about the model. I'm still learning all the ins and outs of Spring.

    At any rate, VIEW_EDIT is just a static final String with the name of the view, that's all. Specifically, it's: "/sessions/patient-session/edit"

    I've been utterly baffled all day, especially since I have other Controllers and views that output validation and binding errors without problems, and I could swear that I'm not doing anything differently.

    Is there any extra information that I could provide to help track this down?

  6. #6

    Default

    Well, after countless hours, I discovered the problem. My @ModelAttribute parameter was not explicitly named. (i.e. @ModelAttribute("command") MyCommand command)

    Since the command parameter was different from the class name, it somehow created an issue outputting the form errors. I assume this is a convention over configuration thing that I just wasn't aware of.

Tags for this Thread

Posting Permissions

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