1、配置struts.xml
<action name="getGsonAction" class="com.test.action.json.GetGsonAction"> </action>
2、添加js方法
getJson = function(){ $.post("getGsonAction.action",{name:$('#name').val()},function(ret,status){ if("success" == status){ } }); }
3、添加jsp页面
<html> <head> <script type="text/javascript" src="js/jquery-1.8.2.js"></script> <script type="text/javascript" src="scripts/common.js"></script> </head> <body id="xmlbody"> <select id="name"> <option value="james" selected="selected">james</option> <option value="currey">currey</option> </select> <input type="button" value="getInfo" onclick="getJson();"/> </body> </html>
4、bean文件
public class Address { private String homeAddress; private String companyAddress; public String getHomeAddress() { return homeAddress; } public void setHomeAddress(String homeAddress) { this.homeAddress = homeAddress; } public String getCompanyAddress() { return companyAddress; } public void setCompanyAddress(String companyAddress) { this.companyAddress = companyAddress; } } package com.test.action.json; import java.util.List; //9分钟 public class People { private int id; private String name; private Address address; private List<People> friends; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public List<People> getFriends() { return friends; } public void setFriends(List<People> friends) { this.friends = friends; } }
5、添加action类
public class GetGsonAction extends ActionSupport { private static final long serialVersionUID = 5409431608941107203L; public String execute() throws Exception { List<People> list = new ArrayList<People>(); //第一个人的信息 Address address1 = new Address(); address1.setCompanyAddress("shanghai"); address1.setHomeAddress("beijing"); People people1 = new People(); people1.setId(1); people1.setName("james"); people1.setAddress(address1); People friend1 = new People(); people1.setId(4); people1.setName("smith"); People friend2 = new People(); friend2.setId(5); friend2.setName("owen"); List<People> list1 = new ArrayList<People>(); list1.add(friend1); list1.add(friend2); people1.setFriends(list1); //第二个人的信息 People people2 = new People(); Address address2 = new Address(); address2.setCompanyAddress("tianjin"); address2.setHomeAddress("tanggu"); people2.setId(2); people2.setName("currey"); people2.setAddress(address2); List<People> list2 = new ArrayList<People>(); People friend3 = new People(); friend3.setId(7); friend3.setName("igdala"); list2.add(friend3); People friend4 = new People(); friend4.setId(8); friend4.setName("toms"); list2.add(friend4); people2.setFriends(list2); list.add(people1); list.add(people2); Gson gson = new Gson(); String result = gson.toJson(list); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/json; charset=utf-8"); response.setHeader("Cache-Control", "no-cache");//取消浏览器缓存 PrintWriter writer = response.getWriter(); writer.write(result); writer.flush(); writer.close(); System.out.println(result); return null; } }