如何将对象从 th:each 循环传递到控制器



我有表单和相应的控制器,对象列表被传递给它。之后,此列表将作为属性重定向到另一种方法并显示在网站中。之后,我想通过 carSearchResult.html 文件中的表单将此列表中的对象传递给控制器。我试图像这里描述的那样做 春天和百里香叶:从 th:each 表将对象发送到控制器,但它无法正常工作。它正在将对象发送到控制器,但字段属性和模型设置为空白(但它们不为空)。

搜索汽车表格

<form action="#" th:action="@{/user/searchCar}" method="post">
<input type="radio" name="type" value="hatch" /> Hatchback <br/>
<input type="radio" name="type" value="sedan" /> Sedan <br/>
<input type="radio" name="type" value="combi" /> Kombi <br/>
<input type="radio" name="type" value="sport" /> Sport <br/>
<input type="radio" name="type" value="cabrio" /> Cabrio <br/>
<input type="text" name="dailyPrice" placeholder="Price" /> <br/>
<input type="date" name="dateOfRent" placeholder="Date of rent" /> <br/>
<input type="date" name="dateOfReturn" placeholder="Date of return" /> <br/>
<input type="submit" value="Show" />
</form>

用于搜索汽车发布请求的控制器方法

@PostMapping("/user/searchCar")
public String searchCar(Model model, @RequestParam String type, @RequestParam double dailyPrice,
@RequestParam Date dateOfRent, @RequestParam Date dateOfReturn, RedirectAttributes redirectAttr) {
List<Car> allCarsList = carDao.getAllCars(type, dailyPrice, dateOfRent, dateOfReturn);
redirectAttr.addFlashAttribute("carList", allCarsList);
return "redirect:/user/carSearchResults";
}

重定向列表的方法

@GetMapping("/user/carSearchResults")
public String showCarSearchResults(Model model) {
model.addAttribute("car", new Car());
return "user/carSearchResults";
}

显示列表的表单

<div th:each="car: ${carList}">
<h3 th:text="${car.producer}"></h3>
<h3 th:text="${car.model}"></h3>
<form action="#" th:action="@{/user/showCarDetails}" method="post" th:object="${car}">
<input type="hidden" th:field="*{producer}" /> <br/>            
<input type="hidden" th:field="*{model}" /> <br/>
<input type="submit" value="Show" />
</form>
</div> 

我想从 th:each 循环发送对象的方法

@PostMapping("/user/showCarDetails")
public String showCarDetails(@ModelAttribute Car car) {
System.out.println(car);
return "user/showCarDetails";
}

在控制台中打印有关汽车的信息后,我收到下面的对象。它看起来像是传递的,但参数作为空格。谁能帮我?非常感谢。

Car [id=null, producer=, model=, seatsNumber=null, type=null, registrationNumber=null, dailyPrice=null, description=null]

编辑

我已经更改了表格。带有 th:attr 和 th:value 的选项有效,但我真的不明白为什么 th:field 不起作用以及它们之间有什么区别。正如我从文档中了解到的那样,th:value 是 th:field 的某种旧版本?

<div th:each="car: ${carList}" th:object="${car}">
<h3 th:text="*{producer}"></h3>
<h3 th:text="*{model}"></h3>
<form action="#" th:action="@{/user/showCarDetails}" method="post">
<input type="hidden" th:value="*{producer}" name="producer" /> <br/>       <!-- It works -->
<input type="hidden" th:attr="value=*{producer}" name="producer" /> <br/>  <!-- It works -->        
<input type="hidden" th:field="*{producer}" />                             <!-- It does not work -->
<input type="submit" value="Pokaż" />
</form>
</div> 
th:object

当你在<form>上使用它时,与在其他地方使用它时,它的工作方式不同。 当您在窗体中使用它时,它希望它解析为模型上的单个变量。 您不能将其与临时变量(如{$car}- 它不在模型上,它只是由th:each创建的临时变量)一起使用,因为该属性不是为那样工作的

th:field生成valuename标签 - 但同样,它必须在与th:object相同的限制内工作。 手动添加namevalue属性之所以有效,是因为它们恰好与 spring 在帖子中的期望一致,并且它能够根据名称创建对象。

至于原来的问题怎么解决...我认为您有两种选择:

  1. 创建一个ListCars 作为属性的命令对象。 这样,您就可以使用th:objectth:field属性。 您必须具有提交按钮才能提供正在编辑的汽车。

  2. 继续像以前一样手动创建名称和值字段。

最新更新