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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值