Thymeleaf表单与ArrayList对象一起提交

编程入门 行业动态 更新时间:2024-10-25 10:29:53
本文介绍了Thymeleaf表单与ArrayList对象一起提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经编写了一个简单的程序,用于使用数据(ArrayList)进行表单提交,以便将数据从表发送到控制器类.

I have written a simple program for form submit with the data(ArrayList) to send from table to controller class.

提交表单时,数据始终为空,不确定我在这里做错了什么.

While submitting the form the data is always empty not sure what I am doing wrong here.

我几乎要花很多时间来确定问题,否则就不行了:(

I am almost spending a lot of time to identify the issue no luck :(

控制器类(我在Post方法中总是得到null的地方)

Controller Class ( Where I always getting null in Post method )

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)

HTML Form:(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>

推荐答案

<td />元素未与表单一起提交.您需要使用某种输入.它应该看起来像这样:

<td /> elements aren't submitted with a form. You need to use some kind of input. It should look something like this:

<td class="table-data"> <input type="text" th:field="*{accountList[__${stat.index}__].accountnumber}" /> </td>

或者如果您要提交而没有看到字段为可编辑状态,则类似

or if you want to submit without seeing the fields as editable, something like this

<td class="table-data"> <span th:text="${account.accountnumber}" /> <input type="hidden" th:field="*{accountList[__${stat.index}__].accountnumber}" /> </td>

更多推荐

Thymeleaf表单与ArrayList对象一起提交

本文发布于:2023-10-09 18:06:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   对象   Thymeleaf   ArrayList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!