我已经尝试过3天了解问题,但我不能。 实际上我想更新一行,但不是更新,而是.merge()进行插入。
Id是从mySql数据库自动生成的。
这是调用公式的第一个按钮,其中包含来自先前持久报告的id:
<a th:href="@{|/reports/updateForm/${reportId}|}">
<button class="button btn-default btn-xs pull-right" type="button" th:title="#{report.formEdit}">
<i class="fa fa-edit fa-fw"></i></button></a>
这是应该调用视图updateformular的方面:
@RequestMapping(value = "/updateForm/{id}", produces = "text/html", method = RequestMethod.GET)
public String ReportController.updateForm(@PathVariable("id") long id, Model model) {
CRMReport newCRMReport = CRMReport.findCRMReport(id);
model.addAttribute("newCRMReport", newCRMReport);
return "reports/update";
}
视图更新摘录:
<form action="#" class="form" role="form"
th:object="${newCRMReport}" th:action="@{/reports/update/}"
th:method="put">
<div class="modal-body col-lg-6 form-left">
<div class="panel panel-info">
<div class="panel-heading">
<i th:text="#{report.info}"></i>
</div>
</div>
//Example of the send data fields
<div class="form-group" th:classappend="${#fields.hasErrors('projectState')} ? error">
<label for="projectState" th:text="#{report.projectstatus}"></label>
<select class="form-control" id="projectState" th:field="*{projectState}"
th:size="${pros.length}" multiple="multiple">
<option th:each="pro : ${pros}" th:value="${{pro}}" th:field="*{projectState}" th:text="${pro}"></option>
</select>
</div>
<div class="form-group">
<div>
<input type="hidden" th:field="*{id}" class="form-control" id="id"/>
</div>
</div>
<div class="modal-footer col-lg-12">
<button name="action" value="cancel" type="submit"
class="btn btn-default pull-right">
<i class="fa fa-times fa-fw"></i>
<i th:text="' '+#{report.cancel}"></i>
</button>
<button name="action" value="save" type="submit"
class="btn btn-primary pull-right">
<i class="fa fa-save fa-fw"></i>
<i th:text="' '+#{report.show}"></i>
</button>
</div>
最后一件事就是对previus发送对象newCRMReport进行合并(更新):
@RequestMapping(value = "/update", produces = "text/html", method = RequestMethod.PUT, params = "action=save")
public String ReportController.updateReport(@Valid @ModelAttribute("newCRMReport") CRMReport newCRMReport,
BindingResult result, Model model, SessionStatus status) {
// ID will be x, from the prior object
System.out.println("id before: " + newCRMReport.getId());
status.setComplete();
newCRMReport = newCRMReport.merge();
// ID will be x+1, from the new object
System.out.println("id then: " + newCRMReport.getId());
return "redirect:/reports/list/" + newCRMReport.getId();
}
这是要更新的对象:
@RooJavaBean
@RooToString
@RooJpaActiveRecord(table = "crm_report")
public class CRMReport{
private String projectState;
private String sector;
private String location;
private String client;
private String company;
@JoinColumn
@ManyToOne
private CRMUser responsible;
private String relevance;
private String volumeFrom;
private String volumeTo;
private String chance;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
private Date dateFrom;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
private Date dateTo;
private String timeSpanFrom;
private String timeSpanTo;
@JoinColumn
@ManyToOne
private CRMUser createdBy;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
private Date createdAt;
}
你能帮帮我吗?你知道为什么它执行插入而不是更新吗?
问候
Loopek(:
答案 0 :(得分:2)
我认为问题可能与此块有关:
<div class="form-group">
<div>
<input type="hidden" th:field="*{id}" class="form-control" id="id" th:value="${{reportId}}"/>
<span class="help-inline" th:errors="*{id}">[error]</span>
</div>
</div>
首先,对于隐藏的输入字段,您不会显示任何验证错误消息,因此除了隐藏的输入字段本身之外,除去所有内容。
接下来,使用 th:field =“* {id在 newCRMReport 对象实例的输入字段和 id 属性之间建立关联}“属性。但是您还使用 th:value =“$ {{reportId}} 属性,其值为( reportId ),我认为在视图模型中不可用。这将以 id 字段中的null或空值结束。
尝试使用以下块替换上一个块。然后检查生成的HTML是否有效,隐藏字段是否包含id值。
<input type="hidden" th:field="*{id}" />
答案 1 :(得分:0)
谢谢大家的意见和回答。通过您的注释,我找到了答案。
我现在添加了版本,类型为:&#34;隐藏&#34;到更新视图。
<div class="form-group">
<div>
<input type="hidden" th:field="*{version}"
class="form-control" id="version"/>
</div>
</div>
然后合并执行更新。