今天一天在研究struts的异常处理,下面对自己学习到的相关知识做个简单的总结。
1. 国际资源文件的加载
刚开始由于对struts的国际化没有做一定的了解,认为没有必要。当然现在也没做什么打算去深究它,到需要用到的时候在做简单的研究吧。自己建立国际化资源文件,将其放置于src文件夹下,起名为MessageResources_zh_CN.properties。由于目前我用到的MyEclipse是7。0的版本,因此不需要利用java自带的工具去将汉字转化为ASCII。可以直接输入汉字到时MyEclipse可以自己自动地进行转化。
在struts-config.xml文件中加载该文件。
<message-resources parameter="MessageResources_zh_CN" />
2. 声明式异常
声明式异常采用的方式是先对各种将出现的异常以类的方式进行编程。然后通过抛出异常在配置文件中找到异常对应的key值,从而输出错误信息。
具体步骤:
一.建立异常类
1. public class UserNotFoundException extends RuntimeException {
2.
3. public UserNotFoundException(String message){
4. super(message);
5. System.out.println("用户名不能为空");
6. }
7. }
8. 当然这里可以对构造函数不带参数。
二 抛异常
if(username.trim().equals("")){ //该方式为声明式异常
System.out.println("用户名不能为空");
throw new UserNotFoundException("用户名不能为空");
}
三.写资源文件
user.empty=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A
四.配置struts-config.xml文件中的异常处理方式
由于我针对的是登陆时判断输入的值是否为空,因此可以直接在action后面写异常
处理,也就是一个局部异常处理。同样也可以采用全局的异常处理。在这里采用局
部异常处理方式。
局部异常处理注意两个属性:一个事在exception中的path属性,另一个是在action
中的input属性,这两个都是异常时页面的跳转,struts先检查path后input在全
局中的exception。
<action-mappings>
<action path="/login"
type="bjsydx.test.action.LoginAction"
name="loginForm"
scope="request"
input="/login/login.jsp"
>
<exception key="user.empty" type="bjsydx.test.Exception.UserNotFoundException" />
<exception key="psw.empty" type="bjsydx.test.Exception.PasswordNotCorrectException"></exception>
<forward name="success" path="/user/userlist.jsp"></forward>
<forward name="error" path="/login/login.jsp"></forward>
</action>
五.进行网页界面的处理。使用<html:errors />
六.运行后将出现需要的效果。
3. 个性化异常(用到国际化资源文件)
一.建立异常类
public class ErrorCodeException extends RuntimeException {
private String errorCode; // 错误码
private Object[] args; // 占位符对象
public ErrorCodeException(String errorCode) {
this(errorCode,null);
}
public ErrorCodeException(String errorCode,Object args) {
this(errorCode,new Object[]{args});
}
public ErrorCodeException(String errorCode,Object []args) {
this.errorCode=errorCode;
this.args=args;
}
public String getErrorCode() {
return errorCode;
}
public Object[] getArgs() {
return args;
}
}
二. 抛异常
if(username.trim().equals("")){ //该方式为个性化异常,自己写异常处理方式
System.out.println("用户名不能为空");
throw new ErrorCodeException("user.empty",username);
}
三. 写资源文件(同上)
四.配置struts-config.xml文件中的异常处理方式(全局异常处理方式)
<global-exceptions>
<exception key="error.exception" type="bjsydx.test.Exception.ErrorCodeException"
/>
</global-exceptions>
在这里将发现key值根本没法找到,因此在这里还需要写一个Handler类
五. 写处理异常类。
public class ErrorCodeExceptionHandler extends ExceptionHandler{
public ActionForward execute(
Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
/*
* 用于判断得到的异常是否属于自己定义抛出的异常,如果不是则交给struts异常处理程序进行处理
* 如果是ErrorCodeException抛出的异常,则采取的方式是利用自己定义的处理方式进行处理。
*/
if(!(ex instanceof ErrorCodeException)){
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 {
/*
* 利用采集到的异常得到errorCode和占位符。
*/
ErrorCodeException ecx=(ErrorCodeException)ex; //将该抛出的异常转化为ErrorCodeException类型,从而可以得到其errorCode和占位符
String errorCode=ecx.getErrorCode();
Object[]args=ecx.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());
return forward;
}