将列表添加到会话中并对其进行迭代

时间:2018-06-20 07:18:58

标签: java spring jsp model-view-controller thymeleaf

我已经完成了一个小的Web应用程序,我使用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被记住了

1 个答案:

答案 0 :(得分:1)

根据“错误”,查看数据库表。“ capienzamax”列中的某个位置为“ null”。