涉及
struts.xml、return "success"、return null、request.getWrite().write(str)
Ajax、success: function(data) {}
Struts2、Ajax
1.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> <package name="/" extends="json-default"><!-- 一般是 struts-default,如果用到 ajax,则 json-default --> <action name="calendarAqiAction" class="com.success.action.CalendarAqiAction"></action> <action name="xxxAction" class="com.success.action.Xxxction"> <result name="success" type="json"></result> </action> </package> </struts>
2.Java
package com.success.action; import java.util.ArrayList; import java.util.List; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.success.util.BaseAction; public class XxxAction extends BaseAction { /** * */ private static final long serialVersionUID = 1L; private String jsonArray; /** * * @return "success" * @throws Exception */ public String getJsonData() throws Exception { // 后台代码开始用 fastjson 处理的数据,最后返回数据的时候选择了 net.sf.json(提及下,don't care) jsonArray = new net.sf.json.JSONArray().fromObject(list).toString(); return "success"; } // getter && setter public String getJsonArray() { return jsonArray; } public void setJsonArray(String jsonArray) { this.jsonArray = jsonArray; } }
3.Ajax
$.ajax({ url: '', data: '', async: true, success: function(data){ // 后台回传的 jsonArray,用 data.jsonArray 来调用(ajax 基本操作) // 这样就获得了 JSON 对象 jsonObj var jsonObj = eval('('+data.jsonArray+')'); }, error: function(){ alert('系统错误!'); } });
request.getWrite().write(s)、Struts2、Ajax
1.request.getWrite().write(s)
package com.ikoo; import java.io.IOException; import java.io.PrintWriter;// java.io import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext;// struts2 import org.junit.Test; public class BaseAction{ public void send(String s) { HttpServletResponse response = ServletActionContext.getResponse();// 这里用的Struts2 response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); try { PrintWriter printWriter = response.getWriter(); printWriter.write(s); // response.getWriter().write(s); // 作用:直接向客户端页面返回数据(将数据直接写到客户端页面上) } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2.Java
package com.success.action; import java.util.ArrayList; import java.util.List; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.success.util.BaseAction; public class XxxAction extends BaseAction { /** * */ private static final long serialVersionUID = 1L; private String jsonArray; /** * * @return "success" * @throws Exception */ public String getJsonData() throws Exception { // 后台代码开始用 fastjson 处理的数据,最后返回数据的时候选择了 net.sf.json(提及下,don't care) jsonArray = new net.sf.json.JSONArray().fromObject(list).toString(); // 将数据返回到客户端,必须要 return null; reponse.getWrite().write(jsonArray); return null;// !!! } // getter && setter public String getJsonArray() { return jsonArray; } public void setJsonArray(String jsonArray) { this.jsonArray = jsonArray; } }
3.Ajax
$.ajax({ url: '', data: '', async: true, success: function(data){ // 后台 response.getWrite().write(jsonAray),直接返回到客户端页面上的 jsonArray,在 success 中,就是 data,即 data = jsonArray // data 就是后台 write(s) 的 s }, error: function(){ alert('系统错误!'); } });