Java 将列表添加到会话并对其进行迭代
我已经做了一个小的web应用程序,我正在使用thymeleaf和jsp作为前端,现在我需要使用2个对象,一个列表和一个简单的对象,在控制器中我使用这个:Java 将列表添加到会话并对其进行迭代,java,spring,jsp,model-view-controller,thymeleaf,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, Htt
@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";
}
<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>
在html页面中,我使用以下内容:
@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";
}
<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>
在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”值。尝试将初始数据集设置为session。@SupunDharmarathne,像注释行一样?因为我已经这样做了,但是html不会在没有更改的情况下显示它,我现在也不知道如何显示它,谢谢您在这两个页面之间进行遍历时是否执行任何其他db操作?如果您共享完整的实现会更好。是的,但是我已经使用findById(id)从数据库中检索了object centro,根据错误,您的coulmn capienzamax设置为not null,并且您试图在其中插入null值,这就是您出现此错误的原因。如果您能够在thyemleaf中获取${centro.capienzamax}的值,但在提交后无法获取,则出现此错误意味着您无法在控制器上获取capienzamax对象。当您尝试使用findAll()时,它可能包含一些具有空值的记录。试试这样做。model.addAttribute(“centri”,this.centroService.findById(id))@Sumityes,这就是问题所在,但是为什么,对象是从数据库中检索的,如果它在数据库中,字段就存在,在我可以选择中心的页面中,我看到的数据是OK的:这是display@SupunDharmarathne我使用“全部查找”来显示所有中心,如果我点击其中一个,我会保存它并获取我用来检索它的id:@RequestMapping(“/confermaattivta/{id}”)公共字符串conferma(@PathVariable(“id”)Long id,Model Model,HttpSession session){Centro c=this.centroService.findById(id);Model.addAttribute(“Centro”,c);返回“attivatiform/pagonfermaattivat”;}