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>