Thymeleaf th:字段在<label>标签中不接受,它不显示数据

时间:2017-12-22 07:27:49

标签: html spring-boot thymeleaf

在我使用Spring启动和Thymeleaf的项目中,我尝试以表格格式发布值列表,

1)我制作实体清单

List<EntFlatIncome> flatIncomeList

2)设置为Wrapper类,

WrpFlatIncome wrpFlatIncome = new WrpFlatIncome();
            wrpFlatIncome.setFlatIncomeList(flatIncomeList);

3)将ModelAndView撤回到&#34; ApproveBillList&#34;包含列表数据的HTML页面

ModelAndView modelView = new ModelAndView("ApproveBillList");
modelView.addObject("wrpflatIncome", wrpFlatIncome);

在该HTML页面中我显示值标签而不是输入框,因为数据 IsApproved 布尔值只需更新其他列数据不需要用户修改 https://support.google.com/mail/answer/78754

当我尝试使用Thymeleaf提交数据时:对象只有已批准 数据只有其他列(PaidAmount,paidDate,paymentMode,transactionId,BillAmount)值只有NULL,

在我以前编码的标签中

<td>
       <label th:field="*{flatIncomeList[__${status.index}__].billAmount}"
      th:text="*{flatIncomeList[__${status.index}__].billAmount}"></label>
      </td>

如果我输入框 th:field 禁用 attibute意味着它也只传递NULL,请帮帮我..谢谢

这是完整的HTML代码

&#13;
&#13;
<div class="panel-body">
		<!-- Body Content Starts -->
		<form id="billformid" action="#"  method="post" th:action="@{/updatebill}" th:object="${wrpflatIncome}">
		
		<div class="table-responsive table-responsive-md"> 
	
	<table class="table table-hover">
	<thead>
	    <tr>
	  <th>isApproved</th>   
      <th>paidAmount</th>
      <th>paidDate</th>
      <th>paymentMode</th>
      <th>transactionId</th>
      <th>Bill Amount</th>
      
    </tr>
    </thead>
    
    <tbody>
    <tr th:each="flatIncomeList,status:*{flatIncomeList}" 
    th:if="${not #lists.isEmpty(wrpflatIncome.flatIncomeList)}">
    
       <td hidden="true">
      <input class="form-control" type="text"
       th:field="*{flatIncomeList[__${status.index}__].billId}"/>
      </td> 
      
      
       <td>
      <input type="checkbox" class="form-check-input"  
       th:field="*{flatIncomeList[__${status.index}__].isApproved}"/>
      </td> 
      
      <td th:field="*{flatIncomeList[__${status.index}__].paidAmount}">
      <label 
      th:text="*{flatIncomeList[__${status.index}__].paidAmount}"></label>
      </td>
      
      <td th:field="*{flatIncomeList[__${status.index}__].paidDate}">
       <label 
      th:text="*{flatIncomeList[__${status.index}__].paidDate}"></label>
      </td> 
      
      <td th:field="*{flatIncomeList[__${status.index}__].paymentMode}">
       <label 
      th:text="*{flatIncomeList[__${status.index}__].paymentMode}"></label>
      </td> 
      
      <td th:field="*{flatIncomeList[__${status.index}__].transactionId}">
       <label 
      th:text="*{flatIncomeList[__${status.index}__].transactionId}"></label>
      </td> 
      
       <td>
       <label th:field="*{flatIncomeList[__${status.index}__].billAmount}"
      th:text="*{flatIncomeList[__${status.index}__].billAmount}"></label>
      </td> 
      
      <!-- version not shown in html -->
        <td hidden="true">
      <input class="form-control" type="text"
       th:field="*{flatIncomeList[__${status.index}__].version}"/>
      </td> 
    </tr>
    </tbody>
    
	</table>
	</div>
	
	<div class="text-center">
	<button type="submit" class="btn btn-primary">Submit</button>
	<button type="reset" class="btn btn-danger">Reset</button>
	</div>
		
		
		
		</form>
		<!-- Body Content Ends -->
			</div>
&#13;
&#13;
&#13;

@Controller

@PostMapping("/updatebill")
public ModelAndView doUpdateBillApproveStatus(@ModelAttribute WrpFlatIncome wrpflatIncome) 
{
    ModelAndView modelView = new ModelAndView();
    try {
        if(wrpflatIncome.getFlatIncomeList()!=null) {

            //Boolean result = serbillSave.doUpdateApprovedBillList(wrpflatIncome.getFlatIncomeList());
        }

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return modelView;
}

2 个答案:

答案 0 :(得分:0)

您不应在th:fieldlabel上使用td属性。它不会按预期工作;浏览器不会提交这些元素的任何数据。

相反,您应该使用input。它可以是只读的:

<input type="text" th:field="*{myField}" readonly="readonly"/>

或隐藏:

<input type="hidden" th:field="*{myField}"/>

答案 1 :(得分:0)

  

我不知道这是正确的答案,但它对我有用

, 我用td

显示 th:text
<td th:text="*{flatIncomeList[__${status.index}__].paidAmount}"> </td>

使用隐藏的属性

执行我在另一个td中放置的数据
<td hidden="true">
      <input class="form-control" type="text" 
      th:field="*{flatIncomeList[__${status.index}__].paidAmount}"></input>
      </td>

所以 td hidden =&#34; true&#34; 不会在表格显示中显示,但它可以将数据保存在 th:field

这里的代码看起来像

 <td th:text="*{flatIncomeList[__${status.index}__].paidAmount}"> </td>
  <td hidden="true">
  <input class="form-control" type="text" 
  th:field="*{flatIncomeList[__${status.index}__].paidAmount}"></input>
  </td>

  <td th:text="*{flatIncomeList[__${status.index}__].paidDate}"></td>
  <td hidden="true">
   <input class="form-control" type="text"
  th:field ="*{flatIncomeList[__${status.index}__].paidDate}"></input>
  </td>