1. 首先是在struts2的工程里添加struts2-json-plugin.xxx.jar库,如果是使用的maven,添加以下dependency
1 2 3 4 5 | < dependency > < groupId >org.apache.struts</ groupId > < artifactId >struts2-json-plugin</ artifactId > < version >2.1.8.1</ version > </ dependency > |
2. 创建Action类和一个用到的User类
Action类内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import com.opensymphony.xwork2.Action; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class JSONExample2 { private List<User> userList = new ArrayList<User>(); private Map<String, User> userMap = new LinkedHashMap<String, User>(); public String getMyUserList() { for ( int i = 0 ; i < 3 ; i++) { User user = new User( "id_" + i, "username_" + i, "password_" + i, "desc_" + i); userList.add(user); } return Action.SUCCESS; } public String getMyUserMap() { for ( int i = 0 ; i < 3 ; i++) { User user = new User( "id_" + i, "username_" + i, "password_" + i, "desc_" + i); userMap.put(user.getId(), user); } return Action.SUCCESS; } 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; } } |
User类内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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; } } |
3. Spring配置文件
1 2 3 4 5 6 7 8 9 10 11 | <?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= "jsonExample2" class = "JSONExample2" scope= "prototype" /> </beans> |
4. Struts2配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?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= "getMyUserList" class = "jsonExample2" method= "getMyUserList" > <result type= "json" > <param name= "root" >userList</param> </result> </action> <action name= "getMyUserMap" class = "jsonExample2" method= "getMyUserMap" > <result type= "json" > <param name= "root" >userMap</param> </result> </action> </ package > </struts> |
5. 新建一个jsp文件json.jsp用来做测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | < html > < head > < title ></ title > < mce:script type = "text/javascript" src = "http://www.google.com/jsapi?key=helloworld" mce_src = "http://www.google.com/jsapi?key=helloworld" ></ mce:script > < mce:script type = "text/javascript" > <!-- google.load("jquery", "1.4.1"); function getMyUserList() { var url = 'json/getMyUserList.action'; var params = { }; jQuery.post(url, params, function(data) { var s = ""; for (var idx = 0; idx < data.length; idx++) { s += "user["+idx+"].id=" + data[idx].id + "/n"; } alert(s); }, 'json'); } function getMyUserMap() { var url = 'json/getMyUserMap.action'; var params = { }; jQuery.post(url, params, function(data) { var s = ""; for (var o in data) { s += "user.id=" + data[o].id + ","; s += "user.username=" + data[o].username + "/n"; } alert(s); }, 'json'); } // --> </ mce:script > </ head > < body style = "margin: 20px;" mce_style = "margin: 20px;" > < h3 >json test</ h3 > < ul > < li >< a href = "#" mce_href = "#" onclick = "getMyUserList()" >getMyUserList</ a ></ li > < li >< a href = "#" mce_href = "#" onclick = "getMyUserMap()" >getMyUserMap</ a ></ li > </ ul > </ body > </ html > |
6. 启动web容器测试,可以使用Tomcat或者maven自带的jetty,然后访问
http://localhost:8080/json/json.jsp
此时可以点击页面上的两个链接来测试结果。
联系客服