1

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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I've used your example to findAll but getting error------------There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported---------------- if I remove method=RequestMethod.POST it works, but no record displayed.
I think my edit above should help.. check it please: :)
I did try that as well but no luck :(
Can you post up the current controller code? Method not allowed indicates there is no request mapping for the method you requested. You should have both a get and post method for the view which your form is in (SearchStudent.html), GET will display the SearchStudent.html view, POST will submit the form data from the view.
@RequestMapping(method=RequestMethod.POST, value="/search") public ModelAndView doSearch(Student student){ List<Student> students = (ArrayList<Student>)repository.findAll(); ModelAndView mv = new ModelAndView(); mv.addObject(students); mv.setViewName("/SearchStudent"); return mv; }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.