1
我有有對象的表像這樣一個新的Person形式:動態列表
<table class="table table-striped table-condensed flip-content">
<thead class="flip-content">
<tr>
<th width="20%">
Tipo encabezado
</th>
<th width="20%">
Valor
</th>
<th width="10%"></th>
</tr>
</thead>
<tbody>
<tr th:each="ht : ${headersType}">
<td th:text="${ht.headerType.name}"></td>
<td th:text="${ht.value}"></td>
<td class="operations">
<a class="delete" href="/profile/delete" th:href="@{/profile/delete/__${p.id}__}">
Borrar
</a>
</td>
</tr>
</tbody>
</table>
此表對應於人物形式的下列財產
private Set<EntityHeader> headersType = new HashSet<EntityHeader>();
所以,當我按新標題鏈接時,我想用新信息更新此表而不重新加載頁面。現在,我有這樣的JSON的方法來做到這一點
function addHeader(){
var headerType = $('#idHeaderType').val();
var valueHeader = $('#valueHeaderType').val();
var json = {"headerType.id" : headerType,"value" : valueHeader};
$.ajax({
url: "addHeader.json",
type: 'POST',
data: "idHeader="+headerType+"&valueHeader="+valueHeader,
dataType: "json",
success:function(){
},
error:function(){
}
});
}
這是控制器:
@RequestMapping(value = "person/addHeader.json", method = RequestMethod.POST, headers ="Accept=application/json")
public String addHeader(@RequestParam(value = "idHeader") String idHeader,
@RequestParam(value = "valueHeader") String valueHeader, @ModelAttribute("person") PersonForm personForm,
BindingResult bindingResult, Model model) {
EntityHeader eh = new EntityHeader();
eh.setHeaderType((HeaderType) basicServ.findById(Long.parseLong(idHeader), HeaderType.class));
eh.setValue(valueHeader);
personForm.getHeadersType().add(eh);
model.addAttribute("person", personForm);
return "person/new";
}
我試圖從模型檢索爲personForm並用新值更新列表。但是,當我嘗試保存它時,PersonForm中的headersType爲空,當然表中不顯示添加的標題。
編輯:我發佈一個臨時解決方案,但我喜歡用JSON