Reputation: 631
I've created a page to search students from the database using spring boot & thymeleaf. In my SearchStudent.html
page, there are 3 fields as search parameters (firstName, lastName, city). My requirement is that the search should work even if I enter no parameter (Search All) or based on the parameters. 'Search All' condition is working but not sure how to change the controller to work when I pass some or all parameters in the search condition.
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>
Upvotes: 1
Views: 8533
Reputation: 1464
Your form binds the form fields to a th:object ${student} which is the input parameter to a HTTP POST method which your controller needs to implement. It should be something like:
@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;
}
You should also set the method of your form to 'post'
<form th:object="${student}" th:action="@{/search}" action="#" method="post">
Submitting a form with input field data to be put into a model and sent should be via HTTP POST, see here: http://www.w3schools.com/tags/att_form_method.asp
Alternatively, you could add a second different GET request mapping that parses the form fields from the URL parameters. LEaving the form method as 'get', you would add another GET request mapping as follows:
@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;
}
But do be aware of the limitations, and security implications of using form method='get' for sending form data.
Upvotes: 1