如何从th传递对象:每个循环到控制器

时间:2017-05-22 19:26:01

标签: java spring thymeleaf

我有表单和相应的控制器,传递对象列表。之后,此列表将作为属性重定向到另一个方法并显示在网站中。之后,我想通过carSearchResult.html文件中的表单将对象从此列表传递给控制器​​。我试图这样做就像Spring and Thymeleaf: Sending an object to a controller from a th:each table所描述的那样,但它无法正常工作。它正在向控制器发送一个对象,但是将fields属性和模型设置为空白(但它们不为空)。

searchCar表单

<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>

searchCar post request的控制器方法

@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> 

我想从中发送对象的方法:每个循环

@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:值有效,但我不明白为什么th:字段不起作用,它们之间有什么区别。正如我从文档中所理解的那样:值是某种旧版本的字段:

<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> 

1 个答案:

答案 0 :(得分:1)

th:object works differently when you use it on a <form>, vs. when you use it other places。当您在表单中使用它时,它希望它解析为模型上的单个变量。你不能将它与临时变量一起使用(比如{$car} - 它不在模型上,它只是由th:each创建的一个临时变量),因为该属性不是为了这样工作而构建的

th:field同时生成valuename标记 - 但同样,它必须在与th:object相同的限制范围内工作。手动添加namevalue属性的原因是,因为它们碰巧与帖子中的弹簧一致,并且能够根据名称创建对象。

至于如何解决原始问题......我认为你有两个选择:

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

  2. 继续手动创建您已经存在的名称和值字段。

相关问题