<html> <head> <script type="text/javascript" src="js/jquery-1.11.3.js"> </script> <script type="text/javascript"> $(document).ready(function() { $("#myButton1").click(function() { $.post("/StudyExample/json/JSONAction.action",{},function(data,status){ $("#myDiv1").html(data); }); }); }); $(document).ready(function() { $("#myButton2").click(function() { $.post("/StudyExample/JSONAction.action",status){ $("#myDiv2").html(data); }); }); }); </script> </head> <body> <div id="myDiv1"><h2>Let AJAX change this text1</h2></div> <button id="myButton1" type="button">Change Content1</button> <div id="myDiv2"><h2>Let AJAX change this text2</h2></div> <button id="myButton2" type="button">Change Content2</button> </body> </html>
import java.util.List; import java.util.Map; import java.util.HashMap; import java.util.Arrays; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.apache.struts2.ServletActionContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import com.jaeson.hibernatestudy.bean.Address; import com.jaeson.hibernatestudy.bean.User; @SuppressWarnings("all") @Scope("prototype") @Controller("jsonAction") public class JSONAction extends ActionSupport { @Override public String execute() throws Exception { map.put("jaeson",new Address("海淀",100,"1361")); map.put("chenzq",new Address("昌平",101,"1352")); return SUCCESS; } /** * 在调用getWriter之前未设置编码(既调用setContentType或者setCharacterEncoding方法设置编码),* HttpServletResponse则会返回一个用默认的编码(既ISO-8859-1)的PrintWriter实例。这样就会 * 造成中文乱码。而且设置编码时必须在调用getWriter之前设置,不然是无效的。 */ public void writeJSON() throws IOException{ HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); /*String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"张三\",\"say\":\"Hello,i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}"; out.println(jsonString); out.flush(); out.close();*/ Map<String,User> map = new HashMap<String,User>(); User user1 = new User(); user1.setId("1001"); user1.setUserName("chenzq"); user1.setAddress(new Address("beijing",100021,"134")); User user2 = new User(); user2.setId("1002"); user2.setUserName("jaesonchen"); user2.setAddress(new Address("shanghai",200021,"133")); map.put("user1",user1); map.put("user2",user2); JSONObject json = new JSONObject(); json.accumulate("map",map); //JSONObject json = JSONObject.fromObject(map); out.println(json.toString()); out.flush(); out.close(); } private String fieldString = "String value"; private int fieldInt = 100; private double fieldDouble = 99.1; private boolean fieldBoolean = false; private int[] arrayInt = {101,102,103}; private List<String> list = Arrays.asList("Buenos Aires","Córdoba La","La Plata"); private Map<String,Address> map = new HashMap<String,Address>(); //field without getter method 不会被序列化 private String fieldNoGetter = "NoGetter value"; public String getFieldString() { return fieldString; } public int getFieldInt() { return fieldInt; } public double getFieldDouble() { return fieldDouble; } public boolean isFieldBoolean() { return fieldBoolean; } public int[] getArrayInt() { return arrayInt; } public List<String> getList() { return list; } public Map<String,Address> getMap() { return map; } /** public String getFieldNoGetter() { return fieldNoGetter; }*/ }
不使用JsonResult的配置:
<action name="JSONAction" class="jsonAction" method="writeJSON" />
使用JsonResult的配置:
<package name="json" extends="json-default" namespace="/json"> <action name="JSONAction" class="jsonAction"> <result type="json"> <!-- 指定内容类型,默认为application/json,IE浏览器会提示下载 --> <param name="contentType">text/html</param> <!-- 默认将会序列所有有返回值的getter方法的值,而无论该方法是否有对应属性。 root代表从哪里开始序列化,如果root是对象则会将root对象中所有的getter方法都进行序列化。 --> <!-- <param name="root">map</param> --> <!-- 指定是否序列化空的属性,默认为false,序列化空属性,对象为null,string为"" --> <!-- <param name="excludeNullProperties">true</param> --> <!-- 这里指定将序列化map中的那些属性 --> <!-- <param name="includeProperties">userList.*</param> --> <!-- 这里指定将要从map中排除那些属性,这些排除的属性将不被序列化,一般不与上边的参数配置同时出现。 如果相同属性同时指定exclude和include,则exclude的优先级高,因此不序列化该属性--> <!-- <param name="excludeProperties"> SUCCESS </param> --> </result> </action> </package>