一 配置异常(在struts-config.xml文件中定义),定制异常有两种:
1 全局异常(Global Exceptions)
<global-exceptions>
<exception key="invalideUser"
path="/Login.jsp" type="com.future.struts.MyException" />
</global-exceptions>
2 局部异常(Local Exception)
<action-mappings>
<action attribute="loginForm" name="loginForm"
path="/loginAction(出错转向的路径)" scope="request"
input="/login.jsp"//异常转向的页面,同exception中的path,优先级path高于input
/>
<exception key="invalideUser(异常信息的key)" path="/Error.jsp"
type="cn.itcast.ItcastException(异常类全名)" />
</action-mappings>
path:出现异常后跳转的页面
key:异常信息的键,对应的值在资源文件当中
type:所要处理的异常类
二 在相应的action中的execute方法中抛出异常
三 在异常处理页面(path所指的页面)使用html:errors标签打印异常信息
1 建立资源文件ApplicationResources.properties
内容:invaliduser=it is an invalid user(key/value)
2 配置struts-config.xml文件
<message-resources parameter="cn.itcast.ApplicationResources" key="invalideuser"/>
3 使用html:errors标记打印信息
<html:errors />
--------------------------------------------
1 编程式异常
* 截获异常
* 创建相应的异常消息
* 传递异常消息
* 转向相应的页面处理异常
2 声明式异常(自动处理的异常)
* 在struts-config.xml文件中配置<exeception/>标签
* 理解局部和全局exception
* 注意局部<exception/>标签需要配置到<forward/>标签的前面
<exeception/>标签中的属性说明:
* key:指异常信息对应的国际化消息文本,这个key值需要在国际化资源文件中定义
* type:处理那种异常
* path:定义一但出现异常,需要转向那个页面,如果不定义path,
默认情况下将使用<action>标签中input属性对应的页面
* scope:可以取值request和session,默认为request
* handler:导常的处理类,struts默认采用org.apache.struts.action.ExceptionHandler,
如果做个性化的异常处理可以继承此类复写相应的方法
-------------------------------------------------------------------------
个性异常类定义
一 方法:
1 定义MessageResources.propertices资源文件
2 在struts-config中配置<exception/>
<exception key="error.exception" type="com.bjsxt.struts.ErrorCodeException"
handler="com.bjsxt.struts.ErrorCodeException" />
3 编写异常类ErrorCodeException继承RuntimeException
public class ErrorCodeException extends RuntimeException {
private String errorCode;//这是key对应的值
private Object[] args;//这是参数集合
public ErrorCodeException(String errorCode){
this(errorCode,null);
}
public ErrorCodeException(String errorCode,Object args0){
this(errorCode,new Object[]{args0});
}
public ErrorCodeException(String errorCode,Object[] args){
this.errorCode=errorCode;
this.args=args;
}
public String getErrorCode() {
return errorCode;
}
public Object[] getArgs() {
return args;
}
}
4 编写ErrorCodeExceptionHandler类继承ExceptionHandler,
复写public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException{}方法:
public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
//添加判断
------------------------------------------------------------
if(!(ex instanceof ErrorCodeException)){
return super.execute(ex, ae, mapping, formInstance, request, response);
}
------------------------------------------------------------
ActionForward forward;
ActionMessage error;
String property;
// 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 {
//改修这个地方
//----------------------------------------------
ErrorCodeException ece=(ErrorCodeException)ex;
String errorCode=ece.getErrorCode();
Object[] args=ece.getArgs();
error = new ActionMessage(errorCode, args);
property = error.getKey();
//------------------------------------------------
//error = new ActionMessage(ae.getKey(), ex.getMessage());
//property = error.getKey();
}
this.logException(ex);
// Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
this.storeException(request, property, error, forward, ae.getScope());
if (!response.isCommitted()) {
return forward;
}
return null;
}
}
5 页面直接抛出异常
public void login(String username,String password){
if(!"admin".equals(username)){
throw new ErrorCodeException("user.not.found",username,age);
}
}
---------------------------------------------------------------------
二 方法:
1 定义MessageResources.propertices资源文件
内容:error.exception={0}
2 在struts-config中配置<exception/>
<exception key="error.exception" type="com.bjsxt.struts.ErrorCodeException"/>
3 编写异常类ErrorCodeException继承RuntimeException
public class ErrorCodeException extends RuntimeException {
public ErrorCodeException(String msg){
super(msg);
}
}
4 页面直接抛出异常
public void login(String username,String password){
if(!"admin".equals(username)){
throw new ErrorCodeException("名称"+usrname+"错误!");
}
}
--------------------------------------------------------------------------
1 ApplicationResources.properties文件
num2Meg=is not a double
2 struts-config.xml
<message-resources parameter="ApplicationResources" />
3 ActionMessages errs=new ActionMessages();
errs.add("num1Error",new ActionMessage("num2Meg"));
//errs.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("num2Meg"));
this.saveErrors(request,errs);
页面
<html:errors property="num1Error"/>
//<html:errors/>//全局不能指定property
errs.header="<script>alert("
errs.footer=")</script>"
posted on 2009-11-30 08:11
junly 阅读(489)
评论(0) 编辑 收藏 所属分类:
struts2/struts1.3/JSF