【发布时间】:2018-11-29 05:53:31
【问题描述】:
我做了一个小网络应用程序,我在前端使用 thymeleaf 和 jsp,现在我需要使用 2 个对象,一个列表和一个简单对象,在控制器中我使用这个:
@RequestMapping(value = "/attivita")
public String newCentro(@Valid @ModelAttribute("attivita") Attivita attivita,
Model model, BindingResult br, HttpSession session) {
this.validator.validate(attivita, br);
if (br.hasErrors()) {
return "attivitaForm/formAttivita";
} else if (this.service.alreadyExists(attivita)) {
model.addAttribute("exists", "Questa attività già esiste");
return "attivitaForm/formAttivita";
}
session.setAttribute("attivita", attivita);
// session.setAttribute("centri", this.centroService.findAll());
model.addAttribute("centri", this.centroService.findAll());
return "attivitaForm/addCentro2Attivita";
}
在页面 html 中我使用这个:
<div th:each="centro : ${centri}">
<div class="row">
<div class="col-xs-3 text-left">
<a th:href="@{'/confermaAttivita' + '/' + ${centro.id} }">
<span th:text="${centro.nome}" style="text-decoration: underline;"></span>
</a>
</div>
<div class="col-xs-3"><span th:text="${centro.indirizzo}"></span></div>
<div class="col-xs-3"><span th:text="${centro.email}"></span></div>
<div class="col-xs-3"><span th:text="${centro.capienzamax}"></span></div>
</div>
</div>
到目前为止,它运行良好,我看到了中心列表,但从现在开始列表丢失了,因为我转到另一个检查页面,在该页面之后我返回这个:
There was an unexpected error (type=Internal Server Error, status=500).
could not execute statement; SQL [n/a]; constraint [capienzamax]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement.
在 STS 中,我看到了以下日志:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [capienzamax]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
org.postgresql.util.PSQLException: ERROR: null value in column "capienzamax" violates not-null constraint
Dettaglio: Failing row contains (12, null, null, null, null, null, null, null, null).
我认为是因为从列表中选择的对象丢失了,但我看到 id 被记住了
【问题讨论】:
-
尝试将初始数据集设置为会话。
-
@SupunDharmarathne,喜欢评论的行吗?因为我已经完成了,但是没有更改html不会显示它,我现在不知道如何显示它,谢谢
-
在这两个页面之间进行遍历时,您是否在进行任何其他 db 操作?
-
如果你分享你的完整实现会更好。
标签: java spring jsp model-view-controller thymeleaf