?
Solved

send JSON object to JSP from a spring MVC

Posted on 2009-12-17
11
Medium Priority
?
4,555 Views
Last Modified: 2013-11-11
I am trying to convert a java object to JSON and send it to my jsp (actually jqgrid-jquery object is expecting this JSON) the project is using spring framework.

Any code examples will be extremely helpful.
0
Comment
Question by:Amar-ms
  • 9
11 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 26077794
Try using jackson
   http://jackson.codehaus.org/Tutorial
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26077915
Can u post ur Layout ???

as per layout only we can set the value ...
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26077923
1.set ur values in one bean (what ever u want display)!!
2. passes to view Class .
3.that view calss return JSon Object to ur jsp.
4.ur layout take care of to dispaly the values !!


i think this will help full if u need i will give code !!
 
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26078161
Examples

i think u know the 1st step i mention in the last command !!
lIst having beans .

List userDetails=papaUserManagementInter.getUserDetails();

 map.put("userDetails", userDetails);
return new ModelAndView(new  userView(), map);
public class userView implements View{
	
	public String getContentType() {
		return "text/json";
	}

	@SuppressWarnings("unchecked")
	public void render(Map model, HttpServletRequest arg1,
			HttpServletResponse response) throws Exception {
		response.setContentType("text/json");
		response.setCharacterEncoding("UTF-8");
		
		JsonConfig jsonConfig = new JsonConfig();  
		jsonConfig.registerJsonValueProcessor(Calendar.class, new CalendarJsonValueProcessor());
		

		List userDetails =  (List)model.get("userDetails");
		Map map1 = new HashMap();
		map1.put("identifier", "userCode");
		map1.put("urModelName", userDetails);
		
		
		JSONObject jsonBulletin = JSONObject.fromObject(map1, jsonConfig);
		PrintWriter writer = response.getWriter();
		writer.print(jsonBulletin .toString());
		
		

	}

}

Open in new window

0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26078165
i have copied this jqGrid where i mention in the link

colModel:[ {} ],  this part only having value .

map1.put("colModel", userDetails);

u mention like this !!

jQuery("#rowed1").jqGrid({
 url:'server.php?q=2',
 datatype: "json", 
colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'], 
colModel:[ {} ], 
rowNum:10,
 rowList:[10,20,30],
 pager: '#prowed1', 
sortname: 'id',
 viewrecords: true,
 sortorder: "desc", 
editurl: "server.php", 
caption: "Basic Example" });

Open in new window

0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26078173
Just try !!
0
 

Author Comment

by:Amar-ms
ID: 26099848
Hi Dravidnsr,
thanks for taking your time to explain this.. but i need some more help. how do i configure the view and the controller/bean etc in the xml also if you please paste a simple controller/ bean and all the wiring in the xml.. i am new to spring and its a bit hard for me to grasp the 4 steps u mentioned above.

thanks a bunch for your help.
0
 
LVL 20

Accepted Solution

by:
Sathish David  Kumar N earned 2000 total points
ID: 26102721
<<<<<<how do i configure the view

No need to define  view new operater and call the class

like this
 map.put("userDetails", userDetails);
return new ModelAndView(new  userView(), map);

c above 4 the class code !!

u dont want to define in xml for view(JSon View) !!
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 2000 total points
ID: 26102729
>>>>>>> 1.set ur values in one bean (what ever u want display)!!

Normaly u will excute some HQL query or annotation or Creatria for getting values from DB right !!

So that value u must set to ur bean !!

For eg: username u get from ur DB means
urBeanObject.setUserName("DBValue");
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 2000 total points
ID: 26102733
>>>>>>2. passes to view Class .

that value is return to Controller right !!

Pass that value to ur View

List userDetails=papaUserManagementInter.getUserDetails();
 map.put("userDetails", userDetails);
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26102734
i think others 2 step  are u know
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question