I have to return a list of employees from a controller in response to jQuery AJAX request. How should I do for it?
My controller:
The ModelAndView implies you plan to render a view, which you don't. To just return the object, use the @ResponseBody annotation:
@RequestMapping("phcheck")
public @ResponseBody List pay(@RequestParam("empid") int empid, String fdate, String tdate) {
return entityManager.createQuery("select e from Employee e where e.empId="+empid, Employee.class).getResultList();
}
Also, you should be more careful about how you handle queries. Your query is insecure and would allow sql injection. For example, if someone called your method with empId = "'15' or '1'='1'", then it would return the entire list of employees.