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

前端 未结 4 947
醉酒成梦
醉酒成梦 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:44

    @RequestMapping(value = "phcheck", produces = "application/json")
    @ResponseBody
    public ModelAndView pay(@RequestParam("empid") int empid, @RequestParam("fdate") String fdate, @RequestParam("tdate") String tdate) {
    
       return entityManager.createQuery("select e from Employee e where e.empId="+empid, Employee.class).getResultList();
    }
    
    
    url:"phcheck.htm?empid="+$("#empid").val()+"&fdate="+$("#fdate").val()+"&tdate="+$("#tdate").val()
    

    In Spring MVC you will have to have registered MappingJackson2HttpMessageConverter like being done here

提交回复
热议问题