package xxx
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;
public class AjaxUtil {
// AJAX输出,返回null
public String ajax(String content, String type) {
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType(type + ";charset=UTF-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.getWriter().write(content);
response.getWriter().flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// AJAX输出文本,返回null
public String ajaxText(String text) {
return ajax(text, "text/plain");
}
// AJAX输出HTML,返回null
public String ajaxHtml(String html) {
return ajax(html, "text/html");
}
// AJAX输出XML,返回null
public String ajaxXml(String xml) {
return ajax(xml, "text/xml");
}
// 根据字符串输出JSON,返回null
public String ajaxJson(String jsonString) {
return ajax(jsonString, "text/html");
}
// 根据Map输出JSON,返回null
public String ajaxJson(Map<String, String> jsonMap) {
JSONObject jsonObject = new JSONObject(jsonMap);
String jsonString = "["+ jsonObject.toString()+ "]";
return ajax(jsonString,"text/html");
}
// 输出JSON警告消息,返回null
public String ajaxJsonWarnMessage(String message) {
Map<String, String> jsonMap = new HashMap<String, String>();
jsonMap.put(STATUS, WARN);
jsonMap.put(MESSAGE, message);
JSONObject jsonObject = new JSONObject(jsonMap);
return ajax(jsonObject.toString(), "text/html");
}
// 输出JSON成功消息,返回null
public String ajaxJsonSuccessMessage(String message) {
Map<String, String> jsonMap = new HashMap<String, String>();
jsonMap.put(STATUS, SUCCESS);
jsonMap.put(MESSAGE, message);
JSONObject jsonObject = new JSONObject(jsonMap);
return ajax(jsonObject.toString(), "text/html");
}
// 输出JSON错误消息,返回null
public String ajaxJsonErrorMessage(String message) {
Map<String, String> jsonMap = new HashMap<String, String>();
jsonMap.put(STATUS, ERROR);
jsonMap.put(MESSAGE, message);
JSONObject jsonObject = new JSONObject(jsonMap);
return ajax(jsonObject.toString(), "text/html");
}
// 获取Response
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
// 设置页面不缓存
public void setResponseNoCache() {
getResponse().setHeader("progma", "no-cache");
getResponse().setHeader("Cache-Control", "no-cache");
getResponse().setHeader("Cache-Control", "no-store");
getResponse().setDateHeader("Expires", 0);
}
/**
* 将查询到的List封装成json格式所需的JSONArray格式<br/>
* 因为jsonArray在存放对象时易引起递归,比如存放equipment属性时,会递归保存所属车间属性和公司属性然后在公司和车间之间递归调用<br />
* 所以使用toSting都放String了
* @param tlist
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public <T> JSONArray ListToJson(List<T> tlist) throws IllegalArgumentException, IllegalAccessException{
Map<String,String> jsonMap = new HashMap<String, String>();
JSONArray jsonArray = new JSONArray();
//获取T的实际类型Class
T t1=tlist.get(0);
Class clazz=t1.getClass();
Class superClazz=clazz.getSuperclass();
Field[] fileds=clazz.getDeclaredFields();
for(T entry:tlist){
//将每一个field的值作为value放在以fieldName命名的key里
for(Field f:fileds) {
f.setAccessible(true);
jsonMap.put(f.getName(), f.get(entry)==null?"":f.get(entry).toString());
}
jsonArray.put(jsonMap);
}
return jsonArray;
}
public static final String STATUS = "status";
public static final String WARN = "warn";
public static final String SUCCESS = "success";
public static final String ERROR = "error";
public static final String MESSAGE = "message";
}
网友评论