struts1.2 全局异常
对于一个action若发生异常则先查找本身action是否有相应异常的配置,若无则查找全局异常配置,还没有则报错。异常处理一定会用到资源文件
<!--
??????????? key指定信息在资源文件中的键值
??????????? type指发生的异常类型。
??????????? bundle使用哪个资源文件,不配置则使用默认的资源文件
??????????? ?-->
??? <exception key="user.register" type="edu.yzu.exception.UserRegisterException" bundle="exception" path="/error.jsp"/>
?
?
??? <global-exceptions>
??? <exception? key="user.register" type="edu.yzu.exception.UserRegisterException" bundle="exception" path="/error.jsp"/>
??? </global-exceptions>
???? 3.对于一个异常,通过会转入另外一个页面,提示错误,struts1.x在此设计有些不合理,一旦转入异常提示页面时,即使这个页面设置了isErrorPage="true"但是它的内置有exception对象依然为空,所以不能用exception得到异常中的信息,只能用<html:errors />来得到,一大失误哦,这也就是说struts1.x的异常处理是离不开资源文件的,因为异常的信息只能放在资源文件中。
?
?
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/>
?
?