正在研究,可是对json不是很熟,所以先研究哈。
导入所需jar包:
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
freemarker-2.3.15.jar
json-lib-2.1.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
struts2-json-plugin-2.1.8.1.jar
xwork-core-2.1.6.jar
JsonAction类:
package org.foerver.action; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.foerver.entity.Goods; import com.opensymphony.xwork2.ActionSupport; public class JsonAction extends ActionSupport { private static final long serialVersionUID = 2601714969474548417L; private Goods goods = new Goods(); private String message = "默认消息"; private List list = new ArrayList(); private Map<String, Object> hashMap = new HashMap<String, Object>(); public String queryList() throws Exception { System.out.println("queryList()"); for (int i = 0; i < 4; i++) { Goods item = new Goods(); item.setName("橘子"); item.setStand("每箱33个"); item.setMoney(99); item.setLeavings(43); item.setOrders(3 + i); list.add(item); } return "queryList"; } public String queryMap() { System.out.println("queryMap()"); hashMap.put("username", "admin"); hashMap.put("password", "forever"); Goods item = new Goods(); item.setName("橘子"); item.setStand("每箱27个"); item.setMoney(99); item.setLeavings(43); item.setOrders(3); hashMap.put("item", item); return "queryMap"; } public String message() { System.out.println("message()"); message = "返回普通的字符串内容"; return "message"; } public String query() { System.out.println("query()"); goods.setName("橘子"); goods.setStand("每箱27个"); goods.setMoney(99); goods.setLeavings(43); goods.setOrders(3); return "query"; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Goods getGoods() { return goods; } public void setGoods(Goods goods) { this.goods = goods; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Map<String, Object> getHashMap() { return hashMap; } public void setHashMap(Map<String, Object> hashMap) { this.hashMap = hashMap; } }
Goods类:
package org.foerver.entity; import java.io.Serializable; //商品描述 /** * @author Administrator * */ public class Goods implements Serializable{ private static final long serialVersionUID = -3430492488228556256L; public int id;//编号 public String name;//名字 public String stand;//标准 public int money;//价格 public int leavings;//库存 public int orders;//已订购数量 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 String getStand() { return stand; } public void setStand(String stand) { this.stand = stand; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } public int getLeavings() { return leavings; } public void setLeavings(int leavings) { this.leavings = leavings; } public int getOrders() { return orders; } public void setOrders(int orders) { this.orders = orders; } }
struts.xml配置:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 指定struts配置文件修改后是否自动重新加载文件 --> <constant name="struts.configuration.xml.reload" value="true"></constant> <package name="json" namespace="/json" extends="json-default"> <action name="json_*" class="org.foerver.action.JsonAction" method="{1}"> <result type="json" name="{1}"/> </action> <action name="json_view"> <result>/json.jsp</result> </action> </package> </struts>
json.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>json的使用</title> <s:url id="query_message_url" action="json_message"/> <s:url id="query_url" action="json_query"/> <s:url id="query_list_url" action="json_queryList"/> <s:url id="query_map_url" action="json_queryMap"/> <script type="text/javascript" src="${pageContext.request.contextPath}/jquery1.3.2.js"></script> <script type="text/javascript"> $(function(){ $("#btnQueryMsg").bind("click",function(){ $.ajax({ url:"<s:property value='query_message_url' />", type:"POST", dataType:"json", success:function(data){ //循环json对象的所有键值对 $("#jsonData").empty(); $.each(data,function(key,value){ $("#jsonData").append("key="+key+":value="+value+"<br/>"); }); } }); }); $("#btnQuery").bind("click",function(){ $.ajax({ url:"<s:property value='query_url' />", type:"POST", dataType:"json", success:function(data){ //循环json对象的所有键值对 $("#jsonData").empty(); $.each(data.goods,function(key,value){ $("#jsonData").append("key="+key+":value="+value+"<br/>"); }); } }); }); $("#btnQueryList").bind("click",function(){ $.getJSON( "<s:property value='query_list_url' />", function(data){ //循环json对象的所有键值对 $("#jsonData").empty(); $.each(data.list,function(key,value){ $("#jsonData").append("key="+key+"<br/>"); $.each(value,function(k,v){ $("#jsonData").append("key="+k+":value="+v+"<br/>"); }); $("#jsonData").append("************<br/>"); }); } ); }); $("#btnQueryMap").bind("click",function(){ $.ajax({ url:"<s:property value='query_map_url' />", type:"POST", dataType:"json", success:function(data){ //循环json对象的所有键值对 $("#jsonData").empty(); $.each(data.hashMap,function(key,value){ $("#jsonData").append("key="+key+":value="+value+"<br/>"); }); } }); }); }); </script> </head> <body> 返回的json格式:<div id="jsonData"></div><br/> <input type="button" value="返回普通消息" id="btnQueryMsg"/><br/> <input type="button" value="返回单个对象" id="btnQuery"/><br/> <input type="button" value="返回list对象" id="btnQueryList"/><br/> <input type="button" value="返回map对象" id="btnQueryMap"/><br/> <s:debug></s:debug> </body> </html>