2016-07-01 78 views
1

我创建了一个页面,使用弹簧启动&百里香叶从数据库搜索学生。在我的SearchStudent.html页面中,有3个字段作为搜索参数(firstName,lastName,city)。我的要求是,即使没有输入参数(全部搜索)或基于参数,搜索也应该可以工作。 '搜索全部'条件正在工作,但不确定如何在搜索条件中传递一些或全部参数时如何更改控制器以正常工作。如何在springboot中将参数值传递给控制器​​

SearchController

@Controller 
@ComponentScan 
public class SearchController { 

    @Autowired 
    protected StudentRepository repository; 

    @RequestMapping(value = {"/","/search"}, method = RequestMethod.GET) 
    public String search(Model model) { 
     model.addAttribute("student", new Student()); 
     model.addAttribute("allStudents", (ArrayList<Student>)repository.findAll()); 
     return "SearchStudent"; 
    } 

SearchStudent.html

<div class="panel-body"> 
    <form th:object="${student}" th:action="@{/search}" action="#" method="get"> 
     <input type="text" th:field="*{firstName}" class="form-control" placeholder="First Name" /> 
     <div style="clear: both; display: block; height: 10px;"></div> 
     <input type="text" th:field="*{lastName}" class="form-control" placeholder="Last Name" /> 
     <div style="clear: both; display: block; height: 10px;"></div> 
     <input type="text" th:field="*{city}" class="form-control" placeholder="City" /> 
     <div style="clear: both; display: block; height: 10px;"></div> 
     <input type="submit" class="btn btn-danger pull-right" value="Search"> 
     <input type="submit" class="btn btn-success pull-right" value="Clear"> 
    </form> 
</div> 

回答

1

你的形式结合表单字段到第一个:对象$ {学生}作为输入参数的HTTP POST你的控制器需要实现的方法。它应该是这样的:

@RequestMapping(method=RequestMethod.POST, value="/search") 
public ModelAndView doSearch(Student student){ 
    // do your conditional logic in here to check if form parameters were populated or not 
    // then do something about getting results from the repository 
    List<String> students = repository.find....; 
    // return a model and a view (just as an example) 
    ModelAndView mv = new ModelAndView(); 
    mv.addObject(students); 
    mv.setViewName("/results"); 
    return mv; 
} 

您还应该设置你的形式,以“后”

<form th:object="${student}" th:action="@{/search}" action="#" method="post"> 

方法提交与输入字段数据的形式被放入一个模型,并发送应通过HTTP POST,请参阅:http://www.w3schools.com/tags/att_form_method.asp

或者,您可以添加第二个不同的GET请求映射,它从URL参数中解析表单域。留下的形式方法“获得”,则要再添加一个GET请求映射如下:

@RequestMapping(value = {"/search"}, method = RequestMethod.GET) 
public String doSearch(@PathVariable String firstName, @PathVariable String lastName, @PathVariable String city) { 
    // Add your conditional logic to search JPA repository based on @PathVariable values delivered from form submission using HTTP GET 

    List<String> students = repository.find....; 
    ModelAndView mv = new ModelAndView(); 
    mv.addObject(students); 
    mv.setViewName("/results"); 
    return mv; 
} 

但一定要知道的局限性,以及使用形式方法=“得到”发送表单数据的安全影响。

+0

我用你的例子findAll但得到错误------------有一个意外的错误(type = Method Not Allowed,status = 405)。 请求方法'GET'不支持----------------如果我删除method = RequestMethod.POST它的作品,但没有记录显示。 – Muhammad

+0

我想我的编辑上面应该帮助..请检查它:) :) –

+1

我也尝试过,但没有运气:( – Muhammad

相关问题