Struts2与Gson开发

struts2.xml配置信息:
action name="getGsonAction" class="com.test.action.json.GetGsonAction"></action>

Action对象:
package com.test.action.json;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class GetGsonAction extends ActionSupport
{
@Override
public String execute() throws Exception
{
List<People> list = new ArrayList<People>();

// Info of first user
People people1 = new People();

Address address1 = new Address();
address1.setHomeAddress("beijing");
address1.setCompanyAddress("shanghai");

people1.setId(1);
people1.setName("zhangsan");
people1.setAddress(address1);

People people11 = new People();
people11.setId(5);
people11.setName("zhangsan's friend1");

People people12 = new People();
people12.setId(6);
people12.setName("zhangsan's friend2");

List<People> list1 = new ArrayList<People>();
list1.add(people11);
list1.add(people12);

people1.setFrield(list1);

//Info of second user
People people2 = new People();

Address address2 = new Address();
address2.setHomeAddress("liaoning");
address2.setCompanyAddress("fujian");

people2.setId(2);
people2.setName("lisi");
people2.setAddress(address2);

People people21 = new People();
people21.setId(10);
people21.setName("lisi's friend1");

People people22 = new People();
people22.setId(11);
people22.setName("lisi's friend2");

List<People> list2 = new ArrayList<People>();
list2.add(people21);
list2.add(people22);

people2.setFrield(list2);

// put all the users into result list
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 out = response.getWriter();
out.print(result);

out.flush();
out.close();

return null;
}
}


Address对象:
package com.test.action.json;

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;
}
}

People对象:
package com.test.action.json;

import java.util.List;

public class People {

private int id;
private String name;
private Address address;
private List<People> frield;
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> getFrield() {
return frield;
}
public void setFrield(List<People> frield) {
this.frield = frield;
}


}

你可能感兴趣的:(struts2)