Spring MVC中的简单搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23832012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:45:01  来源:igfitidea点击:

Simple search in Spring MVC

springspring-mvcmodel-view-controllercrud

提问by Rodrigo E. Pinheiro

I'm new to Spring MVC, and I'm trying to do a simple search. Here's my Controller and View. How do I make the search to actually work? The findTeamByName is already implemented from a interface and the Teams are already populated in memory. Thank you in advance guys!

我是 Spring MVC 的新手,我正在尝试做一个简单的搜索。这是我的控制器和视图。如何使搜索真正起作用?findTeamByName 已经从接口实现,并且团队已经填充在内存中。提前谢谢你们!

@Controller
public class SearchController {

  @Autowired    
  SuperPlayerService sp;

  @RequestMapping(value="/search")
    public ModelAndView Search(@RequestParam(value = "searchTerm", required = false) 
    String pSearchTerm, HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView("search");

       mav.addObject("searchTerm", pSearchTerm);
       mav.addObject("searchResult", sp.findTeamByName(pSearchTerm));      

     return mav;
   }
}

JSP:

JSP:

<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>


<t:MasterTag>
<jsp:attribute name="pageTitle"><c:out value="Search"/></jsp:attribute>
<jsp:attribute name="currentMenuName"><c:out value="Search"/></jsp:attribute>
<jsp:body>





<div class="row">
     <div class="small-3 columns">
        <input type="text" id="txt" name="searchString">
      </div>

       <div class="small-5 columns end">
         <button id="button-id" type="submit">Search Teams</button>
       </div>

   </div>

 <div class="row">
      <div>
         ${searchTerm}
     </div>
</div>

回答by chresse

you can return your values i.e in a ModelAndView

你可以返回你的值,即在 ModelAndView

@RequestMapping(value="/search/{searchTerm}")
public ModelAndView Search(@PathVariable("searchTerm") String pSearchTerm) {
    ModelAndView mav = new ModelAndView("search");

    mav.addObject("searchTerm", pSearchTerm);
    mav.addObject("searchResult", sp.findTeamByName(pSearchTerm));      

    return mav;
}

This field can be accessed in your search.jsp by ${searchTerm}

可以通过以下方式在您的 search.jsp 中访问此字段 ${searchTerm}

EDIT:

编辑:

if you want so search like: search?searchTerm=javathen you can do it with:

如果你想这样搜索:search?searchTerm=java那么你可以这样做:

@RequestMapping(value="/search")
public ModelAndView Search(@RequestParam(value = "searchTerm", required = false) String pSearchTerm, HttpServletRequest request, HttpServletResponse response) {
    ModelAndView mav = new ModelAndView("search");

    mav.addObject("searchTerm", pSearchTerm);
    mav.addObject("searchResult", sp.findTeamByName(pSearchTerm));      

    return mav;
}

回答by Kalyan

Spring MVC Controller method can accept a wide range of arguments and one of them is org.springframework.ui.Model.

Spring MVC 控制器方法可以接受范围广泛的参数,其中之一是 org.springframework.ui.Model。

You can add values to the Model which will become available in your JSP at requestScope.

您可以向模型添加值,这些值将在 requestScope 的 JSP 中可用。

In your case, your Java code would look like this

在您的情况下,您的 Java 代码如下所示

@RequestMapping(value = "/search")
public String Search(@RequestParam("searchString") String searchString, Model model) {

    if(searchString != null){
        Object searchResult = sp.findTeamByName(searchString);
        model.addAttribute("searchResult", searchResult);  
    }

    return "search";
}

In your JSP, you could then access the result as usual object in requestScope like ${searchResult}

在您的 JSP 中,您可以像 ${searchResult} 一样在 requestScope 中像往常一样访问结果

回答by geoand

Your JSP needs to look like:

您的 JSP 需要如下所示:

<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>


<t:MasterTag>
<jsp:attribute name="pageTitle"><c:out value="Search"/></jsp:attribute>
<jsp:attribute name="currentMenuName"><c:out value="Search"/></jsp:attribute>
<jsp:body>

<div></div> 

<div class="row">
    <form method="get" action="/search">
       <div class="small-3 columns">
          <input type="text" id ="txt" name="searchString" >
       </div>

       <div class="small-5 columns end">
          <button id="button-id" type="submit">Search Teams</button>
       </div>

       <div>
           ${player.superTeam}
       </div>
    </form>

</div>

And your controller would be:

你的控制器将是:

@Controller
public class SearchController {

   @Autowired    
   SuperPlayerService sp;


   @RequestMapping(value = "/search")
   public String Search(@RequestParam("searchString") String searchString) {

      if(searchString != null){
         sp.findTeamByName(searchString);
      }

       return "search";
   }
}