在 Freemarker 模板中显示 Spring MVC 验证错误 [英] Displaying Spring MVC validation errors in Freemarker templates

查看:36
本文介绍了在 Freemarker 模板中显示 Spring MVC 验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果控制器返回绑定错误,我会尝试在我的 freemarker 模板中显示全局验证错误列表.我可以显示与字段关联的错误,但我想检测特定 bean 中何时发生错误并在页面顶部显示一条消息.我试过使用下面的例子,它不产生任何输出:

<@spring.bind "webPage"/>....<#if spring.status.error>您输入的数据有问题:<ul><#list spring.status.errorMessages as error><li>${error?html}</li></#list></#if>

下面的行总是返回 0,尽管提交的表单有错误:

${spring.status.errorMessages?size}

我的控制器代码如下:

@RequestMapping(method = RequestMethod.POST)public ModelAndView save(@ModelAttribute("webPage") @Valid WebPage page, BindingResult 结果, Model model) {如果(!model.containsAttribute(站点")){throw new IllegalArgumentException("模型必须包含站点属性.");}Site site = (Site) model.asMap().get("site");如果 (!result.hasErrors() && !page.isNew()) {this.pageService.save(page, site);} else if (!result.hasErrors() && page.isNew()) {this.pageService.create(page, site);}返回 createMav(result);}

createMav 方法如下:

public ModelAndView createMav(BindingResult result) {ModelAndView mav = new ModelAndView();mav.setViewName(getPrimaryControllerView());mav.addAllObjects(result.getModel());返回 mav;}

有没有办法使用 Freemarker + Spring MVC 来实现这一点?

解决方案

我找到了一种使用标准 MVC JSP 标记库的迂回方法来做到这一点.我将其提供给 Freemarker:

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

然后我使用以下宏来显示全局错误消息:

<#macro formErrors><#assign formErrors><@form.errors path="*"/></#assign><#if formErrors?has_content><div id="错误"><@spring.message "admin.error.globalMessage"/>

</#if></#macro>

我只是将以下行放在我希望出现此错误消息的地方(这必须包含在提交给控制器的表单元素中):

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

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:

<@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:

${spring.status.errorMessages?size}

My controller code is below:

@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:

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?

解决方案

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>

这篇关于在 Freemarker 模板中显示 Spring MVC 验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆