Featured Posts

ExtJS, Spring MVC 3 and Hibernate 3.5: CRUD DataGrid... This tutorial will walk through how to implement a CRUD (Create, Read, Update, Delete) DataGrid using ExtJS, Spring MVC 3 and Hibernate 3.5. What do we usually want to...

Readmore

ExtJS plugin: PagingToolbarResizer Well, this is my first ExtJS plugin. Though it is not an advanced plugin, I'm very happy and it is a big accomplishment for me! The problem: ExtJS PagingToolbar Component...

Readmore

Hibernate 3 Annotations Tutorial This tutorial will walk through how to implement a hello world project using Hibernate Annotations and MySQL database. Requirements Download and unzip Hibernate Core...

Readmore

My DeveloperWorks: What's life like for a female Java... Just wanted to share with you my interview on Valerie's My developerWorks blog: Interview with Loiane Groner, Java developer in Brazil. I'm very happy, because this interview...

Readmore

Getting started with ExtJS in your J2EE project This tutorial will walk through how to get an Ext JS installation up and running quickly in your java (J2EE) project. Level: Basic This is the screenshot of this tutorial: First,...

Readmore

Loiane Groner Rss

Spring MVC and AJAX with JSON

Posted on : 15-02-2010 | By : Loiane | In : JSON, Spring

7

This tutorial will walk through how to configure Spring MVC to return a JSON object to client browser.

One of the main decisions to be taken while developing AJAX applications is the format of messages passed by the server to the client browser. There are many options to choose from including plain text, XML, CSV etc. One of the more popular choices today is the JavaScript Object Notation (JSON). JSON provides a nice name-value pair data format that is easy to generate and parse.

How to do it?

You can use json-lib-ext-spring. There are other libs, this is the one I found. If you know or use another one, please leave a comment with the library name. :)

Do not forget to download Json-lib and its dependencies.

Now you have to configure your XML files:

Create a views.xml file under WEB-INF folder and paste the following code into it:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation= "http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-2.5.xsd">

 <bean name="jsonView" class="net.sf.json.spring.web.servlet.view.JsonView" />

</beans>

Add this config to you spring configuration file:

<!-- json -->
<bean id="xmlFileViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
	<property name="location">
		<value>/WEB-INF/views.xml</value>
	</property>
	<property name="order">
		<value>1</value>
	</property>
</bean>

Make sure to set the order if you are using any other view resolvers.

Now you just have to use “jsonView” as the viewname and the model will be converted to JSONbefore being sent back to the client:

return new ModelAndView("jsonView", modelMap);

Here is an example:

	public ModelAndView getColumnsJson(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String,Object> modelMap = new HashMap<String,Object>(2);
		modelMap.put("rows", service.generateColumns());
		return new ModelAndView("jsonView", modelMap);

	}

Happy coding!

Comments (7)

I never use json-lib so I cannot tell about it. But I used flexjson and jackson.

flexjson: AFAIK, flexjson is only a one-way java object to json string transformation. If you use flexjson, your json string will also contain information that you rarely need (e.g. you json string will contain the class name of your java object).

jackson: 2-way java object json string. So you can send a javascript object to the server and have the information in that javascript object populated to the java object. And your json string doesn’t have the class name inside :) How to create a json string from the browser? Just use JSON.stringify(a_js_object). Jackson is used in Spring 3.0. So I pick jackson over json-lib (I think Spring chose the best json library for them).

prefer use XStream and jettison :) .
Anyway good article .

Rgds,
AR

Very good article. Thanks for taking the time for doing this.

Hi,

May I know, what does the

[modelMap.put("rows", service.generateColumns());]

service.generateColumns() looks like?

There is also GSON library,http://code.google.com/p/google-gson/ which give you more control for (de)serialization of objects (it help to tune json manipulation if you use inheritance etc.) Personally I prefer this library.

Thanks for this post I was able to fix my problem using your post. One question regarding this why cant we directly add the jsonView bean in the main configuration file ?

Excellent. I was looking for something to simple to get started. Do you have the complete project for distribution? Thanks.

Write a comment