【问题标题】:Validation of @spring.formInput in #list iterator在#list 迭代器中验证@spring.formInput
【发布时间】:2012-10-30 12:43:55
【问题描述】:

我有一个 freemarker 页面,我需要从条目列表中发布一个条目。

为了克服 spring.formInput 和 freemarker 列表 (see problem here) 的问题,我按以下方式构建 html 表单:

<#list entries as entry>
    <@form.form method="POST" commandName="receiver">
        <@spring.formInput "entries.entry[${entry_index}].name"/>
        <@spring.formInput "entries.entry[${entry_index}].value"/>
    </@form.form>
</#list>

我的控制器如下所示:

@RequestMapping(method=POST, params="save") 
public String save(@ModelAttribute(value="entries") @Valid Entries entries, BindingResult result, Model model){
    /*notice, we always get the last item in the list, since the entries  "carries" the index of the form through, and  creates
     * null-object in the first n places
    */ 
    int index = entries.size()-1;
    Entry entry = entries.get(index);
    if (!result.hasErrors()) {
        formService.save(entry);
    }

    return "";
}

不幸的是,每当我发布一个条目而不是列表中的第一个条目时,@Valid 注释都会导致错误。

发生这种情况是因为 spring 自动使用 null 值填充我的列表,直到当前索引。

  • 我更愿意发布条目而不是条目,但我不这样做 认为这是可以做到的。
  • 或者,我如何阻止验证发生在 列表中的前 n-1 个条目?
  • 或者,我能否以某种方式测试列表(索引)中的哪个对象导致 错误,这样我就可以避免前 n-1 个条目的错误?

【问题讨论】:

    标签: java spring validation freemarker


    【解决方案1】:

    几个月前我遇到过类似的问题。但是我在使用 LazyList 时遇到了问题。也许你会在这个question 中找到你的答案。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 2016-01-12
      • 2021-05-26
      • 1970-01-01
      • 2017-11-11
      相关资源
      最近更新 更多