首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2错误处理国际化

2012-10-18 
Struts2异常处理国际化功能:利用struts2内建的拦截器exception来处理异常信息的国际化。部署方法:1.struts.

Struts2异常处理国际化
功能:利用struts2内建的拦截器exception来处理异常信息的国际化。
部署方法:
1.struts.xml中添加国际化支持,设置资源文件名:
<struts>
<constant name="struts.custom.i18n.resources"value="exceptionMassage ">
</constant>
<constant name="struts.locale" value="en_utf-8" />

<global-results>
<result name="operationException">/WEBINF/jsp/common/operationException.jsp
</result>
</global-results>

<global-exception-mappings>
      <exception-mapping result="operationException"
exception="cn.com.base.exception.OperationException">
       </exception-mapping>
</global-exception-mappings>

</struts>

2.资源文件
exceptionMessages_en_US.properties
exceptionMessages_zh_CN.properties:
user_001:请输入正确的客户卡号!

3.建立异常常量类:ExceptionConstants.java
public class ExceptionConstants {
/**
* 业务异常关键字 user_001 --异常信息:请输入正确的客户卡号
*/
public static final String EXCEPTION_OPERATION_USER_CUS = "user_001";

4.建立异常类基类:BaseException.java
public class BaseException extends Exception {
/**
* 类序列化ID
*/
private static final long serialVersionUID = -5246579929018578466L;
/**
* 国际化信息key
*/
private String errorCode;

public BaseException() {
super();
}

public BaseException(final String errorCode) {
this.errorCode = errorCode;
}

public String getErrorCode() {
return errorCode;
}

public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}

}

5.建立异常类:OperationException.java
public class OperationException extends BaseException {
/**
* 类序列化ID
*/
private static final long serialVersionUID = -2657816827277800681L;

public OperationException() {
super();
}

public OperationException(final String errorCode) {
super(errorCode);
}
}

5.action类抛出异常
public String save(){
   ………………
  if(BlankUtil.isBlank(user.getCardid())){
      throw new OperationException(
ExceptionConstants.EXCEPTION_OPERATION_USER_CUS);
   }

}

6.异常跳转页面:operationException.jsp
  <body>
    操作提示:<s:property value="%{getText(exception.errorCode)}"/>
  </body>


热点排行