Link to home
Start Free TrialLog in
Avatar of Amar-ms
Amar-ms

asked on

send JSON object to JSP from a spring MVC

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.
Avatar of Ajay-Singh
Ajay-Singh

Try using jackson
   http://jackson.codehaus.org/Tutorial
Avatar of Sathish David  Kumar N
Can u post ur Layout ???

as per layout only we can set the value ...
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 !!
 
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

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

Avatar of Amar-ms

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i think others 2 step  are u know