在 Freemarker 模板中显示 Spring MVC 验证错误

发布于 2024-12-06 23:49:59 字数 1425 浏览 1 评论 0原文

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

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

尽管提交的表单存在错误,下面的行始终返回 0:

${spring.status.errorMessages?size}

我的控制器代码如下:

@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);
}

createMav 方法如下:

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

有没有办法使用 Freemarker 来实现此目的+ Spring MVC?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生生漫 2024-12-13 23:49:59

我找到了一种使用标准 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="errors">
            <@spring.message "admin.error.globalMessage" />
        </div>
    </#if>
</#macro>

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

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

            <@formErrors />                        
            ....
</@form.form>

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>
忆依然 2024-12-13 23:49:59

您可以编写如下:

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

更多信息位于 BindStatus错误类。

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.

身边 2024-12-13 23:49:59

尝试这样的事情:

<@spring.bind "webPage" />
<#if (spring.status.errors.allErrors?size > 0) >
    <@spring.message "my.global.error.code"/>
</#if>

Try something like this:

<@spring.bind "webPage" />
<#if (spring.status.errors.allErrors?size > 0) >
    <@spring.message "my.global.error.code"/>
</#if>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文