Struts2 json plugin实战1

jopen 13年前
     <p>1. 首先是在struts2的工程里添加struts2-json-plugin.xxx.jar库,如果是使用的maven,添加以下dependency</p>    <p></p>    <pre class="brush:xml; toolbar: true; auto-links: false;"> <dependency>   <groupId>org.apache.struts</groupId>   <artifactId>struts2-json-plugin</artifactId>   <version>2.1.8.1</version>  </dependency></pre>     <p></p>    <p> </p>    <p>2. 创建Action类和一个用到的User类</p>    <p> </p>    <p>Action类内容</p>    <p></p>    <pre class="brush:java; toolbar: true; auto-links: false;">import com.opensymphony.xwork2.Action; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.struts2.json.annotations.JSON; public class JSONExample1 {  private int[] ints = {10, 20};  private List<User> userList = new ArrayList<User>();  private Map<String, User> userMap = new HashMap<String, User>();  private Date date = Calendar.getInstance().getTime();    private String customName = "custom";  private String field1 = "field1";  //'transient' fields are not serialized  private transient String field2 = "field2";    //fields without getter method are not serialized  private String field3;  public String execute() {   for (int i = 0; i < 2; i++) {    User user = new User("id_" + i, "username_" + i, "password_" + i, "desc_" + i);    userList.add(user);    userMap.put(user.getId(), user);   }   return Action.SUCCESS;  }  public int[] getInts() {   return ints;  }  public void setInts(int[] ints) {   this.ints = ints;  }  public List<User> getUserList() {   return userList;  }  public void setUserList(List<User> userList) {   this.userList = userList;  }  public Map<String, User> getUserMap() {   return userMap;  }  public void setUserMap(Map<String, User> userMap) {   this.userMap = userMap;  }  public Date getDate() {   return date;  }  public void setDate(Date date) {   this.date = date;  }    public String getField1() {   return field1;  }  public void setField1(String field1) {   this.field1 = field1;  }  public String getField2() {   return field2;  }  public void setField2(String field2) {   this.field2 = field2;  }    @JSON(name = "newCustomName")  public String getCustomName() {   return this.customName;  } }</pre>     <p></p>    <p> </p>    <p>User类内容</p>    <p></p>    <pre class="brush:java; toolbar: true; auto-links: false;">import java.io.Serializable; public class User implements Serializable {  private static final long serialVersionUID = 1L;    private String id;  private String username;  private String password;  private String description;    public User() {     }  public User(String id, String username, String password, String description) {   this.id = id;   this.username = username;   this.password = password;   this.description = description;  }    public String getId() {   return id;  }  public void setId(String id) {   this.id = id;  }  public String getUsername() {   return username;  }  public void setUsername(String username) {   this.username = username;  }    public String getPassword() {   return password;  }  public void setPassword(String password) {   this.password = password;  }    public String getDescription() {   return description;  }  public void setDescription(String description) {   this.description = description;  } }</pre>     <p></p>    <p> </p>    <p>3. Spring配置文件</p>    <p></p>    <pre class="brush:xml; toolbar: true; auto-links: false;"><?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:aop="http://www.springframework.org/schema/aop"        xmlns:lang="http://www.springframework.org/schema/lang"        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd                            http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"        default-lazy-init="true">  <bean id="jsonExample1" class="JSONExample1" scope="prototype"/> </beans></pre>     <p></p>    <p> </p>    <p>4. Struts2配置文件</p>    <p></p>    <pre class="brush:java; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>    <include file="struts-default.xml" />  <package name="json" namespace="/json" extends="struts-default" >      <result-types>    <result-type name="json" class="org.apache.struts2.json.JSONResult"/>   </result-types>      <interceptors>    <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>   </interceptors>   <action name="jsonExample1" class="jsonExample1">    <result type="json"/>   </action>  </package> </struts></pre>     <p></p>    <p> </p>    <p></p>    <p>5. 启动web容器测试,可以使用Tomcat或者maven自带的jetty,然后访问</p>    <p>  http://localhost:8080/json/jsonExample1.action</p>    <p>  此时可以看到页面上会输出json格式的字符串。</p>    <p></p>    <p> </p>    <p>注:这里可以安装一个Firefox的JSONView扩展,此时如果请求的页面输出是json的话就会以比较好的格式显示,对于chrome浏览器来说也有相类似的插件可以使用。</p>    <p> </p>