【问题标题】:add List to session and iterate on it将列表添加到会话并对其进行迭代
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 是的,但我已经使用 findById(id) 从数据库中检索对象中心,它没有该数据为空
  • 根据错误,您的库伦 capienzamax 设置为 not-null 并且您正在尝试插入 null 值,这就是您收到此错误的原因。如果您能够在 ${centro.capienzamax} 的 thyemleaf 中获得值,但在提交后没有获得此错误,则意味着您没有获得 capienzamax 控制器上的对象。
  • 当您尝试使用 findAll() 时,它可能包含一些具有空值的记录。尝试做这样的事情。 model.addAttribute("centri",this.centroService.findById(id));
  • @Sumityes,这就是问题所在,但是为什么,所以对象是从数据库中检索的,如果它在数据库上,则该字段存在,并且在我可以选择中心的页面中我看到数据是好的:
    这是显示
  • @SupunDharmarathne 我用这个查找所有显示所有中心,如果我点击一个我保存它并获取我用来检索它的 ID:@RequestMapping("/confermaAttivita/{id}" ) public String conferma(@PathVariable("id") Long id, Model model, HttpSession session) { Centro c = this.centroService.findById(id); model.addAttribute("centro",c);返回“attivitaForm/paginaConfermaAttivita”; }
  • 猜你喜欢
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2017-05-11
    • 2014-03-21
    • 1970-01-01
    • 2021-04-11
    相关资源
    最近更新 更多