Results 1 to 10 of 10

Thread: Error messages not displaying in freemarker view, bindingResult is showing errors

  1. #1

    Default Error messages not displaying in freemarker view, bindingResult is showing errors

    When my POST controller action fires, the bindingResult is showing errors for my Account model, but the freemarker view is now showing the error messages for some reason.

    Why are the errrors not being shown?

    Code:
    @RequestMapping(value = "/create", method = RequestMethod.POST)
        public ModelAndView create(@Valid Account account, BindingResult bindingResult) {
    
            ModelAndView mav = new ModelAndView("accounts/new");
            mav.addObject("account", account);
    
            if(bindingResult.hasErrors()) {
                return mav;
            }
       ...
       }
    My view:


    Code:
    <#import "/shared/spring.ftl" as spring>
    <@spring.bind "account" />
    
    <#if spring.status.error >
        <div class="alert alert-error">
            <#list spring.status.errorMessages as error>
                <li>${error}</li>
            </#list>
        </div>
    </#if>
    
    ..
    ..

  2. #2

    Default

    Does anything look wrong with what I did?

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You are returning a ModelAndView and don't add the model from the BindingResult to it which basically destroys your messages. I suggest simply annotate your Account with @ModelAttribute and return the view name to render. Or add the BindingResults model to th ModelAndView.
    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

  4. #4

    Default

    Thanks Marten.

    If I add BindingResults to my ModelAndView, what is the collection that will have the errors?

    BTW, I read your post on multiple validators, curious, how can you do multiple model validation when you use annotation based validation via jsr 303?

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I strongly suggest a read of the API.

    Something along these lines.

    Code:
    ModelAndView mav = new ModelAndView("yourview", bindingResult.getModel());
    Also for it to work 100% you also need the @ModelAttribute annotation...
    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

  6. #6

    Default

    So would the @ModelAttribute be included with the @Valid attribute?

  7. #7

    Default

    OK so this is what I have now:


    Code:
     @RequestMapping(value = "/create", method = RequestMethod.POST)
        public ModelAndView create(@Valid @ModelAttribute("accountForm") AccountForm accountForm, HttpServletRequest request, BindingResult bindingResult) {
    
       ...
       ....
    }
    My accountForm:


    Code:
    public class AccountForm  {
    
        @NotNull
        @Size(min=3, max=50, message="Account name must be between 3 and 50 characters long.")
        private String accountName;
    
        @NotNull
        @Size(min=3, max=50, message="Company name must be between 3 and 50 characters long.")
        private String companyName;
    
       ...
    
    }
    So now when I go to the form page, and hit submit, I get an error, and the breakpoint in my create method doesn't even get run.

    So the error is even before my code in the controller runs, why is

    Code:
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
    Field error in object 'accountForm' on field 'accountName': rejected value [null]; codes [NotNull.accountForm.accountName,NotNull.accountName,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [accountForm.accountName,accountName]; arguments []; default message [accountName]]; default message [may not be null]
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    I'm confused how this could happen, even if my accountForm has validation issues, it shouldn't crash everything like this and not even go to the controller method to handle the post?

  8. #8

    Default

    Ok it was crashing because in my html I had name="name" instead of "accountName".

  9. #9

    Default

    Actually I take that back, if any of my form fields are empty, it crashes before the controller that handles the post can get fired.

    Seems like the validation runs, and fails, and throws the exception:

    Code:
    rg.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
    Field error in object 'accountForm' on field 'companyName': rejected value []; codes [Size.accountForm.companyName,Size.companyName,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [accountForm.companyName,companyName]; arguments []; default message [companyName],50,3]; default message [Company name must be between 3 and 50 characters long.]
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    My fields are marked as not null, but no idea why this would be happening? The whole idea was to see if there were errors in my controller action, and if so, send the model back to the view and display the errors!

    ideas?

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    OK so this is what I have now:
    Which is wrong. Swap the HttpServletRequest and BindingResult. The BindingResult should directly follow the @ModelAttribute (as explained in the reference guide).
    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

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
  •