I have form and corresponding controller to which list of objects is passed. After that this list is redirected as an attribute to another method and displayed in a website. After that I would like to pass object from this list via form in a carSearchResult.html file to controller. I was trying to do like it is described here Spring and Thymeleaf: Sending an object to a controller from a th:each table but it does not work properly. It is sending an object to controller but with fields property and model set as blank (but they are not null).
searchCar form
<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> Controller method for 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"; } Method where list is redirected
@GetMapping("/user/carSearchResults") public String showCarSearchResults(Model model) { model.addAttribute("car", new Car()); return "user/carSearchResults"; } Form where list is displayed
<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> Method to which I want to send object from th:each loop
@PostMapping("/user/showCarDetails") public String showCarDetails(@ModelAttribute Car car) { System.out.println(car); return "user/showCarDetails"; } After printing information about car in console I receive object below. It looks like it is passed but with parameters as a blank space. Can anyone help me? Many thanks.
Car [id=null, producer=, model=, seatsNumber=null, type=null, registrationNumber=null, dailyPrice=null, description=null] EDIT
I have changed the form. Option with th:attr and th:value works but I do not really understand why th:field does not work and what is the difference between them. As I understand from documentation th:value is some kind of older version of 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>