Displaying Spring MVC validation errors in Freemarker templates

前端 未结 4 1103
终归单人心
终归单人心 2021-01-06 14:29

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 f

相关标签:
4条回答
  • 2021-01-06 14:47

    You can write as follows:

    <#if spring.status.error>
    <ul>
       <#list spring.status.errors.globalErrors as error>
       <li>${error.defaultMessage}</li>   
       </#list>
    </ul>
    </#if>
    

    More info at BindStatus and Errors classes.

    0 讨论(0)
  • 2021-01-06 14:49

    See the documentation: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/view.html#view-velocity-forms. It has an example of what you want to do.

    0 讨论(0)
  • 2021-01-06 15:01

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

    <#assign form=JspTaglibs["http://www.springframework.org/tags/form"] />
    

    I then use the following macro to display global error message:

    <#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):

    <@form.form method="POST" commandName="webPage">
    
                <@formErrors />                        
                ....
    </@form.form>
    
    0 讨论(0)
  • 2021-01-06 15:01

    Try something like this:

    <@spring.bind "webPage" />
    <#if (spring.status.errors.allErrors?size > 0) >
        <@spring.message "my.global.error.code"/>
    </#if>
    
    0 讨论(0)
提交回复
热议问题