关于项目中的异常与Action和Properties的讨论
异常
异常分“意外异常”和“错误异常”两类
意外异常—是可以预见的异常。意外异常一般用可检测的异常(checked Exception) Exception
错误异常—不可预见的异常。例如:内存溢出而且无法解决。错误异常一般用不检测异常(unChecked Excetpion) RuntimeException
在项目中一般建议使用RuntimeException 异常来处理。
在我们的项目中一般有这么几层,视图层,Action层,业务Service层,Dao层。
一般情况我们在我们所有的业务在Service层来处理,所以我们把异常处理放在这一层。一般情况我们 throw 出这个异常。
我们在Action来调用Service的时候遇到业务逻辑的异常,让会被拦截器捕获抛出INPUT。下面举个例子说明。
Propertie
例子用到Propertie文件。这里先对Webwork中Propertie说明。
因为我们错误处理返回的错误提示都记录在Propertie文件中,新建一个Propertie文件。名称为error.properties放在classes目录下。
propertie文件都是用 key=value 来进行设置匹配的。
例如 not_exiet={0}"{1}"不存在的??? 在此文件中如果要用“”必须先转义 用 / 这个符号来进行转义。这里的{} 是指定显示的内容,这个可以在下面来进行指定。
在页面中可以用<ww:property value="%{getText('not_exiet')}"/> 或
<ww:property value="getText('not_exiet',{'参数1','参数2'})"/>得到 参数1对应{1},参数2对应{2}
error.properties 文件要配置在webwork.properties 文件中 加入 webwork.custom.i18n.resources=error 这么一句
异常
例如我有一个业务逻辑层。目的 推荐一个产品,如果推荐超过10个,那么就不能再推荐。这时候我们就可以抛出一个异常。这个异常我们定义为 usinessException 现如下
********************************************
public class BusinessException extends RuntimeException {
?????? //错误的类型
??? public static final class ERROR_TYPE{
??????? public static final String UNIQUE="unique";??//?唯一
?????? public static final String NOT_EXIET = "not_exiet";?//不存在
??????? public static final String RECOMMENDATORY_NUMBER_OUT = "recommendatory_number_out"; //越界
??? }
??? private String who;?????//谁的错误
??? pivate String errorType; //错误类型
??? pivate String value;????? //错误的值
??? public BusinessException(String who, String errorType, String value) {
??????? this.who = who;
??????? this.errorType = errorType;
??????? this.value = value;
??? }
??? public String getWho() {
??????? return who;
??? }
??? public String getErrorType() {
??????? return errorType;
??? }
??? public String getValue() {
??????? return value;
??? }
}
*******************************************************
错误异常定义好后,我们在业务层就可以抛出了
例如 推荐产品的业务方法如下
*******************************************************
public void recommendatoryTravelBureau(TravelBureau travelBureau) {
??????? List<TravelBureau> travelBureaus = getRecommendatoryTravelBureau();??//等到多有产品
???? if (travelBureaus.size() >=10) {???? //如果产品超过10 个就抛出异常
????? throw new BusinessException("travel_bureau", BusinessException.ERROR_TYPE.RECOMMENDATORY_NUMBER_OUT, "10");
??????? }
...............
}
**********************************************************
在Action中有个方法来调用此业务逻辑如下:
***********************************************
? public String recommend() throws Exception {
??????? travelBureauService.recommendatoryTravelBureau(travelBureau);
??????? return SUCCESS;
??? }
?
************************************************
这时候如果遇到异常将被拦截器捕获,返回INPUT
拦截器如下:
*********************************************************
public class ExceptionInterceptor extends StaticParametersInterceptor {
??? public String intercept(ActionInvocation invocation) throws Exception {
??????? try {
??????????? return invocation.invoke();
??????? } catch (Exception e) {
??????????? ActionSupport actionSupport = ((ActionSupport)invocation.getAction());
??????????? exception(e,actionSupport);
??????????? return Action.INPUT;
??????? }
??? }
??? private void exception(Exception e, ActionSupport actionSupport) throws Exception{
???????? if(e instanceof BusinessException){
???????????? BusinessException be = (BusinessException)e;
???????????? String who = actionSupport.getText(be.getWho());
???????????? String errorType = actionSupport.getText(be.getErrorType(),new String[]{who,be.getValue()});
???????????? actionSupport.addActionError(errorType);
???????? }else{
???????????? throw e;
???????? }
??? }
}
*********************************************************