Thymeleaf 表单使用 ArrayList 对象提交
Posted
技术标签:
【中文标题】Thymeleaf 表单使用 ArrayList 对象提交【英文标题】:Thymeleaf form submit with ArrayList Object 【发布时间】:2019-11-28 20:07:09 【问题描述】:我编写了一个简单的表单提交程序,将数据(ArrayList)从表发送到控制器类。
提交表单时,数据总是为空,不知道我在这里做错了什么。
我几乎花了很多时间来确定问题没有运气:(
Controller 类(我在 Post 方法中总是得到 null)
public class AccountContoller
private ArrayList<AccountwithSelection> allAccountwithSelect = new ArrayList<AccountwithSelection>();
public AccountContoller()
//Written some test data in Array
AccountwithSelection accountwithSelection1 = new AccountwithSelection();
accountwithSelection1.setAccountnumber("Acct1");
accountwithSelection1.setIlc("ILC1");
allAccountwithSelect.add(accountwithSelection1);
AccountwithSelection accountwithSelection2 = new AccountwithSelection();
accountwithSelection2.setAccountnumber("Acct2");
accountwithSelection1.setIlc("ILC2");
allAccountwithSelect.add(accountwithSelection2);
@RequestMapping(value = "/accountload", method = RequestMethod.GET)
String accountload(Model model)
AccountSelectionListWrapper wrapper = new AccountSelectionListWrapper();
wrapper.setAccountList(allAccountwithSelect);
model.addAttribute("accountload", wrapper);
return "accountload";
@RequestMapping(value = "/accountload", method = RequestMethod.POST)
public String addimeiPost(Model model,
@ModelAttribute("accountload") AccountSelectionListWrapper wrapper,
HttpServletRequest request)
System.out.println(wrapper.getAccountList()); //Always getting null, why ?
return "accountload";
类:AccountwithSelection
public class AccountwithSelection
public String accountnumber, ilc;
public String getAccountnumber()
return accountnumber;
public void setAccountnumber(String accountnumber)
this.accountnumber = accountnumber;
public String getIlc()
return ilc;
public void setIlc(String ilc)
this.ilc = ilc;
WrapperClass-AccountSelectionListWrapper
public class AccountSelectionListWrapper
public ArrayList<AccountwithSelection> accountList;
public ArrayList<AccountwithSelection> getAccountList()
return accountList;
public void setAccountList(ArrayList<AccountwithSelection> accountList)
this.accountList = accountList;
HTML 表单:(accountload.html)
<form action="#" th:action="accountload" th:object="$accountload" method="post">
<div class="row">
<div class=form-group-1>
<input type="submit" value="Send Data" name="action">
</div>
</div>
<table id="mytable" class="table">
<tbody class="table-tbody" style="width: 90%">
<tr class="table-head">
<th>ACCOUNT NUMBER</th>
</tr>
<tr class="table-row">
<tr class="table-row" th:each="account, stat : *accountList">
<td class="table-data" th:text="$account.getAccountnumber()"
th:field="*accountList[__$stat.index__].accountnumber"
th:value="$account.getAccountnumber()"></td>
</tr>
</tbody>
</table>
</form>
【问题讨论】:
【参考方案1】:<td />
元素未与表单一起提交。您需要使用某种输入。它应该看起来像这样:
<td class="table-data">
<input type="text" th:field="*accountList[__$stat.index__].accountnumber" />
</td>
或者如果您想提交而不看到字段可编辑,类似这样
<td class="table-data">
<span th:text="$account.accountnumber" />
<input type="hidden" th:field="*accountList[__$stat.index__].accountnumber" />
</td>
【讨论】:
以上是关于Thymeleaf 表单使用 ArrayList 对象提交的主要内容,如果未能解决你的问题,请参考以下文章
在模态表单中使用 Spring Boot 和 thymeleaf 进行服务器端验证
如何使用 Bootstrap 和 thymeleaf 在模态内部的表单中填充值?
Spring Boot、Spring Security 和 Thymeleaf:将 CsrfFilter 应用到带有表单的网站