0

I want to add search field in the crud project I used the method findAllByNameAndLast(Name, Last); but the method is not working also I don't know how to return a result to the index.html

project link is : project link

@RequestMapping(value = "/searchEmployee/{firstname}/{lastname}", method = RequestMethod.GET)
public String getEmployeeByName(@PathVariable("name") String Name, @PathVariable("last") String Last, ModelMap modelMap) {
    List<Student> student = re.findAllByNameAndLast(Name,Last);
    modelMap.addAttribute("message", student);
    return "index";
}
Hardik Modha
  • 12,098
  • 3
  • 36
  • 40
Ninos
  • 43
  • 1
  • 6
  • 1
    What does "the method is not working" mean? Do you get a compile error, or an exception when you run the program? What is the error message? – Jesper Jul 12 '18 at 06:07

1 Answers1

0

Modify your controller method like below.

@RequestMapping(value = "/searchEmployee/{firstname}/{lastname}", method = RequestMethod.GET)
public String getEmployeeByName(@PathVariable("firstname") String Name, @PathVariable("lastname") String Last, ModelMap modelMap) {
    List<Student> student = re.findByNameAndLast(Name,Last);
    modelMap.addAttribute("message", student);
    return "index";
}

and in jsp iterate this list using foreach loop like below :

<c:foreach items="message" var="item">
  <c:cout value="item.name"></c:out>
  <c:cout value="item.last"></c:out>
</c:foreach>

Use findByNameAndLast instead of findAllByNameAndLast as but this is not the reason behind failure of your code because jpa ignores all in findAllByNameAndLast

Spring Data JPA difference between findBy / findAllBy

Alien
  • 15,141
  • 6
  • 37
  • 57