Ajax and JQuery autocomplete integration with Spring MVC

Discussions

News: Ajax and JQuery autocomplete integration with Spring MVC

  1. I believe after Spring blog announced Ajax integration with Spring MVC, http://blog.springsource.org/2010/01/25/ajax-simplifications-in-spring-3-0/ many of us are excited.

    The best part is add @ResponseBody in your controller and Spring will create JSON response for you.

    @RequestMapping(value=”/testJquery”, method=RequestMethod.GET)
    public @ResponseBody List getProperResponse(@RequestParam String name) {

    }

    jquery autocomplete
    (http://jqueryui.com/demos/autocomplete/#remote) expects json response in the following format.

    {
    label: blablabla

    value: blablabla

    }

    So your ResponseBean should have following annotations and field names

    ResponseBean{

    @JsonProperty(value=”label”)
    String name;

    @JsonProperty(value=”name”)
    String value;
    }

    You need entry in your dispatcher-servlet.xml so that Jackson Jars are invoked to formulate your ajax response.

    And you need jackson jars in app server classpath.
    (jackson-core-asl-1.9.5 and jackson-mapper-asl-1.9.5)

    Install firbug to see your json response. If its improper, it means that jackson jars are not being picked up by your Spring and you may need to place them in your classpath.

    Thats all for now. Happy coding with Spring 

     

  2. Glad you're excited, and nice post! Spring 3.1 is out now, and provides even smoother support. A quick point of clarification: if you have the Jackson (for JSON) or JAXB (for XML, for example) .jars in your classpath, Spring will automatically install the HttpMessageConverters that let you render JSON and XML responses for methods annotated with @ResponseBody from a Spring MVC controller. Additionally, in the case of Jackson, you don't need the Jackson annotations on your domain model, but they can be used to precisely specify certain things about how your domain's mapped to a JSON representation. You can force the response to render JSON by either sending an Accept: application/json header in the request (assuming the defaults are in place and that JSON is supported), or by specifying the "produces=..." attribute in Spring MVC 3.1 on the @RequestMapping annotation.  

  3. Um...yeah, hey...[ Go to top ]

    jQuery is MVC in the browser.  Servlets are really fun, simple, lightweight, scalable, can call EJB.  GSON does conversion to JSON for you.  There are people out there still originating projects in Spring/Struts like frameworks?  Wow.

     

    -- John