Returning a java.util.List from a Spring MVC controller via AJAX using Jackson Returning a java.util.List from a Spring MVC controller via AJAX using Jackson ajax ajax

Returning a java.util.List from a Spring MVC controller via AJAX using Jackson


The strategy you should use is to make the AJAX call from jQuery, take the JSON response and use it to update the form dynamically from jQuery not java. The only use I can see for the JSP tags would be to render the page when it loads. Here is how I would approach this...

  1. Change your controller to use a GET instead of a POST. For one, it is incorrect in REST to use a POST here (you are only retrieving data, not altering it). But, more importantly, it will make it easier for you to test the controller by simply putting the URL into a browser to see the JSON response. Using the @ResponseBody annotation and including Jackson on the classpath should produce a JSON response here (unless you have some Hibernate lazy loading issues).

  2. Once you verify the controller is returning JSON, update your jQuery success handler to dynamically populate the dropdown. This should be relatively easy. Test this in the browser.

  3. Write a new controller to handle this form submission. In this controller, just include a method to return the list and annotate it as @ModelAttribute("stateList"). This will make the list available for use in your <c:forEach> loop to render the page on load. You will have another method that will handle the actual form submission in the same controller.

Another thing to consider is to have better separation of concerns by putting the database code in its own service or repository. It's a bad practice to do data access in a controller in an MVC architecture. As a bonus, you won't need to duplicate any code to load the list in two different controllers.

Also, look into Spring's @Transactional declarative transaction handling. That way you won't need to write any code for transaction handling. You can also simply inject the SessionFactory instead of writing your own HibernateUtils.

Hope that helps

Edit : In REST, here are HTTP methods mapped to their corresponding CRUD actions.

  • POST - Create
  • GET - Retrieve
  • PUT - Update
  • DELETE - Delete


you should try to your option list in ajax response instead of json like below one

<option value="1">Mumbai</option><option value="2">Delhi</option><option value="3">Kerala</option><option value="4">Rajasthan</option>

and then your should add it to your select box liek below one

function getStates(countryId){    $.ajax({    datatype:"html",    type: "POST",    url: "/wagafashion/ajax/TempAjax.htm",    data: "countryId=" + countryId,    success: function(response)    {        $('#stateList').html(response);    },    error: function(e)    {        alert('Error: ' + e);    }  });}

or you can create json on serverside like below one

[{"key":1, "value": "Mumbai"}, {"key":2, "value":"Delhi"}....]

and in javascript code

function getStates(countryId){    $.ajax({    datatype:"html",    type: "POST",    url: "/wagafashion/ajax/TempAjax.htm",    data: "countryId=" + countryId,    success: function(response)    {         if(response !=''){              jQuery("#stateList").html("");              jQuery.each(response, function(index,item) {                jQuery("#stateList").append(jQuery("<option />")                .attr("value",item.value)                .text(item.key));             });         }    },    error: function(e)    {        alert('Error: ' + e);    }  });}