Issue
I have 2 tables, city and hotel_details in my database. I am trying to fetch the data from these tables and populating inside a form for registering the customer. But I am getting "java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute" as error.
JSP file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <head> <title>Search Hotels</title> </head> <body> <h4>Search Hotels</h4> <form:form action="search"> <table> <tr> <td>City:</td> <td> <form:select path="cities"> <form:options items="${cities}" /> </form:select> </td> </tr> <tr> <td>Hotel:</td> <td> <form:select path="hotels"> <form:options items="${hotels}" /> </form:select> </td> </tr> <tr> <td>Date:</td> <td> <input type="date" id="date" name="date"> </td> </tr> <tr> <td colspan="3"> <input type="submit" value="Check Availability"> </td> </tr> </table> </form:form>
- Controller
@Controller public class HomeController {
//need a controller method to show the initial HTML form
@Autowired(required=true)
private CityDAO cityDAO;
@Autowired(required=true)
private HotelDetailsDAO hotelDetailsDAO;
@RequestMapping("/")
public String showCheckAvailablityForm(Model theModel) {
// get customers from the dao
//List<City> theCities = cityDAO.getCities();
List<String> theCities = cityDAO.getCities();
Set<String> theHotels = hotelDetailsDAO.getHotels();
// add the customers to the model
theModel.addAttribute("cities", theCities);
theModel.addAttribute("hotels", theHotels);
//printing the data fetched
System.out.println("In HomeController showCheckAvailability method where city name is being fetched from city table");
theCities.forEach((n) -> System.out.println(n));
System.out.println("printing hotels");
for (String temp : theHotels) {
System.out.print(temp + " ");
}
return "checkAvailability-form";
}
@RequestMapping("/search")
public String searchResult(@RequestParam("cityName") String theCityName, @RequestParam("hotelName") String theHotelName,Model model) {
System.out.println("processed successfully");
return null;
}
}
Solution
When you use <form:form>
attribute, it requires you to specify model object that should be bound to form
tag. If you don't specify any model attribute default name is used as command
.
Following is the description of form:form
tag from spring-form.tld -
<attribute>
<description>Name of the model attribute under which the form object is exposed.
Defaults to 'command'.</description>
<name>modelAttribute</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>Name of the model attribute under which the form object is exposed.
Defaults to 'command'.</description>
<name>commandName</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
As you don't have any model object bound to form
, try removing form:form
tag and use HTML form
tag and also make sure you match input parameter names with method parameter names. i.e -
<form action="search">
...
</form>
Answered By - Sachin Gorade
Answer Checked By - Gilberto Lyons (JavaFixing Admin)