1、在action.xml中加入如下配置
<global-results> <result name="exception" type="chain">jsonException</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="exception"/> </global-exception-mappings> <!-- 出错处理 --> <action name="jsonException" class="com.taobao.ad.zuanshi.web.common.JsonExceptionHandlerAction"> <result name="success" type="json"> <param name="root">ajaxResult</param> </result> </action>
2、添加 JsonExceptionHandlerAction 异常处理类
package com.taobao.ad.zuanshi.web.common;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.alibaba.common.logging.Logger;
import com.alibaba.common.logging.LoggerFactory;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
import com.taobao.ad.zuanshi.common.exception.BOException;
/**
* 异常处理Action.
*
* @author buming.pl
*/
public class JsonExceptionHandlerAction extends ActionSupport{
/**
* serialVersionUID
*/
private static final long serialVersionUID = 6394328169901196101L;
/** The Constant log. */
protected static final Logger log = LoggerFactory.getLogger(JsonExceptionHandlerAction.class);
/** The ajax result. */
private AjaxResult ajaxResult;
/* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
Exception exception = (Exception) findException();
if (exception == null) {
ajaxResult = AjaxResult.errorResult();
} else {
writeLog(exception);
if (exception instanceof IllegalArgumentException) {
ajaxResult = AjaxResult.errorResult(getText(exception.getMessage()));
} else if (exception instanceof BOException) {
ajaxResult = AjaxResult.errorResult(getText(exception.getMessage()));
} else {
ajaxResult = AjaxResult.errorResult();
}
}
return SUCCESS;
}
private Object findException() {
ActionContext ac = ActionContext.getContext();
ValueStack vs = ac.getValueStack();
Object exception = vs.findValue("exception");
return exception;
}
/**
* Write log.
*
* @param e the e
*/
@SuppressWarnings("unchecked")
private void writeLog(Exception e) {
HttpServletRequest request = ServletActionContext.getRequest();
StringBuffer sb = new StringBuffer();
sb.append("requestUrl:[").append(ServletActionContext.getRequest().getRequestURI()).append("],");
sb.append("parameterMap:[");
Enumeration parameterNames = ServletActionContext.getRequest().getParameterNames();
while (parameterNames.hasMoreElements()) {
String pName = parameterNames.nextElement().toString();
sb.append(pName);
sb.append("=");
sb.append(request.getParameter(pName));
sb.append(parameterNames.hasMoreElements()?",":"");
}
sb.append("],");
log.error("Exception:" + sb.toString(), e);
}
public AjaxResult getAjaxResult() {
return ajaxResult;
}
}
3、在你的BO层代码中,如果需要抛出异常,可以直接使用
throw new BOException("业务出错");
4、如果是BO层参数验证的问题,可以使用spring的Assert,会抛出IllegalArgumentException异常
Assert.isNotNull(id,"ID不能为空");
5、现在支持的异常有 IllegalArgumentException,BOException,HSFRemoteException,其他的异常全部走基本异常,返回“系统错误”