你需要一个包装对象来保存提交的数据,如下所示:
public class ClientForm { private ArrayList<String> clientList; public ArrayList<String> getClientList() { return clientList; } public void setClientList(ArrayList<String> clientList) { this.clientList = clientList; }}并将其用作
@ModelAttribute你的
processQuery方法:
@RequestMapping(value="/submitQuery", method = RequestMethod.POST)public String processQuery(@ModelAttribute ClientForm form, Model model){ System.out.println(form.getClientList());}此外,该
input元素需要a
name和a
value。如果你直接构建html,请考虑到名称必须为
clientList[i],
i该项目在列表中的位置在哪里:
<tr th:each="currentClient, stat : ${clientList}"> <td><input type="checkbox" th:name="|clientList[${stat.index}]|" th:value="${currentClient.getClientID()}" th:checked="${currentClient.selected}" /> </td> <td th:text="${currentClient.getClientID()}" ></td> <td th:text="${currentClient.getIpAddress()}"></td> <td th:text="${currentClient.getDescription()}" ></td> </tr>注意,
clientList可以包含
null在中间位置。例如,如果发布的数据为:
clientList[1] = 'B'clientList[3] = 'D'
结果
ArrayList将是:
[null, B, null, D]
更新1:
在上面的示例中,
ClientForm是的包装
List<String>。但在你的情况下
ClientWithSelectionListWrapper包含
ArrayList<ClientWithSelection>。因此,
clientList[1]应该
clientList[1].clientID与要发送回的其他属性依此类推:
<tr th:each="currentClient, stat : ${wrapper.clientList}"> <td><input type="checkbox" th:name="|clientList[${stat.index}].clientID|" th:value="${currentClient.getClientID()}" th:checked="${currentClient.selected}" /></td> <td th:text="${currentClient.getClientID()}"></td> <td th:text="${currentClient.getIpAddress()}"></td> <td th:text="${currentClient.getDescription()}"></td></tr>我已经构建了一个小演示,因此你可以对其进行测试:
应用程序
@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }ClientWithSelection.java
public class ClientWithSelection { private Boolean selected; private String clientID; private String ipAddress; private String description; public ClientWithSelection() { } public ClientWithSelection(Boolean selected, String clientID, String ipAddress, String description) { super(); this.selected = selected; this.clientID = clientID; this.ipAddress = ipAddress; this.description = description; } }ClientWithSelectionListWrapper.javapublic class ClientWithSelectionListWrapper { private ArrayList<ClientWithSelection> clientList; public ArrayList<ClientWithSelection> getClientList() { return clientList; } public void setClientList(ArrayList<ClientWithSelection> clients) { this.clientList = clients; }}TestController.java
@Controllerclass TestController { private ArrayList<ClientWithSelection> allClientsWithSelection = new ArrayList<ClientWithSelection>(); public TestController() { allClientsWithSelection.add(new ClientWithSelection(false, "1", "192.168.0.10", "Client A")); allClientsWithSelection.add(new ClientWithSelection(false, "2", "192.168.0.11", "Client B")); allClientsWithSelection.add(new ClientWithSelection(false, "3", "192.168.0.12", "Client C")); allClientsWithSelection.add(new ClientWithSelection(false, "4", "192.168.0.13", "Client D")); } @RequestMapping("/") String index(Model model) { ClientWithSelectionListWrapper wrapper = new ClientWithSelectionListWrapper(); wrapper.setClientList(allClientsWithSelection); model.addAttribute("wrapper", wrapper); return "test"; } @RequestMapping(value = "/query/submitQuery", method = RequestMethod.POST) public String processQuery(@ModelAttribute ClientWithSelectionListWrapper wrapper, Model model) { System.out.println(wrapper.getClientList() != null ? wrapper.getClientList().size() : "null list"); System.out.println("--"); model.addAttribute("wrapper", wrapper); return "test"; }}test.html
<!DOCTYPE html><html><head></head><body> <form action="#" th:action="@{/query/submitQuery}" th:object="${wrapper}" method="post"> <table > <thead> <tr> <th>Select</th> <th>Client ID</th> <th>IP Addresss</th> <th>Description</th> </tr> </thead> <tbody> <tr th:each="currentClient, stat : ${wrapper.clientList}"> <td><input type="checkbox" th:name="|clientList[${stat.index}].clientID|" th:value="${currentClient.getClientID()}" th:checked="${currentClient.selected}" /></td> <td th:text="${currentClient.getClientID()}"></td> <td th:text="${currentClient.getIpAddress()}"></td> <td th:text="${currentClient.getDescription()}"></td> </tr> </tbody> </table> <button type="submit" value="submit" >Submit</button> </form></body></html>更新1.B:
以下是使用
th:field并发送所有其他属性作为隐藏值的相同示例。
<tbody> <tr th:each="currentClient, stat : *{clientList}"> <td> <input type="checkbox" th:field="*{clientList[__${stat.index}__].selected}" /> <input type="hidden" th:field="*{clientList[__${stat.index}__].clientID}" /> <input type="hidden" th:field="*{clientList[__${stat.index}__].ipAddress}" /> <input type="hidden" th:field="*{clientList[__${stat.index}__].description}" /> </td> <td th:text="${currentClient.getClientID()}"></td> <td th:text="${currentClient.getIpAddress()}"></td> <td th:text="${currentClient.getDescription()}"></td> </tr> </tbody>


