栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将对象列表与thymeleaf绑定?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将对象列表与thymeleaf绑定?

你需要一个包装对象来保存提交的数据,如下所示:

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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/420438.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号