struts.xml
<package name="demo4" extends="json-default" namespace="/demo4"> <action name="*_*" class="com.lan.action.json.{1}Action" method="{2}"> <result name="success" type="json"></result> </action> </package>
action
package com.lan.action.json; import java.util.HashMap; import java.util.Map; import com.opensymphony.xwork2.ActionSupport; /** * Struts2-Json 返回一个map * * @author wy * */ public class Demo4Action extends ActionSupport { private Map<String, String> map = new HashMap<String, String>(); private String value; public String gainMap() { map.put("key", value); return SUCCESS; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
js
/* * ajax Map */ //load $(function() { $("#map").load("demo4/Demo4_gainMap.action", {value:"load"}, function(data) { }); }); // post $(function() { $("#button-map-001").click(function() { $.post("demo4/Demo4_gainMap.action", { value : $("#value").val() }, function(data) { $("#map-value").html(data.map.key); }); }); }); // get $(function() { $("#button-map-002").click(function() { $.get("demo4/Demo4_gainMap.action", { value : $("#value").val() }, function(data) { $("#map-value").html(data.map.key); }); }); }); // ajax $(function() { }); $("#button-map-003").click(function() { $.ajax({ url : "demo4/Demo4_gainMap.action", type : "post", data : "value=" + $("#value").val(), dataType : "JSON", success : function(data) { $("#map-value").html(data.map.key); } }); });