欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个 ajax 的工具包。可用于strutsX.X

程序员文章站 2022-05-26 13:58:44
...
package com.util.comm.actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * [email protected]
 * @author linlin yu
 * @version 1.0
 */
public class BaseAction extends ActionSupport implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public void outJsonString(String json) {
		getResponse().setContentType("text/javascript;charset=UTF-8");
		outString(json);
	}

	public void outJson(Object obj) {
		outJsonString(JSONObject.fromObject(obj).toString());
	}

	public void outJsonArray(Object array) {
		outJsonArray(JSONArray.fromObject(array).toString());
	}

	public void outString(String json) {
		try {
			PrintWriter out = getResponse().getWriter();
			out.write(json);
		} catch (IOException e) {
		}
	}

	public void outXMLString(String xmlStr) {
		getResponse().setContentType("application/xml;charset=UTF-8");
		outString(xmlStr);
	}

}

 

 

下面还有一个包