作品:
エラーでリクエストの結果が
これは、基本的要求はHTTPエラーで終了しなければならないことを意味します。たとえばHTTP 500のように、サーバーが現時点では使用できないことを意味する可能性があります。それの
例(Javaの):JavaScriptコンソールで
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');" />
あなたは、 "HTTPエラー" が表示されるはずです。
その他の例外の場合は、oncomplete
コードが実行されます。これは、AJAXリクエストが成功するためです。したがって、コード内で例外に反応することができない場合は、自分で処理する必要があります。これを行うには多くの方法があります。あなたが "成功とエラー" を参照してくださいだろうJavaScriptコンソールで
public void handler1() {
throw new RuntimeException("Error to the browser");
}
:
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') }" />
とハンドラと私はこれを使用しています。
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} "' '()'を使わないことを意味しましたか? –
#{aBean.handler()}や#{aBean.handler}の両方の方法で違いはありません。私たちはいませんか? –
はい、この場合ははいですが、私の答えの最後を見てください。 –