Announcement Announcement Module
Collapse
No announcement yet.
Error messages not displaying in freemarker view, bindingResult is showing errors Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Does anything look wrong with what I did?

    Comment


    • #3
      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.

      Comment


      • #4
        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?

        Comment


        • #5
          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...

          Comment


          • #6
            So would the @ModelAttribute be included with the @Valid attribute?

            Comment


            • #7
              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?

              Comment


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

                Comment


                • #9
                  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?

                  Comment


                  • #10
                    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).

                    Comment

                    Working...
                    X