我有以下对象。
public class Anonymization {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToMany(mappedBy = "anonymization")
private List<Host> hosts;
@OneToMany(mappedBy = "anonymization")
private List<Item> items;
对象Host
和Item
有几个布尔字段。我想显示一个表单,其中所有Host和Item对象都显示为复选框,以便可以编辑布尔字段。我写了一个控制器来显示表格
@RequestMapping("monitoringsystem/{mId}/anonymization")
public String editAnonymisatzion(Model model, @PathVariable Long mId){
model.addAttribute("mId", mId);
model.addAttribute("anonymization", monitoringSystemRepository.findOne(mId).getAnonymization());
return "monitoringsystem/anonymizationForm";
}
anonymizationForm
应显示for-each-loop
<!-- more code --->
<form class="form-horizontal" th:modelAttribute="anonymization" th:object="${anonymization}" th:action="@{'monitoringsystem/' + ${mId} + '/anonymisatzion/save'}" method="post">
<input type="hidden" th:field="*{id}"/>
<fieldset>
<legend>Set Anonymization</legend>
<div class="panel panel-default">
<div class="panel-heading">Anonymizie Hostnames</div>
<div class="panel-body">
<div class="form-group" th:each="host : ${anonymization.hosts}">
<label class="col-md-4 control-label" for="checkboxes" th:text="${host.name}"></label>
<div class="col-md-4">
<div class="checkbox">
<label for="checkboxes-0">
<input th:field="${host.anonymizeName}" type="checkbox" />
anonymizie
</label>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Anonymizie Items</div>
<div class="panel-body">
<div class="form-group" th:each="item : ${anonymization.items}">
<label class="col-md-4 control-label" for="checkboxes" th:text="${item.name}"></label>
<div class="col-md-4">
<div class="checkbox">
<label for="checkboxes-0">
<input th:field="${item.anonymizeName}" type="checkbox" />
anonymizie name
</label>
</div>
<div class="checkbox">
<label for="checkboxes-0">
<input th:field="${item.anonymizeData}" type="checkbox" />
anonymizie data
</label>
</div>
</div>
</div>
</div>
</div>
<!-- more code --->
不幸的是,当我调用View时,我收到以下错误
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'host' available as request attribute
我检查我的数据库所有关系都已正确设置。 List<Host>
应包含1个和List<Item>
2个条目。
我找不到我的错误。
更新1:
我对传递给表单的匿名化对象进行了调查。它已经完成了。这意味着所有的听力都存在。所以我猜错误与我的观点有关 - 我如何调用该对象。
答案 0 :(得分:0)
我找到了myselfe的解决方案。所有的拳头我改变了在表单中调用列表对象的方式
<form class="form-horizontal" th:modelAttribute="anonymization" th:object="${anonymization}" th:action="@{'monitoringsystem/' + ${mId} + '/anonymisatzion/save'}" method="post">
<input type="hidden" th:field="*{id}"/>
<fieldset>
<legend>Set Anonymization</legend>
<div class="panel panel-default">
<div class="panel-heading">Anonymizie Hostnames</div>
<div class="panel-body">
<div class="form-group" th:each="host, statHost : *{hosts}">
<label class="col-md-4 control-label" for="checkboxes" th:text="*{hosts[__${statHost.index}__].name}"></label>
<div class="col-md-4">
<div class="checkbox">
<label for="checkboxes-0">
<input th:field="*{hosts[__${statHost.index}__].anonymizeName}" type="checkbox" />
anonymizie
</label>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Anonymizie Items</div>
<div class="panel-body">
<div class="form-group" th:each="item, statItem : *{items}">
<label class="col-md-4 control-label" for="checkboxes" th:text="*{items[__${statItem.index}__].name}"></label>
<div class="col-md-4">
<div class="checkbox">
<label for="checkboxes-0">
<input th:field="*{items[__${statItem.index}__].anonymizeName}" type="checkbox" />
anonymizie name
</label>
</div>
<div class="checkbox">
<label for="checkboxes-0">
<input th:field="*{items[__${statItem.index}__].anonymizeData}" type="checkbox" />
anonymizie data
</label>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<a th:href="@{/monitoringsystem}" class="btn btn-default btn-small">Cancel</a> <button id="singlebutton" name="singlebutton" class="btn btn-primary btn-small">Submit</button>
</div>
</div>
</div>
</div>
</fieldset>
</form>
此外,getter和setter的命名似乎存在问题。我的就像:
setBoolean(boolean b){
this.b = b;
}
isBoolean(){
return b;
}
Spring或thymeleaf(我不知道哪个组件负责)似乎需要以get开头的getter。改变它之后它现在正在工作。
setBoolean(boolean b){
this.b = b;
}
getBoolean(){
return b;
}