2017-10-10 51 views
1

我有一个布尔值矩阵(数组数组),我想在表单中显示,然后在更改后提交。 我有这个问题,我没有更多的想法,为什么它不工作。 有人可以给我一个建议吗?BindingResult和bean名称'matrix [0] [0]'的无格式目标对象都不作为请求属性

我的控制器:

@Controller 
@RequestMapping(value = "/konfiguration") 
public class VerteilungController { 

@ModelAttribute("matrix") 
public List<List<Boolean>> getVerteilungenMatrix() { 
    List<List<Boolean>> result2 = new ArrayList<>(); 
    for (int i = 0; i < kategorien.size(); i++) { 
     result2.add(new ArrayList<>()); 
    } 
    //... 
    return result2; 
} 

@RequestMapping(value = "/verteilung", method = RequestMethod.GET) 
public String showPage(Model model) { 
    model.addAttribute("matrix", getVerteilungenMatrix()); 
    return "konfiguration/verteilung"; 
} 
} 

形式:

<form id="verteilung_form" class="form-horizontal" method="post" action="/verteilung" 
       th:action="@{/konfiguration/verteilung}" 
       th:object="${matrix}"> 

       <table class="table-hover"> 
        <tr th:each="row: ${matrix}"> 
        <td th:each="value: ${row}"> 
           <input type="checkbox" th:field="${matrix[__${rowStat.index}__][__${valueStat.index}__]}"/> 
         </td> 
        </tr> 
       </table> 
       <div > 
        <button th:text="#{button.save}" class="btn btn-default" type="submit" name="save">Speichern</button> 
        <button th:text="#{button.reset}" name="reset" class="btn btn-default">Zurücksetzen</button> 
       </div> 
      </form> 

Openening的页面,我得到

例外:处理器的“执行过程中的错误org.thymeleaf.spring4.processor.attr .SpringInputCheckboxFieldAttrProcessor'

而在日志

java.lang.IllegalStateException:既不BindingResult也不对bean名称纯目标对象 '矩阵[0] [0]' 可作为请求属性

+0

什么是'kategorien'?我没有看到它的定义在任何地方。 –

+0

这与此无关。它的大小大于零。 'result2'充满了非空的布尔值。 – Juliane

回答

1

你滥用th:objectth:fieldth:object代表命令对象代表整个表单。

Command对象的名称是Spring MVC的给予形成-支持豆,这是,该模型形式的领域,并提供将由框架,建立和获得的值输入中使用getter和setter方法的对象由浏览器侧的用户提供。

在另一方面th:field确实与形式,后台bean的属性绑定您输入的所有繁重的工作。th:field中的值应指向th:object中的对象字段。

th:field属性的值必须是选择表达式(* {...}),这是有道理的,因为它们将在form-b​​acking bean上进行评估,而不是在上下文变量上进行评估(或Spring MVC术语中的模型属性)。

请检查出来here

回到你的代码。要解决它,你应该创建一个表单支撑类,并提供矩阵作为类中的字段,例如:

public class FormBean { 

    private List<List<Boolean>> matrix; 

    FormBean() { } 

    public FormBean(List<List<Boolean>> matrix) { 
     this.matrix = matrix; 
    } 

    public List<List<Boolean>> getMatrix() { 
     return matrix; 
    } 

    public void setMatrix(List<List<Boolean>> matrix) { 
     this.matrix = matrix; 
    } 
} 

下一页请提供FormBean对象作为一个模型属性。当您提供标记为@ModelAttribute的方法时,将为您完成模型分配。更新您的控制器的身体下面:

@ModelAttribute("formBean") 
public FormBean getFormBean() { 
    return new FormBean(getVerteilungenMatrix()); 
} 

@RequestMapping(value = "/verteilung", method = RequestMethod.GET) 
public String showPage() { 
    return "konfiguration/verteilung"; 
} 

private List<List<Boolean>> getVerteilungenMatrix() { 
    List<List<Boolean>> result2 = new ArrayList<>(); 
     for (int i = 0; i < kategorien.size(); i++) { 
      result2.add(new ArrayList<>()); 
     } 
     //... 
     return result2; 
} 

最后请更新您的形式为以下内容:

<form id="verteilung_form" class="form-horizontal" method="post" action="/verteilung" 
      th:action="@{/konfiguration/verteilung}" 
      th:object="${formBean}"> 

      <table class="table-hover"> 
       <tr th:each="row: *{matrix}"> 
       <td th:each="value: ${row}"> 
          <input type="checkbox" th:field="*{matrix[__${rowStat.index}__][__${valueStat.index}__]}"/> 
        </td> 
       </tr> 
      </table> 
      <div > 
       <button th:text="#{button.save}" class="btn btn-default" type="submit" name="save">Speichern</button> 
       <button th:text="#{button.reset}" name="reset" class="btn btn-default">Zurücksetzen</button> 
      </div> 
     </form> 

现在一切都应该按预期工作。

+0

非常感谢您的详细解答。有用 :)。我不知道应该总是有豆状物体,希望有一种方法可以在其中放入简单的物体。所以我必须包裹我的矩阵,没问题。 – Juliane

相关问题