Struts1.2自定义全局异常类:
来源:互联网 发布:淘宝卖家采集软件用法 编辑:程序博客网 时间:2024/05/22 19:19
1. 修改struts-config文件,加入全局异常处理标签:
<global-exceptions>
<exception
key="errors.del" //异常代码
type="com.ls.oa.exception.SystemException" //异常类型
//异常处理类
handler="com.ls.oa.exception.SystemExceptionHandler" path="/common/exception.jsp"//异常转向页面
scope="request"//异常保存范围
>
</exception>
</global-exceptions>
2. 在MessageResources.properties文件中加入国际化消息:
errors.del = Can not delete! {0} has child org!
3. 编写异常类SystemException:
package com.ls.oa.exception;
@SuppressWarnings("serial")
public class SystemException extends RuntimeException {
private String key;//异常代码
private Object[] values;//参数
public Object[] getValue() {
return values;
}
public String getKey() {
return key;
}
public SystemException() {
super();
}
public SystemException(String message, Throwable throwable) {
super(message, throwable);
}
public SystemException(String message, Throwable throwable, String key) {
super(message, throwable);
this.key = key;
}
public SystemException(String message) {
super(message);
}
public SystemException(String message, String key) {
super(message);
this.key = key;
}
public SystemException(String message, String key, Object value) {
super(message);
this.key = key;
this.values = new Object[]{value};
}
public SystemException(String message, String key, Object[] values) {
super(message);
this.key = key;
this.values = values;
}
public SystemException(Throwable throwable) {
super(throwable);
}
public SystemException(Throwable throwable, String key) {
super(throwable);
this.key = key;
}
}
4. 编写异常处理类:SystemExceptionHandler
package com.ls.oa.exception;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
public class SystemExceptionHandler extends ExceptionHandler {
@Override
public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
ActionForward forward = null;
ActionMessage msg = null;
if(ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
}else {
forward = mapping.getInputForward();
}
if(ex instanceof SystemException) {
SystemException se = (SystemException) ex;
String key = se.getKey();
if(key == null) {
msg = new ActionMessage(ae.getKey(),ex.getMessage());
}
else {
if(se.getValue() != null) {
msg = new ActionMessage(key,se.getValue());
}
else {
msg = new ActionMessage(key);
}
}
this.storeException(request, key, msg, forward, ae.getScope());
return forward;
}
return super.execute(ex, ae, mapping, formInstance, request, response);
}
5. 编写exception.jsp页面,加入如下标签:
<html:errors/>
- Struts1.2自定义全局异常类:
- 自定义全局异常捕获
- 自定义全局异常捕获
- 自定义捕获全局异常
- 自定义捕获全局异常
- struts1.2异常处理
- struts1,2异常处理
- android自定义捕获全局异常
- Android自定义全局异常捕获
- Spring自定义全局异常处理器
- android 自定义处理全局异常
- Struts1 和Struts 2 国际化全局资源
- struts1.2x异常处理
- 全局捕获异常类
- Struts 2 全局异常
- Android自定义捕获Application全局异常
- Android自定义捕获Application全局异常
- Android自定义捕获Application全局异常
- rails 入门笔记(一)----- database.yml配置mysql
- WM 工作流设计器 v2.0(仿JBPM)
- 工程初体验之模仿收费系统
- 零星关键词
- ASP.NET中的HttpWorkerRequest对像
- Struts1.2自定义全局异常类:
- msFlexGrid 实现 直接修改单元格内容
- hibernate dbcp study转载
- trade-零星关键词
- trade
- wrong number of arguments (1 for 2)
- 100year
- C++多线程编程入门(转)
- Mac OS X瘦身多法