在Spring MVC中,我有一个搜索表单。如果用户提交搜索表单,结果应显示在同一页面中。 它重定向到同一页面但是我没有从控制器获取JSP中的属性。
qSearch.jsp
<form:form name="quickSearchForm" id="searchFormId" method="POST" action="./searchQuick.html" modelAttribute="searchForm" onsubmit="return validateForm()">
<table>
<tr>
<th>Change/Defect ID</th><td><form:input type="text" name="identifier" path="identifier"/></td>
</tr>
</table>
<div class="icons">
<span><button style="left:0px" type="reset" name="clear" style="border-radius: 25px;">CLEAR</button></span>
<span><button style="right:0px" type="submit" name="submit" style="border-radius: 25px;" >SEARCH</button></span>
</div>
</form:form>
<hr>
<div>
<c:if test="${empty SEARCH_RESULTS_KEY}">
<table style="border-collapse: collapse;" border="1" class="showResults">
<tr>
<td colspan="7">No Results found</td>
</tr>
</table>
</c:if>
<c:if test="${! empty SEARCH_RESULTS_KEY}">
<table style="border-collapse: collapse;" border="1" class="showResults">
<tr>
<td colspan="7">Result found</td>
</tr>
</table>
</c:if>
控制器
@RequestMapping(value="/qSearch", method = RequestMethod.GET)
public String getQuickSearchmodel(Model model) {
System.out.println("Welcome to search tool\n");
ArchivalIssue archivalIssue=new ArchivalIssue();
model.addAttribute("searchForm", archivalIssue);
return "quickSearchPage";
}
@RequestMapping(value = "/searchQuick", method = RequestMethod.POST)
public ModelAndView getAllArchivalIssues(HttpServletRequest request){
String identifier = request.getParameter("identifier");
List<ArchivalIssue> archivalIssue = archivalIssueService.getAllArchivalIssue(identifier);
ModelAndView mav = new ModelAndView("redirect:/qSearch"); //Add model to display results
mav.addObject("SEARCH_RESULTS_KEY", archivalIssue); //Add result object to model
return mav;
}
请有人帮助我,如何在JSP中获得结果。我总是找不到结果。
答案 0 :(得分:0)
当返回值包含redirect:prefix时,视图名称的其余部分将被视为重定向URL。浏览器将向此重定向URL发送新请求。因此,将执行映射到此URL的处理程序方法。
只返回与模型属性响应相同的页面。
您是否正在使用JSTL是否需要进行任何更改
${empty param.SEARCH_RESULTS_KEY}
答案 1 :(得分:0)
我认为在这种情况下你不应该重定向。因为重定向将为服务器创建新的GET请求。 quickSearchPage
和getAllArchivalIssues
应该返回相同的视图
@RequestMapping(value = "/searchQuick", method = RequestMethod.POST)
public ModelAndView getAllArchivalIssues(HttpServletRequest request){
String identifier = request.getParameter("identifier");
List<ArchivalIssue> archivalIssue = archivalIssueService.getAllArchivalIssue(identifier);
//return quickSearchPage, so that the client can render the list archivalIssue
ModelAndView mav = new ModelAndView("quickSearchPage");
mav.addObject("SEARCH_RESULTS_KEY", archivalIssue); //Add result object to model
return mav;
}