你可以在ADF Business Components内部定义任何错误消息。
这里阐述两种自定义错误消息:ADFBC错误消息、数据库约束错误消息。
一、定制ADFBC错误消息
ADFBC错误消息一般以Jbo开头,默认格式为:
{productCode}-{errorCode}: {messageBody}
如:
JBO-25013 TooManyException
更多JBO异常,请参照oracle.jbo.CSMessageBundle
介绍一下JboException的一个构造方法及用法
JboException(java.lang.Class resBundleClass, java.lang.String errorCode, java.lang.Object[] params)
resBundleClass - ResourceBundle的子类提供消息文本。
errorCode - Resource bundle中和消息关联的错误代码。
params - 错误消息的参数。
如:
Resource bundle
import oracle.jbo.common.util.CheckedListResourceBundle; public class MyMsgBundle extends CheckedListResourceBundle // String Constants public static final String BAD_EMPNO = "101"; public static final String DEPT_NOT_FOUND = "102"; public static final String NOT_IN_DEPT = "103"; ... //Private 2-D array of key-value pairs private static final Object[][] sMessageStrings = { ... { BAD_EMPNO, "Invalid employee number." }, { DEPT_NOT_FOUND, "Department {0} does not exist." } { NOT_IN_DEPT, "Employee {0} not found in Department {1}." } ... } }
抛定制异常
throw new JboException(MyMsgBundle.class // Class of Bundle MyMsgBundle.DEPT_NOT_FOUND // String Constant new Object[1] { localVar1 } //Arguments to message );
步骤:
1.创建定制消息束文件
a.双击工程,进入Project properies
b.在Project properties对话框选择 Business Components > Options
Custom Message Bundles to use in this Project出现在对话框的底部
c.选择New创建Message Bundle Class
d.创建成功后显示如下
2. 在Message Bundle文件中定制消息
此处定制一个JBO-25013异常消息,代码如下:
package model; import java.util.ListResourceBundle; public class MessageBundle extends ListResourceBundle { private static final Object[][] sMessageStrings = new String[][] {{"25013", "Too many objects !"}}; /**Return String Identifiers and corresponding Messages in a two-dimensional array. */ protected Object[][] getContents() { return sMessageStrings; } }
当出现JBO-25013错误时,系统会提示"Too many objects !"
3.运行结果
二、定制数据库约束消息
默认ADFBC是不提供捕获数据库端的约束信息,数据库的约束消息都统一为JBO-26048,在消息束文件中定制26048消息是不起作用的,并且数据库约束消息种类繁多,也不便于直接给26048定制消息。通过自定义的DatabaseTransactionFactory解决这一问题。以下为操作步骤:
1.在MessageBundle 中的sMessageStrings 数组中加上以下内容:
{"SALE_INDEX", "索引验证:记录{0}已经存在!"}
注:SALE_INDEX为数据库中表的索引,用来确保记录的唯一性。{0}为参数,在JboException构造方法中放入的参数将会填充此处。参数值为数组的第一个,{1}、{2}...以此类推。
2.创建自定义Database Transaction Framework Extension Class
package model; import oracle.jbo.CSMessageBundle; import oracle.jbo.DMLConstraintException; import oracle.jbo.JboException; import oracle.jbo.Row; import oracle.jbo.server.DBTransactionImpl2; import oracle.jbo.server.TransactionEvent; public class CustomDBTransactionImpl extends DBTransactionImpl2 { public CustomDBTransactionImpl() { super(); System.out.println("instanced CustomDBTransactionImpl ....."); } @Override protected void postChanges(TransactionEvent transactionEvent) { try { super.postChanges(transactionEvent); } catch (DMLConstraintException e) { Row row = e.getEntityRow(); e.setExceptions(null); String errorCode = e.getErrorCode(); // String lmsg = e.getLocalizedMessage(); // jbo-26048 // A constraint violation occurred in the database while posting (writing) an entity to the database if (CSMessageBundle.EXC_DML_POST_ENTITY_CONSTRAINT_VIOLATION.equals(errorCode)) { throw new JboException(MessageBundle.class, e.getConstraintName(), new Object[] { row.toString() }); } else { throw e; } } } }
说明:DMLConstraintException 是继承JboException的一个类,负责抛出的约束异常,DMLConstraintException 的方法getConstraintName()用来获取约束名称。DMLConstraintException中还可以直接获取到再Message Bundle中定制的 文本信息,通过getLocalizedMessage()方法获取。另外DMLConstraintException对象还可以得到当前抛出异常时的EntityRow通过getEntityRow()方法获取,这里我通过重新EntityImpl的toString()方法,将错误更准确的信息传到Message Bundle中定义的SALE_INDEX的消息文本中的参数{0}。
3.创建Database Transaction Factory Class
package model; import oracle.jbo.server.DBTransactionImpl2; import oracle.jbo.server.DatabaseTransactionFactory; /** * @author King Yin */ public class CustomDBTransactionFactoryImpl extends DatabaseTransactionFactory { public CustomDBTransactionFactoryImpl() { super(); } @Override public DBTransactionImpl2 create() { return new CustomDBTransactionImpl(); } }
4.在Application Module中配置Database Transaction Factory Class
4.运行结果