已经提出了许多关于此错误的问题。但我找不到解决方案。这是我的表格:
<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的新手,所以如果可能,请帮助我。任何帮助都将不胜感激。
发布于 2021-05-03 17:20:23
我认为你需要在你的控制器上添加@SessionAttributes("flightSearch")。
模型对象将在get之后在后台绑定到会话,然后可以在post中检索。
之后,您需要在post方法中使用SessionStatus.setComplete (或类似的东西)清理会话。
您只需要将SessionStatus添加到post方法参数。
我想这就是在spring mvc中实现'get和post‘表单工作流的方式。
发布于 2021-05-04 00:27:04
正如你在评论中提到的,你正在重定向。
假设上面的jsp代码是'welcome.jsp‘
看起来您正在将它重定向到jsp页面,而不是RequestMapping值。
试试这个:
ModelAndView mv=new ModelAndView();
mv.setViewName("redirect:welcome"); //Code Should added where you redirecting the login page to search pagehttps://stackoverflow.com/questions/67364772
复制相似问题