如何将对象列表与百里香咖啡绑定?

我在将表单POS回控制器时遇到了很多困难,控制器应该只包含用户可以编辑的对象数组列表。

表单正确加载,但是当它发布时,它似乎从未真正发布任何内容。

这是我的表格:

<form action="#" th:action="@{/query/submitQuery}" th:object="${clientList}" method="post">

<table class="table table-bordered table-hover table-striped">
<thead>
    <tr>
        <th>Select</th>
        <th>Client ID</th>
        <th>IP Addresss</th>
        <th>Description</th>            
   </tr>
 </thead>
 <tbody>     
     <tr th:each="currentClient, stat : ${clientList}">         
         <td><input type="checkbox" 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" class="btn btn-success">Submit</button>
  </form>

上面工作正常,它正确地加载了列表。但是,当我开机自检时,它返回一个空对象(大小为0)。我相信这是由于缺乏,但无论如何这里是控制器POST方法:th:field

...
private List<ClientWithSelection> allClientsWithSelection = new ArrayList<ClientWithSelection>();
//GET method
...
model.addAttribute("clientList", allClientsWithSelection)
....
//POST method
@RequestMapping(value="/submitQuery", method = RequestMethod.POST)
public String processQuery(@ModelAttribute(value="clientList") ArrayList clientList, Model model){
    //clientList== 0 in size
    ...
}

我尝试过添加一个,但无论我做什么,它都会导致异常。th:field

我试过了:

...
<tr th:each="currentClient, stat : ${clientList}">   
     <td><input type="checkbox" th:checked="${currentClient.selected}"  th:field="*{}" /></td>

    <td th th:field="*{currentClient.selected}" ></td>
...

我无法访问currentClient(编译错误),我甚至不能选择 clientList,它给了我像 ,等选项,所以它应该有一个数组,但是,我不能传入数组。get()add()clearAll()

我也尝试过使用类似的东西,这会导致运行时异常th:field=${}

我试过了

th:field = "*{clientList[__currentClient.clientID__]}" 

但也编译错误。

有什么想法吗?


更新 1:

托比亚斯建议我需要把我的清单包装成一个捕食者。这就是我所做的:

ClientWithSelectionWrapper:

public class ClientWithSelectionListWrapper {

private ArrayList<ClientWithSelection> clientList;

public List<ClientWithSelection> getClientList(){
    return clientList;
}

public void setClientList(ArrayList<ClientWithSelection> clients){
    this.clientList = clients;
}
}

我的页面:

<form action="#" th:action="@{/query/submitQuery}" th:object="${wrapper}" method="post">
....
 <tr th:each="currentClient, stat : ${wrapper.clientList}">
     <td th:text="${stat}"></td>
     <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>

以上负载精细:enter image description here

然后我的控制器:

@RequestMapping(value="/submitQuery", method = RequestMethod.POST)
public String processQuery(@ModelAttribute ClientWithSelectionListWrapper wrapper, Model model){
... 
}

页面正确加载,数据按预期显示。如果我在没有任何选择的情况下发布表单,我得到这个:

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'clientList' cannot be found on null

不知道为什么在抱怨

(在GET方法中,它具有:model.addAttribute("wrapper", wrapper);)

enter image description here

如果我随后进行选择,即勾选第一个条目:

There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='clientWithSelectionListWrapper'. Error count: 1

我猜我的POST控制器没有获得客户端WithSelectionListWrapper。不知道为什么,因为我已经设置了包装器对象通过FORM标头回发。th:object="wrapper"


更新 2:

我取得了一些进展!最后,控制器中的POST方法正在拾取提交的表单。但是,所有属性似乎都为 null,但项目是否已勾选。我做了各种更改,这就是它的外观:

<form action="#" th:action="@{/query/submitQuery}" th:object="${wrapper}" method="post">
....
 <tr th:each="currentClient, stat : ${clientList}">
     <td th:text="${stat}"></td>
     <td>
         <input type="checkbox"
                th:name="|clientList[${stat.index}]|"
                th:value="${currentClient.getClientID()}"
                th:checked="${currentClient.selected}"
                th:field="*{clientList[__${stat.index}__].selected}">
     </td>
     <td th:text="${currentClient.getClientID()}"
         th:field="*{clientList[__${stat.index}__].clientID}"
         th:value="${currentClient.getClientID()}"
     ></td>
     <td th:text="${currentClient.getIpAddress()}"
         th:field="*{clientList[__${stat.index}__].ipAddress}"
         th:value="${currentClient.getIpAddress()}"
     ></td>
     <td th:text="${currentClient.getDescription()}"
         th:field="*{clientList[__${stat.index}__].description}"
         th:value="${currentClient.getDescription()}"
     ></td>
     </tr>

我还向我的包装类添加了一个默认的无参数构造函数,并向 POST 方法添加了一个参数(不确定是否需要)。bindingResult

public String processQuery(@ModelAttribute ClientWithSelectionListWrapper wrapper, BindingResult bindingResult, Model model)

因此,当发布对象时,它的外观如下:enter image description here

当然,systemInfo 应该是空的(在这个阶段),但是 clientID 总是 0,而 ipAddress/Description 总是空的。所选布尔值对于所有属性都是正确的。我确信我在某个地方的某个属性上犯了一个错误。回到调查。


更新 3:

好的,我已经设法正确填写了所有值!但是我不得不改变我的,以包括一个这不是我想要的...尽管如此,这些值正在正确填充,这表明spring寻找输入标记可能是用于数据映射?td<input />

以下是我如何更改 clientID 表数据的示例:

<td>
 <input type="text" readonly="readonly"                                                          
     th:name="|clientList[${stat.index}]|"
     th:value="${currentClient.getClientID()}"
     th:field="*{clientList[__${stat.index}__].clientID}"
  />
</td>

现在我需要弄清楚如何将其显示为纯数据,理想情况下没有任何输入框...


答案 1

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

public class ClientForm {
    private ArrayList<String> clientList;

    public ArrayList<String> getClientList() {
        return clientList;
    }

    public void setClientList(ArrayList<String> clientList) {
        this.clientList = clientList;
    }
}

并将其用作方法中@ModelAttributeprocessQuery

@RequestMapping(value="/submitQuery", method = RequestMethod.POST)
public String processQuery(@ModelAttribute ClientForm form, Model model){
    System.out.println(form.getClientList());
}

此外,该元素需要 a 和 a。如果您直接构建 html,则考虑到名称必须是 ,其中是列表中项目的位置:inputnamevalueclientList[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>

请注意,可以包含在中间位置。例如,如果发布的数据为:clientListnull

clientList[1] = 'B'
clientList[3] = 'D'

结果将是:ArrayList[null, B, null, D]

更新 1:

在我上面的示例中,是 .但在您的案例中包含 .因此,应该与您要发回的其他属性一起依此类推:ClientFormList<String>ClientWithSelectionListWrapperArrayList<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>

我已经构建了一个小演示,所以你可以测试它:

应用.java

@SpringBootApplication
public class Application {      
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }       
}

客户端选择.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;
   }

   /* Getters and setters ... */
}

ClientWithSelectionListWrapper.java

public class ClientWithSelectionListWrapper {

   private ArrayList<ClientWithSelection> clientList;

   public ArrayList<ClientWithSelection> getClientList() {
      return clientList;
   }
   public void setClientList(ArrayList<ClientWithSelection> clients) {
      this.clientList = clients;
   }
}

测试控制器.java

@Controller
class TestController {

   private ArrayList<ClientWithSelection> allClientsWithSelection = new ArrayList<ClientWithSelection>();

   public TestController() {
      /* Dummy data */
      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";
   }
}

测试.html

<!DOCTYPE html>
<html>
<head></head>
<body>
   <form action="#" th:action="@{/query/submitQuery}" th:object="${wrapper}" method="post">

      <table class="table table-bordered table-hover table-striped">
         <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" class="btn btn-success">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>

答案 2

当您想要在百里香中选择对象时,您实际上不需要创建包装器来存储选择字段。按照百里香指南的语法使用适用于想要访问集合中已存在的一组对象的情况。它并不是真正设计用于通过使用包装器对象IMO进行选择而设计的,因为它会创建不必要的样板代码,并且是一种黑客攻击。booleandynamic fieldsth:field="*{rows[__${rowStat.index}__].variety}"

考虑这个简单的例子,一个可以选择他们喜欢的例子。注意:为清楚起见,省略了构造函数、Getter 和 setter。此外,这些对象通常存储在数据库中,但我在内存数组中使用来解释这个概念。PersonDrinks

public class Person {
    private Long id;
    private List<Drink> drinks;
}

public class Drink {
    private Long id;
    private String name;
}

弹簧控制器

这里最主要的是,我们将 存储在 中,以便我们可以将其绑定到 中的形式。其次,一个人可以在UI上选择的饮料。PersonModelth:objectselectableDrinks

   @GetMapping("/drinks")
   public String getDrinks(Model model) {
        Person person = new Person(30L);

        // ud normally get these from the database.
        List<Drink> selectableDrinks = Arrays.asList(
                new Drink(1L, "coke"),
                new Drink(2L, "fanta"),
                new Drink(3L, "sprite")
        );

        model.addAttribute("person", person);
        model.addAttribute("selectableDrinks", selectableDrinks);

        return "templates/drinks";
    }

    @PostMapping("/drinks")
    public String postDrinks(@ModelAttribute("person") Person person) {           
        // person.drinks will contain only the selected drinks
        System.out.println(person);
        return "templates/drinks";
    }

模板代码

密切关注循环以及如何用于获得所有可以选择的饮料。liselectableDrinks

复选框实际上扩展到因为绑定到并且只是引用对象上的属性的快捷方式。您可以将其视为只是告诉春季/百里香叶,任何选定的内容都将放入 at 位置 。th:fieldperson.drinksth:objectPerson*{drinks}PersonDrinksArrayListperson.drinks

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" >
<body>

<div class="ui top attached segment">
    <div class="ui top attached label">Drink demo</div>

    <form class="ui form" th:action="@{/drinks}" method="post" th:object="${person}">
        <ul>
            <li th:each="drink : ${selectableDrinks}">
                <div class="ui checkbox">
                    <input type="checkbox" th:field="*{drinks}" th:value="${drink.id}">
                    <label th:text="${drink.name}"></label>
                </div>
            </li>
        </ul>

        <div class="field">
            <button class="ui button" type="submit">Submit</button>
        </div>
    </form>
</div>
</body>
</html>

无论如何。。。秘诀是使用.这依赖于弹簧转换器。当表单发布时,spring将尝试重新创建一个,为此,它需要知道如何将任何选定的字符串转换为实际类型。注意:如果你在复选框中做了键,html将是一个不是你想要的,因此需要使用id!如果您正在继续学习,那么您需要做的就是创建自己的转换器(如果尚未创建)。th:value=${drinks.id}Persondrink.idDrinkth:value${drinks}valuetoString()Drink

如果没有转换器,您将收到类似这样的错误Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'drinks'

您可以打开登录以详细查看错误。application.propertieslogging.level.org.springframework.web=TRACE

这只是意味着spring不知道如何将表示a的字符串id转换为.以下是解决此问题的示例。通常,您会在获取数据库访问权限中注入存储库。drink.idDrinkConverter

@Component
public class DrinkConverter implements Converter<String, Drink> {
    @Override
    public Drink convert(String id) {
        System.out.println("Trying to convert id=" + id + " into a drink");

        int parsedId = Integer.parseInt(id);
        List<Drink> selectableDrinks = Arrays.asList(
                new Drink(1L, "coke"),
                new Drink(2L, "fanta"),
                new Drink(3L, "sprite")
        );
        int index = parsedId - 1;
        return selectableDrinks.get(index);
    }
}

如果一个实体有一个相应的spring数据存储库,spring会自动创建转换器,并在提供id时处理获取实体(字符串id似乎也很好,所以spring通过外观在那里做了一些额外的转换)。这真的很酷,但一开始可能会令人困惑。


推荐