java - 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的新手,所以请尽可能提供帮助。任何帮助,将不胜感激。
解决方案
正如您在评论中提到的那样,您正在重定向。
假设你上面的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
推荐阅读
- jquery - SetInterval 不能与 setTimeout 一起使用?
- pyrocms - 如何在 PyroCMS 3.x 中的数据库中存储联系人表单数据
- typescript - 防止在 Firebase 函数中调用子触发器
- veins - Veins 中是否有任何 DCC 算法?
- php - 如何从 WooCommerce 中的订单项中获取产品类别 ID
- swift - macOS 应用全屏按钮变为加号并禁用进入全屏
- node.js - 使用 node.js express 和 reactjs 将数据插入 phpmyadmin
- prometheus - x509:prometheus 的未知权威签署的证书
- asp.net-core - 访问网络驱动器 asp net core 上的文件
- angular - 没有残酷过渡的角粘性导航栏