Java 如何将对象从th:each循环传递到控制器
我有表单和相应的控制器,对象列表被传递给它们。之后,此列表将作为属性重定向到另一个方法,并显示在网站中。之后,我想通过carSearchResult.html文件中的表单将该列表中的对象传递给控制器。我试着像这里描述的那样做,但它不能正常工作。它正在向控制器发送一个对象,但属性字段和模型字段设置为空(但它们不为空) 搜索车表格Java 如何将对象从th:each循环传递到控制器,java,spring,thymeleaf,Java,Spring,Thymeleaf,我有表单和相应的控制器,对象列表被传递给它们。之后,此列表将作为属性重定向到另一个方法,并显示在网站中。之后,我想通过carSearchResult.html文件中的表单将该列表中的对象传递给控制器。我试着像这里描述的那样做,但它不能正常工作。它正在向控制器发送一个对象,但属性字段和模型字段设置为空(但它们不为空) 搜索车表格 <form action="#" th:action="@{/user/searchCar}" method="post"> <input ty
<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>
显示列表的窗体
<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>
在控制台中打印有关汽车的信息后,我收到下面的对象。它看起来像是被传递的,但参数是空白的。有人能帮我吗?非常感谢
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>
。当您在表单中使用它时,它希望它解析为模型上的单个变量。您不能将它与临时变量一起使用(如{$car}
——它不在模型上,它只是由th:each
创建的临时变量),因为该属性不是为这样工作而构建的
th:field
同时生成value
和name
标记,但它必须在与th:object
相同的限制范围内工作。手动添加name
和value
属性之所以有效,是因为它们恰好符合spring在文章中的预期,并且它能够基于名称创建对象
至于如何解决原来的问题。。。我认为你有两个选择:
列表属性为Car
s。这样,您就可以使用th:object
和th:field
属性。您必须有“提交”按钮才能提供正在编辑的汽车
您不能将对象从thymeleaf传递到控制器。仅从控制器到thymeleaf。您可以通过POST调用从HTML表单向控制器发送内容。你们能澄清一下你们的问题吗?但通过@modeldattribute注释,我收到了来自参数的对象,这些参数是通过POST调用发送的?好的,我想现在对我来说有点清楚了。总而言之:
-之所以有效,是因为我们将这个临时变量{$car}
分配给它的生产者字段?这就是为什么Spring能够创建汽车对象的原因?
@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]
<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>