Issue
Using - Spring Boot, Thymeleaf
I have following code under controller class:
public class HotelController {
private HotelService hotelService;
private HotelRepository hotelRepository;
@Autowired
public HotelController(HotelService hotelService,HotelRepository hotelRepository) {
this.hotelService = hotelService;
this.hotelRepository = hotelRepository;
}
@GetMapping("/index")
public String searchHotel(Model model) {
Hotel hotel = new Hotel();
model.addAttribute("searchObject",hotel);
model.addAttribute("cityList", hotelRepository.findListOfCities());
model.addAttribute("hotelList", hotelRepository.findListOfHotels());
return "search_hotel";
}
@PostMapping("/availabilityResult")
public String afterClickingSearch(@ModelAttribute("searchObject") Hotel hotel,Model model) {
if(hotelService.searchHotelResult(hotel)==null)
return "failure_page";
else {
model.addAttribute("hotelRoom_TypePriceGST",hotelService.findHotelPriceandRoomType(hotel.getCity(),hotel.getHotel()).split(","));
model.addAttribute("total", hotelService.findTotal(hotel.getCity(), hotel.getHotel()));
return "booking_confirmation";
}
}
@GetMapping("/navUserRegisterPage")
public String userRegisterPage(Model model){
User user = new User();
model.addAttribute("userObj", user);
return "user_registerForm";
}
@PostMapping("/reserveUser")
public String reserveUserHandler(@ModelAttribute("userObj") User user,@ModelAttribute("searchObject") Hotel hotel,Model model) {
System.out.println(hotel.getHotel());
System.out.println(user.getGuest_name());
model.addAttribute("hotelName", hotel.getHotel());
model.addAttribute("userName", user.getGuest_name());
return "confirmation_page";
}
}
I can find hotel value(usig hotel.getHotel()) under handler method- public String afterClickingSearch()
But when I try to find the hotel value(using hotel.getHotel()) under handler method -reserveUserHandler(); I get null as value.
Please help with how can I retrieve hotel value under reserveUserHandler() method.
Solution
It appears as though you have a Hotel backed form on the "search_hotel" page and the action goes to afterClickingSearch()
. You can access the Hotel model attribute in afterClickingSearch()
because you are submitting the attributes of the Hotel in your form. When you transition to "booking_confirmation" and other pages it's not apparent that you are passing the hotel's id to maintain state.
You have a few options, some more elegant than others. You can pass around the hotel's id via a request parameter. A second option is to include a hidden Hotel id in all forms from which reserveUserHandler()
is reachable, making sure each form is initialized with the id, and lookup the hotel by id in reserveUserHandler()
. A third option, which I wouldn't recommend, is to make Hotel or Hotel id a session attribute. See this for more information on session attributes. When you use session attributes you need to be wary of multiple browser tabs and how unique state is maintained across each tab.
Answered By - Lee Greiner