本文介绍了如何使用Thymeleaf发布实体类型数据列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景我的场景是使用Thyemeleaf发布EntSetCharges数据列表并点击Spring boot @RestController.以前,我曾处理此错误错误1:错误2:Rightnow遇到另一个错误

ScenarioMy 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

该行表示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发布实体类型数据列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:58