Struts2 Exception拦截 HTTP请求与Ajax请求
Struts.xml配置:
<package name="publicPackage" extends="json-default"> <interceptors> <!-- seven --> <interceptor name="exceptionInterceptor" class="org.vbs.front.common.intercept.ExceptionInterceptor"></interceptor> <interceptor-stack name="userStack"> <!-- seven --> <interceptor-ref name="exceptionInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="userStack"></default-interceptor-ref> <global-results> <result name="exception" type="json"> <param name="root">exceptionResult</param> </result> </global-results> </package>
Interceptor:
package org.vbs.front.common.intercept; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import org.apache.struts2.StrutsStatics; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; import com.opensymphony.xwork2.util.ValueStack; /* * seven * 异常获取拦截器,在action中将exception抛出, * 若一般请求则会跳转到dennied页面, * 若为ajax请求,则会返回json数据 {"success":"false","message":"抛出的错误信息" } * */ public class ExceptionInterceptor implements Interceptor { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void init() { // TODO Auto-generated method stub } @Override public String intercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub String result = null; // Action的返回值 try { // 运行被拦截的Action,期间如果发生异常会被catch住 result = invocation.invoke(); return result; } catch (Exception e) { /** * 处理异常 */ String errorMsg = "出现错误信息,请查看日志!"; /** * log4j记录日志 */ Logger log = Logger.getLogger(invocation.getAction().getClass()); log.error(errorMsg, e); HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); //分ajax请求与普通请求 if (request.getHeader("accept").indexOf("application/json") > -1 || (request.getHeader("X-Requested-With")!= null && request.getHeader("X-Requested-With").indexOf("XMLHttpRequest") > -1)) { /* seven * ajax返回json * 数据格式为 {"success":"false", * "message":"exception信息" } */ System.out.println("Ajax请求回应(exception)"); ValueStack stack = invocation.getStack(); HashMap<String, Object> exceptionResult = new HashMap<String, Object>(); stack.set("exceptionResult", exceptionResult); exceptionResult.put("success", "false"); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); exceptionResult.put("message", sw.getBuffer().toString()); return "exception"; } else{//如果是普通请求 System.out.println("正常请求回应(exception)"); return "denied"; } } } }
ajax请求:
$.ajax({ url : "subjectMaintenance!toCreateSubjectMaintenance.action", data : $("#createSubject_queryForm").serialize(), type : "post", success : function(data) { hidemask(); if(data.success=="false"){ $.messager.alert(selectLang("提示","提示","tip"), selectLang("创建失败!","創建失敗!","Create fail!")); }else{ } } });