public abstract class BaseAction extends ActionSupport {
private static final long serialVersionUID = 1L;
protected static Logger log = Logger.getLogger(BaseAction.class);
public String error(String message){
this.setAttribute("message", message);
return "error";
}
public static void returnJson(Object data, boolean end) {
returnJson(ResultDto.OK, data, end);
}
public static void returnOk(boolean end) {
returnJson(ResultDto.OK, "ok", end);
}
public static void returnLogin(boolean end) {
returnJson(ResultDto.TIME_OUT, "timeout", end);
}
public static void returnError(String errorMsg, boolean end) {
returnJson(ResultDto.WARN, errorMsg, end);
}
public static void returnJson(Object data) {
returnJson(ResultDto.OK, data, false);
}
public static void returnErrorParam(Object data) {
returnJson(ResultDto.PARAM, data);
}
public static void returnOk() {
returnJson(ResultDto.OK, "ok", false);
}
public static void returnLogin() {
returnJson(ResultDto.TIME_OUT, "timeout", false);
}
public static void returnError(String errorMsg) {
returnJson(ResultDto.WARN, errorMsg, false);
}
public static void returnJson(int code, Object data) {
returnJson(code, data, false);
}
public static String getJson(ResultDto data){
JSONObject jobj = JSONObject.fromObject(data);
return jobj.toString();
}
public static void returnJson(int code, Object data, boolean end) {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
ResultDto res = new ResultDto(code, data);
JSONObject jobj = JSONObject.fromObject(res);
out.write(jobj.toString());
} catch (Exception e) {
log.error("",e);
} finally {
out.flush();
out.close();
}
if (end) throw new MyException("");
}
public static void returnHtml(int code, Object data) {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
ResultDto res = new ResultDto(code, data);
JSONObject jobj = JSONObject.fromObject(res);
out.write(jobj.toString());
} catch (Exception e) {
log.error("",e);
} finally {
out.flush();
out.close();
}
}
public String renderHtml(String text) {
return render(text, "text/html;charset=UTF-8");
}
protected String getParameter(String name){
return this.getRequest().getParameter(name);
}
public String renderText(String text) {
return render(text, "text/plain;charset=UTF-8");
}
public String render(String text, String contentType) {
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Cache-Control", "no-cache");
response.setContentType(contentType);
response.getWriter().write(text);
response.getWriter().flush();
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
}
return null;
}
/**
* @return 设置属性
*/
protected void setAttribute(String name, Object o){
ServletActionContext.getRequest().setAttribute(name, o);
}
/**
* @return 设置属性
*/
protected String getHeaderReferer(){
return ServletActionContext.getRequest().getHeader("Referer");
}
/**
* @return 登录用户真实Ip
*/
protected String getUserIp(){
String ip = ServletActionContext.getRequest().getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = ServletActionContext.getRequest().getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = ServletActionContext.getRequest().getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = ServletActionContext.getRequest().getRemoteAddr();
}
return ip;
}
protected HttpSession getSession() {
return ServletActionContext.getRequest().getSession();
}
protected HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
public static final String USER_MENU_URL = "userMenuURL";
/**
* 设置用户所拥有的菜单URL
*/
public void setUserMenuURL(String userMenuURL){
getSession().setAttribute(USER_MENU_URL,userMenuURL);
}
/**
* 专门用于客户端ajax获取数据时发送数据方法
* @param json
*/
public void sendJson(String json){
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
out.print(json);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(out != null){
out.close();
out = null;
}
}
}
}
原文:http://www.cnblogs.com/smilesing/p/5164019.html