首页 > 解决方案 > Bean 名称“flightSearch”的 BindingResult 和普通目标对象都不能用作请求属性

问题描述

已询问有关此错误的许多问题。但我找不到解决方案。这是我的表格:

    <form:form id="flightSearchForm" modelAttribute="flightSearch" action="searchFlights"
    method="post">
    <table align="center">
        <tr>
            <td><form:label path="departureLocation">Departure Location: </form:label></td>
            <td><form:input path="departureLocation" name="departureLocation" id="departureLocation" />
            </td>
        </tr>
        <tr>
            <td><form:label path="arrivalLocation">Arrival Location:</form:label></td>
            <td><form:input path="arrivalLocation" name="arrivalLocation"
                    id="arrivalLocation" /></td>
        </tr>
        <tr>
            <td><form:label path="flightDate">Flight Date</form:label></td>
            <td><form:input type="date" path="flightDate" name="flightDate"
                    id="flightDate" /></td>
        </tr>
        <tr>
            <td><form:label path="flightClass">Flight Class</form:label></td>
            <td><form:select path="flightClass" name="flightClass"
                    items = "${flightClasses}" id="flightClass" /></td>
        </tr>
        
        <tr>
            <td><form:label path="outputPreference">Output Preference</form:label></td>
            <td><form:select path="outputPreference" name="outputPreference"
                    items = "${outputPreferences}" id="outputPreference" /></td>
        </tr>
        <tr>
            <td></td>
            <td align="left"><form:button id="search" name="search">Search</form:button>
            </td>
        </tr>
    </table>
</form:form>

这是我的控制器:

 @Controller
 public class FlightSearchController {
 @RequestMapping(value = "/welcome" ,method = RequestMethod.GET)
public ModelAndView showWelcomePage()
{
    ModelAndView mav = new ModelAndView();
    mav.addObject("flightSearch", new FlightSearch());
    mav.setViewName("welcome.jsp");
    return mav;
}


@RequestMapping(value = "/searchFlights" ,method = RequestMethod.POST)
public ModelAndView searchForFlights(@ModelAttribute("flightSearch") FlightSearch flightSearch)
{
    ModelAndView mav = new ModelAndView();
    mav.addObject("depLoc", flightSearch.getDepartureLocation());
    mav.addObject("arrivalLocation", flightSearch.getArrivalLocation());
    mav.addObject("flightDate",flightSearch.getFlightDate());
    mav.setViewName("flightDetails.jsp");
    return mav;

}

}

我几乎尝试了这里提到的所有解决方案,但无法解决问题。我是Spring的新手,所以请尽可能提供帮助。任何帮助,将不胜感激。

标签: javaspringformsjsp

解决方案


正如您在评论中提到的那样,您正在重定向。

假设你上面的jsp代码是'welcome.jsp'

似乎您将其重定向到 jsp 页面而不是 RequestMapping 值

尝试这个:

    ModelAndView mv=new ModelAndView(); 
    mv.setViewName("redirect:welcome"); //Code Should added where you redirecting the login page to search page

推荐阅读