如何使用Thymeleaf发布实体类型数据列表? [英] How to post a List of Entity type data using Thymeleaf?
问题描述
我的项目基于spring boot,Thymeleaf,mysql,html和Jquery.
My project based on spring boot,Thymeleaf,mysql,html and Jquery.
场景 我的场景是使用Thyemeleaf发布EntSetCharges数据列表并点击Spring boot @RestController. 以前,我曾处理此错误错误1: 错误2: Rightnow遇到另一个错误
Scenario My scenario is POST a List of EntSetCharges Data using Thyemeleaf and hitting the Spring boot @RestController. Previously i worked with this errors Error 1: Error 2: Rightnow got another Error
2017-11-22 14:58:19.717错误1420-[nio-8080-exec-1] oaccC [.[.[/].[dispatcherServlet]:Servlet [dispatcherServlet]的Servlet.service()在路径为[]的上下文中引发了异常[请求处理失败;嵌套的异常是org.thymeleaf.exceptions.TemplateProcessingException:评估SpringEL表达式的异常:"tempEntSetChargesList [0] .chargesName"(模板:"SetCharges"-第37行,第91行)] ,其根本原因 org.springframework.expression.spel.SpelEvaluationException:EL1025E:集合具有'0'元素,索引'0'无效
2017-11-22 14:58:19.717 ERROR 1420 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "tempEntSetChargesList[0].chargesName" (template: "SetCharges" - line 37, col 91)] with root cause org.springframework.expression.spel.SpelEvaluationException: EL1025E: The collection has '0' elements, index '0' is invalid
该行表示HTML代码是表格的第一列
The Line Indicates the HTML code is table's 1st column
th:field ="* {tempEntSetChargesList [__ $ {status.index} __].chargesName}"
我仍然没有成功将数据发布到控制器.请帮助我.
I still not succeeded to POST the data to controller..Please Help me out..
这里是完整代码
<form id="setchargesformid" method="post" th:object="${wrpSetCharges}" th:action="@{/updatesetcharges}">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Charge Name</th>
<th>Amount</th>
<th>Charge/Unit Type</th>
</tr>
</thead>
<tbody>
<tr th:each="savedcharges,status:${savedchargeslist}">
<!-- <td id="colhide" hidden="true">
<label th:value="${savedcharges.pkSetCharges}" th:field="*{pkSetCharges}" ></label>
</td> -->
<td>
<label th:text="${savedcharges.chargesName}" th:value="${savedcharges.chargesName}" th:field="*{tempEntSetChargesList[__${status.index}__].chargesName}"></label>
</td>
<td>
<input id="amt1" class="form-control" th:field="*{tempUnitAmount}">
</td>
<td>
<select id="societyname" class="form-control" th:field="*{tempunitType}" >
<option value="perFlat" selected="selected">perFlat</option>
<option value="perUnit">perUnit</option>
<option value="perSqrft">perSqrft</option>
</select>
</td>
</tr>
</tbody>
</table>
</div>
<button type="submit" class="btn btn-info">Submit</button>
<button type="reset" class="btn btn-warn">Reset</button>
</form>
@RestController
@GetMapping(value="/setchargeslist")
public ModelAndView doGetSetchargesList()
{
List<EntSetCharges> listCharges = new ArrayList<>();
ModelAndView respondResult = new ModelAndView("SetCharges");
try {
/*Get the set charges list*/
listCharges = serSetCharges.doGetSetChargesList();
if(listCharges!=null)
{
respondResult.addObject("savedchargeslist",listCharges);
//respondResult.addObject("tempEntSetChargesList",new EntSetCharges());
respondResult.addObject("wrpSetCharges",new WrpSetCharges());
}
else
{
respondResult.addObject("savedchargeslist",new ArrayList<>());
}
} catch (Exception e) {
// TODO: handle exception
}
return respondResult;
}
在单击提交"按钮时,它将点击/updatesetcharges
While clicking the Submit Button it will hits to /updatesetcharges
@PostMapping(value="/updatesetcharges")
public ModelAndView doUpdateSetCharges(@ModelAttribute WrpSetCharges wrpSetCharges)
{
ModelAndView respondResult = new ModelAndView();
try {
List<EntSetCharges> entSetChargesList = new ArrayList<>();
Boolean result = serSetCharges.doUpdateSetCharges(entSetChargesList);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return respondResult;
}
这是包装器类
This is a wrapper class
public class WrpSetCharges {
private List<EntSetCharges> tempEntSetChargesList = new ArrayList<>();
public List<EntSetCharges> getTempEntSetChargesList() {
return tempEntSetChargesList;
}
public void setTempEntSetChargesList(List<EntSetCharges> tempEntSetChargesList) {
this.tempEntSetChargesList = tempEntSetChargesList;
}
}
推荐答案
如注释中所述:导致异常的原因是
As mentioned in the comments: the cause of the exception is
"org.springframework.expression.spel.SpelEvaluationException:EL1025E :(位置10):集合具有'0'元素,索引'0'无效"
如果team.user的元素可以为零,则只需添加一个th:if条件.
Simply add an th:if condition if the team.users can have zero elements.
这篇关于如何使用Thymeleaf发布实体类型数据列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!