在docs您可以閱讀onerror
屬性適用於:
當錯誤請求結果
這基本上意味着,請求必須HTTP錯誤結束。例如HTTP 500,這可能意味着該服務器目前無法使用。
它(JAVA)例:
public void handler2() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().responseSendError(HttpServletResponse.SC_NOT_FOUND,
"404 Page not found!");
context.responseComplete();
}
和a4j:commandButton
(XHTML)
<a4j:commandButton value="OK" actionListener="#{bean.handler2}"
onerror="console.log('HTTP error');" />
在JavaScript控制檯,你會看到 「HTTP錯誤」。
在任何其他情況下的例外oncomplete
由於AJAX請求成功結束,因此將觸發代碼。所以如果你不想對代碼中的異常作出反應,你必須自己處理。有很多方法可以做到這一點。我用這個:
public boolean isNoErrorsOccured() {
FacesContext facesContext = FacesContext.getCurrentInstance();
return ((facesContext.getMaximumSeverity() == null) ||
(facesContext.getMaximumSeverity()
.compareTo(FacesMessage.SEVERITY_INFO) <= 0));
}
而且我oncomplete
看起來是這樣的:
<a4j:commandButton value="OK" execute="@this" actionListener="#{bean.handler1}"
oncomplete="if (#{facesHelper.noErrorsOccured})
{ console.log('success'); } else { console.log('success and error') }" />
與處理程序是這樣的:
public void handler1() {
throw new RuntimeException("Error to the browser");
}
在JavaScript控制檯,你會看到 「成功和錯誤」。
BTW。最好寫actionListener="#{bean.handler3}"
而不是actionListener="#{bean.handler3()}"
。原因背後:
public void handler3() { // will work
throw new RuntimeException("Error to the browser");
}
// the "real" actionListener with ActionEvent won't work and
// method not found exception will be thrown
public void handler3(ActionEvent e) {
throw new RuntimeException("Error to the browser");
}
您的意思是'actionListener =「#{aBean.handler}」''without'()'? –
我在兩種方式#{aBean.handler()}或#{aBean.handler}中看不到任何區別。我們不? –
是的,它是這樣的情況,但也看我的答案的結尾。 –