How to return an object from a Spring MVC controller in response to AJAX request?

前端 未结 4 946
醉酒成梦
醉酒成梦 2020-11-30 08:13

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:



        
4条回答
  •  抹茶落季
    2020-11-30 08:35

    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.

提交回复
热议问题