为什么将 <form:form> 标记放入 spring 的 jsp 文件后出现错误 "java.lang.IllegalStateException"?

Why do am I getting error "java.lang.IllegalStateException" after putting <form:form> tag in jsp file of spring?

我的数据库中有 2 个表,city 和 hotel_details。我正在尝试从这些表中获取数据并填充到用于注册客户的表单中。但是我收到“java.lang.IllegalStateException:bean 名称 'command' 的 BindingResult 和普通目标对象均不可用作为请求属性”作为错误。

  1. JSP 文件

     <%@ 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>
    
  1. 控制器

@控制器 public class 家庭控制器 {

//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; 
      }
     

}

当您使用 <form:form> 属性时,它要求您指定应绑定到 form 标签的模型对象。如果您未指定任何模型属性,则默认名称用作 command.

以下是来自spring-form.tld-

form:form标签的描述
        <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>

因为您没有任何模型对象绑定到 form,请尝试删除 form:form 标签并使用 HTML form 标签并确保您匹配输入参数名称与方法参数名称。即-

         <form action="search">
...
         </form>