如何使简单的搜索表单工作

时间:2013-12-12 21:14:47

标签: spring jsp spring-mvc

首先,存储库和服务search(searchCriteria)已经过测试并正常运行。

这是我的表格:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
...

 <form:form method="GET" action="fooSearchResults">
    <div class="form-group">
        <form:input path="searchCriteria" type="text" id="searchCriteria" placeholder="Find a location..." />
    </div>
    <input type="submit" class="btn btn-default" value="Search" />
</form:form>

控制器方法:

@RequestMapping(value = "/fooSearchResults", method = RequestMethod.GET)
    public ModelAndView search(@ModelAttribute("searchCriteria") String searchCriteria, BindingResult result) {
        List<Foos> foos = fooService.search(searchCriteria);

        return new ModelAndView("searchResults", "command", locations);
    }

当我点击页面时出现以下错误消息:

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/locationSearch.jsp at line 20

17:                  <div class="visible-md visible-lg well well-search">
18:                    <form:form method="GET" action="fooSearchResults">
19:                       <div class="form-group">
20:                         <form:input path="searchCriteria" type="text" id="searchCriteria" placeholder="Find a location..." />
21:                       </div>
22:                       <input type="submit" class="btn btn-default" value="Search" />
23:                     </form:form>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)


root cause 

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute

1 个答案:

答案 0 :(得分:0)

您不需要modelAttribute也不需要bindingResult因为searchCriteriaString而不是pojo。 而是这样做:

@RequestMapping(value = "/fooSearchResults", method = RequestMethod.GET)
public ModelAndView search(@RequestParam("searchCriteria") String searchCriteria) {
    List<Foos> foos = fooService.search(searchCriteria);

    return new ModelAndView("searchResults", "command", locations);
}