How to return an object from a controller in ajax in spring mvc

I need to return a list of employees from Controller to jQuery ajax. How can I do it?

//controller part @RequestMapping("phcheck") public ModelAndView pay(@RequestParam("empid") int empid, String fdate, String tdate) { ModelAndView mav = new ModelAndView("phcheck"); List<Employee> emp=entityManager.createQuery("select e from Employee e where e.empId="+empid, Employee.class).getResultList(); mav.addObject("emp", emp); <----I need this list of employee in ajax return mav; } 

Ajax in view

 //Ajax part $(document).ready(function () { $("#empid").change(function () { if ($("#fdate").val() != "" && $("#tdate").val() != "" && $("#empid").val() != "") { jQuery.ajax({ url: "phcheck.htm?empid=" + $("#empid").val() + "&&fdate=" + $("#fdate").val() + "&&tdate=" + $("#tdate").val(), success: function (data) { alert(data + "success"); }, error: function (data) { alert(data + "error"); } }); } else { alert("Please fill the from date and to date or select the employee id"); $("#empid .option").attr("selected", "selected") } }); 

});

Thanks in advance.

+7
jquery spring list ajax controller
source share
4 answers

I need this list of employees in ajax

In spring, when you need object serialization, de-serialization and message conversion. in this case, you need to annotate the controller handler method using @RequestBody and @ResponseBody .

Where:

  • @ResponseBody: tells spring to try to convert its return value and automatically write it to the http response.
  • @RequestBody: tells spring, which is trying to convert the contents of the body of the incoming request into a parameter object on the fly.

in your case, you need a JSON type, you must add @ResponseBody to your method signature or just above the method, and also produce and consume, which are optional, for example:

 @RequestMapping(value="phcheck", method=RequestMethod.GET produces="application/json") public @ResponseBody List<Employee> pay(@RequestParam("empid") int empid, String fdate, String tdate) { //get your employee list here return empList; } 

and in an AJAX call use:

  • Attribute
  • contentType: 'application/json' indicates the type of data you are sending. and attribute
  • dataType: json tells jquery what type of response will be received.

in your case contentType: 'application/json' not required, by default one ie 'application/x-www-form-urlencoded; charset=UTF-8' 'application/x-www-form-urlencoded; charset=UTF-8' enough.

and you can get a list of employees in your AJAX success, iterate over it:

  success: function (data) { $.each(data, function(index, currEmp) { console.log(currEmp.name); //to print name of employee }); }, 

<h / "> Note: Jackson mapper or any other mapper must be available along the build path in order to work with JSON serialization and deserialization.

See also:

+14
source share
 @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 need to register the MappingJackson2HttpMessageConverter as done here

+1
source share

ModelAndView assumes that you plan to display a view that is not there. To simply return an object, use the @ResponseBody annotation:

 @RequestMapping("phcheck") public @ResponseBody List<Employee> pay(@RequestParam("empid") int empid, String fdate, String tdate) { return entityManager.createQuery("select e from Employee e where e.empId="+empid, Employee.class).getResultList(); } 

In addition, you should be more careful about how to handle requests. Your query is unsafe and will allow you to use SQL injection. For example, if someone called your method using empId = "'15' or '1' = '1'", then he will return the entire list of employees.

+1
source share

Make the method as @ResponseBody Type in the controller and in ajax take the List from success function.

Place the Jackson Mapper file in the Pom.xml file if using Maven

0
source share

All Articles