开发环境:JDevloper 11.1.2.2.0+ Oracle Database 10g Express Edition 10.2.0.1。
本实验接着上一个实验,由于使用Method Activity 绑定抛出异常的方法,ADF会在页面直接抛出异常,用户友好性较差,因此需要开发人员编写异常处理逻辑。
我将定义一个Task Flow Template,并在其中定义一个Exception Handler,然后让其它BTF继承该TaskFlow Template。
重要步骤说明:
1. 新建一个ADF TaskFlow Template:error-handler-task-flow
(1)拖放一个Method Activity,并Mark为Exception Handler。
(2)Method Activity指向Managed Bean中的方法:controllerExceptionHandler
public void controllerExceptionHandler() { System.out.println("######################## controllerExceptionHandler "); ControllerContext context = ControllerContext.getInstance(); ViewPortContext currentRootViewPort = context.getCurrentRootViewPort(); Exception exceptionData = currentRootViewPort.getExceptionData(); if (currentRootViewPort.isExceptionPresent()) { exceptionData.printStackTrace(); currentRootViewPort.clearException(); FacesContext facesContext = FacesContext.getCurrentInstance(); facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, exceptionData.getMessage(), null)); } }
2. 让employees-taskflow-btf继承ADF TaskFlow Template:error-handler-task-flow
3. 运行,点击Go to ProcessData Method按钮
这次页面上不再直接抛出异常,而是会调用controllerExceptionHandler,该方法使用弹出窗口显示异常:
同时,在Console日志中输出如下:
######################## controllerExceptionHandler
oracle.jbo.JboException: JboException! Data Processing Failed
at model.AppModuleImpl.processData(AppModuleImpl.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at oracle.adf.model.binding.DCInvokeMethod.invokeMethod(DCInvokeMethod.java:655)
at oracle.adf.model.binding.DCDataControl.invokeMethod(DCDataControl.java:2162)
at oracle.adf.model.bc4j.DCJboDataControl.invokeMethod(DCJboDataControl.java:3088)
at oracle.adf.model.binding.DCInvokeMethod.callMethod(DCInvokeMethod.java:266)
at oracle.jbo.uicli.binding.JUCtrlActionBinding.doIt(JUCtrlActionBinding.java:1626)
at oracle.adf.model.binding.DCDataControl.invokeOperation(DCDataControl.java:2169)
at oracle.jbo.uicli.binding.JUCtrlActionBinding.invoke(JUCtrlActionBinding.java:731)
at oracle.adf.controller.v2.lifecycle.PageLifecycleImpl.executeEvent(PageLifecycleImpl.java:402)
at oracle.adfinternal.view.faces.model.binding.FacesCtrlActionBinding._execute(FacesCtrlActionBinding.java:252)
at oracle.adfinternal.view.faces.model.binding.FacesCtrlActionBinding.execute(FacesCtrlActionBinding.java:210)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.el.parser.AstValue.invoke(Unknown Source)
at com.sun.el.MethodExpressionImpl.invoke(Unknown Source)
Project 下载:ADF_TaskFlow_PageToTaskFlow(ExceptionHandler2).7z