Results 1 to 2 of 2

Thread: Displaying Spring MVC validation errors in Freemarker templates

  1. #1
    Join Date
    Nov 2007
    Posts
    12

    Default Displaying Spring MVC validation errors in Freemarker templates

    I'm trying to display a list of global validation errors in my freemarker template if a controller returns binding errors. I can display errors that are associated with a field, but I want to detect when an error has occurred within a specific bean and display a message at the top of the page. I've tried using the example below which produces no output:

    Code:
    <@spring.bind "webPage" />
    ....
    <#if spring.status.error>
    There were problems with the data you entered:
    <ul>
    <#list spring.status.errorMessages as error>
    <li>${error?html}</li>
    </#list>
    </ul>
    </#if>
    The line below always returns 0, despite there being errors with the submitted form:

    Code:
    ${spring.status.errorMessages?size}
    My controller code is below:

    Code:
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView save(@ModelAttribute("webPage") @Valid WebPage page, BindingResult result, Model model) {
        if (!model.containsAttribute("site")) {
            throw new IllegalArgumentException("Model must contain site attribute.");
        }
        Site site = (Site) model.asMap().get("site");
        if (!result.hasErrors() && !page.isNew()) {
            this.pageService.save(page, site);
        } else if (!result.hasErrors() && page.isNew()) {
            this.pageService.create(page, site);
        } 
        return createMav(result);
    }
    The createMav method is below:

    Code:
    public ModelAndView createMav(BindingResult result) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName(getPrimaryControllerView());
        mav.addAllObjects(result.getModel());
        return mav;
    }
    Is there a way to achieve this using Freemarker + Spring MVC?

  2. #2
    Join Date
    Nov 2007
    Posts
    12

    Default

    I found a roundabout way to do this using the standard MVC JSP taglib. I make this available to Freemarker:

    Code:
    <#assign form=JspTaglibs["http://www.springframework.org/tags/form"] />
    I then use the following macro to display global error message:

    Code:
    <#macro formErrors>
        <#assign formErrors><@form.errors path="*" /></#assign>
        <#if formErrors?has_content>
            <div id="errors">
                <@spring.message "admin.error.globalMessage" />
            </div>
        </#if>
    </#macro>
    I just place the following line where ever I want this error message to appear (this has to be contained within the form element that submits to the controller):

    Code:
    <@form.form method="POST" commandName="webPage">
    
                <@formErrors />                        
                ....
    </@form.form>

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
  •