java - ADF 面对 : It's possible to Customize Error Handling without ADF BC

标签 java error-handling oracle-adf jdeveloper

是否可以在没有 ADF BC 的情况下自定义 ADF Faces 中的错误处理?

这是我的方法。
MyErrorHandler 类扩展 DCErrorHandlerImpl

    public class MyErrorHandler extends DCErrorHandlerImpl {
    private static final ADFLogger logger = ADFLogger.createADFLogger(MyErrorHandler.class);
    private static ResourceBundle rb =
        ResourceBundle.getBundle("error.handling.messages.ErrorMessages_de_DE");

    public MyErrorHandler() {
        super(true);
    }

    public MyErrorHandler(boolean setToThrow) {
        super(setToThrow);
    }

    public void reportException(DCBindingContainer bc, java.lang.Exception ex) {
        disableAppendCodes(ex);
        logger.info("entering reportException() method");
        BindingContext ctx = bc.getBindingContext();
        if (ex instanceof NullPointerException) {
            logger.severe(ex);
            JboException e = new JboException(rb.getString("STANDARD_ERROR_MESSAGE"));
            super.reportException(bc, e);
        } else if (ex instanceof RowValException) {

            Object[] exceptions = ((RowValException) ex).getDetails();
            if (exceptions != null) {
                for (int i = 0; i < exceptions.length; i++) {
                    if (exceptions[i] instanceof RowValException) {
                        this.reportException(bc, (Exception) exceptions[i]);
                    } else if (exceptions[i] instanceof AttrValException) {
                        super.reportException(bc, (Exception) exceptions[i]);
                    }
                }
            } else {
                this.reportException(bc, ex);
            }

        } else if (ex instanceof TxnValException) {
            Object[] exceptions = ((TxnValException) ex).getDetails();
            if (exceptions != null) {
                for (int i = 0; i < exceptions.length; i++) {
                    if (exceptions[i] instanceof RowValException) {
                        this.reportException(bc, (Exception) exceptions[i]);
                    } else {
                        super.reportException(bc, (Exception) exceptions[i]);
                    }
                }
            } else {
                super.reportException(bc, ex);
            }
        }

        else if (ex instanceof oracle.jbo.DMLException) {
            JboException e = new JboException(rb.getString("STANDARD_ERROR_MESSAGE"));
            super.reportException(bc, e);
        } else if (ex instanceof javax.xml.ws.WebServiceException) {
            JboException e = new JboException(rb.getString("WEB_SERVICE_EXCEPTION"));
            super.reportException(bc, e);
        }

        else if (ex instanceof JboException) {
            super.reportException(bc, ex);
        }

    }


    public static FacesMessage getMessageFromBundle(String key, FacesMessage.Severity severity) {
        ResourceBundle bundle =
            ResourceBundle.getBundle("sahaj.apps.vleadministration.view.resources.VLEAdministrationUIBundle");
        String summary = JSFUtils.getStringSafely(bundle, key, null);
        String detail = JSFUtils.getStringSafely(bundle, key + "_detail", summary);
        FacesMessage message = new FacesMessage(summary, detail);
        message.setSeverity(severity);
        return message;
    }

    private void disableAppendCodes(Exception ex) {
        if (ex instanceof JboException) {
            JboException jboEx = (JboException) ex;
            jboEx.setAppendCodes(false);
            Object[] detailExceptions = jboEx.getDetails();
            if ((detailExceptions != null) && (detailExceptions.length > 0)) {
                for (int z = 0, numEx = detailExceptions.length; z < numEx; z++) {
                    System.err.println("Detailed Exception  : "+  detailExceptions[z].toString());
                    disableAppendCodes((Exception) detailExceptions[z]);
                }
            }
        }
    }

    @Override
    protected boolean skipException(Exception ex) {
        if (ex instanceof JboException) {
            return false;
        } else if (ex instanceof SQLIntegrityConstraintViolationException) {
            return true;
        }
        return super.skipException(ex);
    }


    private String handleApplicationError(String errorMessageRaw) {
        String errorMessageCode = getErrorCode(errorMessageRaw);

        // application error code
        String errorMessage = null;

        for (String key : errorPrefixes) {
            if (errorMessageCode.startsWith(key)) {
                try {
                    errorMessage = rb.getString(errorMessageCode);
                } catch (MissingResourceException mre) {
                    // application error code not found in the bundle,
                    // use original message
                    return errorMessageRaw;
                }
                break;
            }
        }
        // return the formated application error message
        return errorMessage;
    }


    private String getErrorCode(String errorMessageRaw) {
        // check for null/empty error message
        if (errorMessageRaw == null || errorMessageRaw.isEmpty()) {
            return errorMessageRaw;
        }

        int start = 0;
        String currentErrorCodePrefix = null;
        int count = 0;
        // check for error message
        for (String errorCode : errorPrefixes) {
            count += 1;
            start = errorMessageRaw.indexOf(errorCode);
            if (start >= 0) {
                currentErrorCodePrefix = errorCode;
                start += currentErrorCodePrefix.length();
                break;
            }
            if (count == errorPrefixes.size())
                return errorMessageRaw;
        }

        int endIndex = start + 5;
        // get the CURRENT error code
        return currentErrorCodePrefix + errorMessageRaw.substring(start, endIndex);
    }


    @Override
    public String getDisplayMessage(BindingContext bindingContext, Exception exception) {
        String data=super.getDisplayMessage(bindingContext, exception);
        System.err.println("Exception DATA : "+  data);
        String msg= handleApplicationError(data);
        System.err.println("Exception MSG : "+  msg);
        return msg;
    }

    @Override
    public DCErrorMessage getDetailedDisplayMessage(BindingContext bindingContext, RegionBinding regionBinding,
                                                    Exception exception) {
        return super.getDetailedDisplayMessage(bindingContext, regionBinding, exception);
    }

    private static Set<String> errorPrefixes = new HashSet<String>();
    static {
        errorPrefixes.add("JBO-");
        errorPrefixes.add("ORA-");
        errorPrefixes.add("DCA-");
    }

}

在我的 DataBinding.cpx
<Application xmlns="http://xmlns.oracle.com/adfm/application" version="12.1.2.66.68" id="DataBindings"
             SeparateXMLFiles="false" Package="de.nkk.oasis.ui.web" ClientType="Generic"
             ErrorHandlerClass="MyErrorHandler">

之后,我从 Myclass 生成数据 Controller 。

//我的课
/**
 * method throwing a Nullpointer exception
 */
public void throwNPE() {
  Object o = null;
  String s = o.toString();
  //bang occurs in the line above, no need for any more code
  //...
}

    /**
 * Method that throws a single JboException
 */
public void throwJboException(){
    throw new JboException("This is a JboException thrown in ADF BC");
}

并将这两种方法绑定(bind)到 JSF
<af:button actionListener="#{bindings.throwNPE.execute}" text="throwNPE"
 disabled="#{!bindings.throwNPE.enabled}" id="b2"/>

 <af:button actionListener="#{bindings.throwJboException.execute}" text="throwJboException"
 disabled="#{!bindings.throwJboException.enabled}" id="b3"/>

现在我的问题来了:
每当我单击一个按钮时,我都会得到

DCA-29000 无异常(exception)异常

最佳答案

尝试删除

disabled="#{!bindings.throwNPE.enabled}"


disabled="#{!bindings.throwJboException.enabled}"

关于java - ADF 面对 : It's possible to Customize Error Handling without ADF BC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18782270/

相关文章:

r - 解释 R 中的错​​误消息,将一列分成两列并创建一个新的数据框

php - set_error_handler回调函数中的$ errno是什么?

oracle-adf - 删除 <af :query> 中的尾随空格

java - 在完全离线的环境下开发Spring Boot项目,可能吗?

java - Jenkins:Gerrit 触发器问题的设置

java - 在这种情况下应该使用哪个集合?

Java:打印带有页眉和页脚的自定义(可打印)页面

r - 错误 : $ operator not defined for this S4 class

javascript - 获取oracle adf中java back bean中js函数的返回值

java - 如何换行 <af :commandLink> in Oracle ADF 11g application 的文本