如何使用Thymeleaf发布实体类型数据列表?

时间:2017-11-22 09:50:06

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

  

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

方案 我的场景是使用Thyemeleaf POST一个EntSetCharges数据列表并点击Spring引导@RestController。 以前我使用过这个错误Error 1: Error 2: Rightnow有另一个错误

  

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

行表示HTML代码是表格的第1列

个:字段=" * {。tempEntSetChargesList [__ $ {status.index} __] chargesName}"

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

This is HTML page 以下是完整代码



<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>
&#13;
&#13;
&#13;

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

1 个答案:

答案 0 :(得分:0)

如评论中所述:异常的原因是

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

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