Displaying Spring MVC validation errors in Freemarker templates

谁说胖子不能爱 提交于 2019-11-30 23:23:51

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>

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.

Try something like this:

<@spring.bind "webPage" />
<#if (spring.status.errors.allErrors?size > 0) >
    <@spring.message "my.global.error.code"/>
</#if>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!