【问题标题】:How to post a List of Entity type data using Thymeleaf?如何使用 Thymeleaf 发布实体类型数据列表?
【发布时间】:2018-05-05 23:57:30
【问题描述】:

我的项目基于spring boot、Thymeleaf、mysql、html和Jquery。

场景 我的场景是使用 Thymeleaf 发布 EntSetCharges 数据列表并点击 Spring boot @RestController。 以前我处理过这个错误Error 1: Error 2: 现在又出现了一个错误

2017-11-22 14:58:19.717 错误 1420 --- [nio-8080-exec-1] oaccC[.[.[/].[dispatcherServlet]:Servlet.service() 用于 servlet [dispatcherServlet]在路径 [] 的上下文中抛出异常 [请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式的异常:“tempEntSetChargesList[0].chargesName”(模板:“SetCharges” - 第 37 行,第 91 列)] 根本原因 org.springframework.expression.spel.SpelEvaluationException: EL1025E: 集合有“0”个元素,索引“0”无效

该行表示 HTML 代码是表格的第一列

th:field="*{tempEntSetChargesList[__${status.index}__].chargesName}"

我仍然没有成功将数据发布到控制器..请帮帮我..

这里是完整代码

<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

1St 命中localhost:8080/setchargeslist

@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

@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;
    }

这是一个包装类

 public class WrpSetCharges {

        private List<EntSetCharges> tempEntSetChargesList = new ArrayList<>();

        public List<EntSetCharges> getTempEntSetChargesList() {
            return tempEntSetChargesList;
        }

        public void setTempEntSetChargesList(List<EntSetCharges> tempEntSetChargesList) {
            this.tempEntSetChargesList = tempEntSetChargesList;
        }
      }

【问题讨论】:

  • 有一个类似的例子stackoverflow.com/questions/36500731/…。尝试在您的帖子表单中使用包装器。即 。就像在示例中一样
  • 我试过了,伙计,但仍然遇到错误..我不知道我的代码在哪里发生了..即使按照语法很难 th:feild..但不起作用..请帮助我
  • 你也可以查看我在这篇文章中给出的错误链接...
  • 我看不到 tempEntSetChargesList 的初始化位置?用户在 th:field 中保存了费用列表
  • 你可以看到我的代码@GetMapping(value="/setchargeslist"),它是 tempEntSetChargesList 已注释但包装类已初始化..包装类内部有 tempEntSetChargesList

标签: hibernate spring-boot spring-data-jpa thymeleaf


【解决方案1】:

在cmets中提到:异常的原因是

“org.springframework.expression.spel.SpelEvaluationException‌​: EL1025E:(pos 10): 集合有'0'元素,索引'0'无效”

如果 team.users 可以有零个元素,只需添加一个 th:if 条件。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签