我已经完成了一个小的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被记住了

最佳答案

按照错误查看数据库表。在“ capienzamax”列中的某处具有“ null”值。

关于java - 将List添加到 session 并对其进行迭代,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50942432/

10-10 23:03