RuntimeException和Exception的区别------
Exception需要显式捕获
Struts中的异常处理和国际化消息文本密切联系
Struts中处理异常的三种方式:
1. 编程式异常
在Action中捕获异常
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UserForm formbean = (UserForm)form; UserManager userManager = new UserManagerImpl(); try { userManager.login(formbean.getUsername(), formbean.getPassword()); } catch (Exception e) { e.printStackTrace(); //创建国际化消息文本 ActionMessages errors = new ActionMessages(); ActionMessage error = new ActionMessage("errors.detail",e.getMessage()); errors.add("user.exception", error); //传递国际化消息文本 this.saveErrors(request, errors); return mapping.findForward("error"); } return mapping.findForward("success"); } |
页面显示异常信息
<body> <html:messages id="msg" message="true"> ${msg} </html:messages> <html:errors/> </body> |
注意:如果处理的是messages对象,需要添加属性message="true"
2. 声明式异常
只需要在struts配置文件中配置
<action path="/login" type="cn.yjj.test.web.UserAction" name="userForm" scope="request" input="/login_error.jsp" > <exception key="errors.detail" type="java.lang.RuntimeException" path="/login_error.jsp"/> <forward name="success" path="/login_success.jsp"/> <forward name="error" path="/login_error.jsp"/> </action> |
Key----国际化消息文本中的key
Type----异常类的完整路径
Path-----转向路径 优先级>input属性
3. 个性化异常处理(解决多个国际化文本和填充符的问题)
需要了解struts中自动处理异常的机制
异常类
package cn.yjj.test.base;
public class SystemException extends RuntimeException {
private String errorCode;
private Object[] params;
public SystemException(String message){ super(message); }
public SystemException(String message, String errorCode){ this(message, errorCode, null); }
public SystemException(String message, String errorCode, Object param){ this(message, errorCode, new Object[]{param}); }
public SystemException(String message, String errorCode, Object[] params){ super(message); this.errorCode = errorCode; this.params = params; }
public String getErrorCode() { return errorCode; }
public Object[] getParams() { return params; }
}
|
异常处理类
package cn.yjj.test.base;
import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals; 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; import org.apache.struts.util.ModuleException;
public class SystemExceptionHandler extends ExceptionHandler {
public ActionForward execute( Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws ServletException {
if(!(ex instanceof SystemException)){ return super.execute(ex, ae, mapping, formInstance, request, response); }
ActionForward forward = null; ActionMessage error = null; String property = null;
// Build the forward from the exception mapping if it exists // or from the form input if (ae.getPath() != null) { forward = new ActionForward(ae.getPath()); } else { forward = mapping.getInputForward(); }
// Figure out the error if (ex instanceof ModuleException) { error = ((ModuleException) ex).getActionMessage(); property = ((ModuleException) ex).getProperty(); } else { SystemException se = (SystemException)ex; String errorCode = se.getErrorCode(); Object[] params = se.getParams(); if(errorCode==null){ error = new ActionMessage(ae.getKey(), se.getMessage()); }else{ error = new ActionMessage(errorCode, params); } property = error.getKey(); }
this.logException(ex);
// Store the exception request.setAttribute(Globals.EXCEPTION_KEY, ex); this.storeException(request, property, error, forward, ae.getScope());
return forward;
} }
|
配置文件
<action path="/login" type="cn.yjj.test.web.UserAction" name="userForm" scope="request" input="/login_error.jsp" > <exception key="errors.detail" type="cn.yjj.test.base.SystemException" path="/login_error.jsp" handler="cn.yjj.test.base.SystemExceptionHandler"/> <forward name="success" path="/login_success.jsp"/> <forward name="error" path="/login_error.jsp"/> </action> |
1、编程式异常
* 截获异常
* 创建相应的异常消息
* 传递异常消息
* 转向相应的页面处理异常
2、声明式异常(自动处理的异常)
* 在struts-config.xml文件中配置<exeception/>标签
* 理解局部和全局exception
* 注意局部<exception/>标签需要配置到<forward/>标签的前面,详见dtd中的约束
<exeception/>标签中的属性说明:
* key:指异常信息对应的国际化消息文本,这个key值需要在国际化资源文件中定义
* type: 处理那种异常
* path: 定义一但出现异常,需要转向那个页面,如果不定义path,
默认情况下将使用<action>标签中input属性对应的页面
* scope:可以取值request和session,默认为request
* handler:异常的处理类,struts默认采用org.apache.struts.action.ExceptionHandler,
如果做个性化的异常处理可以继承此类覆写相应的方法