【发布时间】:2015-08-31 06:17:22
【问题描述】:
我有一个新的 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>
此表对应Person表单的如下属性
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
【问题讨论】:
标签: java jquery ajax json spring