是否可以在没有 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 生成数据控制器。
//我的课
/**
* 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");
}
并将这两种方法绑定到 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 非例外异常