JSF Custom ExceptionHandler not working with ajax
As described in BalusC's and A. Tijms' book "The Definitive Guide to JSF in Java EE 8" (and similarly coded in Omnifaces) I have constructed the following CustomException handler for exceptions occuring in regular and ajax requests.
public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
public CustomExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
super(wrapped);
}
@Override
public ExceptionHandler getExceptionHandler() {
return new CustomExceptionHandler(getWrapped().getExceptionHandler());
}
private class CustomExceptionHandler extends ExceptionHandlerWrapper {
private CustomExceptionHandler(ExceptionHandler wrapped) {
super(wrapped);
}
@Override
public void handle() throws FacesException {
handleException(FacesContext.getCurrentInstance());
getWrapped().handle();
}
private void handleException(FacesContext fctx) {
Iterator<ExceptionQueuedEvent> it
= getUnhandledExceptionQueuedEvents().iterator();
if (fctx == null
|| fctx.getExternalContext().isResponseCommitted()
|| !it.hasNext()) {
return;
}
Throwable t = it.next().getContext().getException();
Throwable tc = t;
while ((tc instanceof FacesException || tc instanceof ELException)
&& tc.getCause() != null) {
tc = tc.getCause();
}
renderErrorPageView(fctx, t);
it.remove();
while (it.hasNext()) {
it.next();
it.remove();
}
}
private void renderErrorPageView(FacesContext fctx, Throwable t) {
ExternalContext ctx = fctx.getExternalContext();
String uri = ctx.getRequestContextPath()
+ ctx.getRequestServletPath();
Map<String, Object> requestMap = ctx.getRequestMap();
requestMap.put(RequestDispatcher.ERROR_REQUEST_URI, uri);
requestMap.put(RequestDispatcher.ERROR_EXCEPTION, t);
String viewId = "/view/stop_error.xhtml";
Application app = fctx.getApplication();
ViewHandler viewHandler = app.getViewHandler();
UIViewRoot viewRoot = viewHandler.createView(fctx, viewId);
fctx.setViewRoot(viewRoot);
try {
ctx.responseReset();
if (!fctx.getPartialViewContext().isAjaxRequest()) {
ctx.setResponseStatus(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
ViewDeclarationLanguage vdl
= viewHandler.getViewDeclarationLanguage(fctx, viewId);
vdl.buildView(fctx, viewRoot);
fctx.getPartialViewContext().setRenderAll(true);
vdl.renderView(fctx, viewRoot);
fctx.responseComplete();
}
catch (IOException e) {
throw new FacesException(e);
}
finally {
requestMap.remove(RequestDispatcher.ERROR_EXCEPTION);
}
}
}
}
In web.xml I have
<error-page>
<exception-type>javax.faces.application.ViewExpredException</exception-type>
<location>/faces/view/expired.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/faces/view/stop_error.xhtml</location>
</error-page>
For regular requests it works like a charm. However, if there is a (Runtime)Exception in an ajax request, I only get the java script alert box that the asynchronous request returned nothing (in Development mode) or nothing (in Production mode). The above code runs fully through, however, the error page is not displayed. I am using Tomcat 9 and Mojarra 2.3.8 under Java 11. What I am doing wrong?
I tested using the composite component described in http://balusc.omnifaces.org/2013/01/composite-component-with-multiple-input.html where I throw an IllegalStateException within the updateDaysIfNecessary method which is triggered by changing the month in the repective drop down box.
ajax jsf exceptionhandler
|
show 5 more comments
As described in BalusC's and A. Tijms' book "The Definitive Guide to JSF in Java EE 8" (and similarly coded in Omnifaces) I have constructed the following CustomException handler for exceptions occuring in regular and ajax requests.
public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
public CustomExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
super(wrapped);
}
@Override
public ExceptionHandler getExceptionHandler() {
return new CustomExceptionHandler(getWrapped().getExceptionHandler());
}
private class CustomExceptionHandler extends ExceptionHandlerWrapper {
private CustomExceptionHandler(ExceptionHandler wrapped) {
super(wrapped);
}
@Override
public void handle() throws FacesException {
handleException(FacesContext.getCurrentInstance());
getWrapped().handle();
}
private void handleException(FacesContext fctx) {
Iterator<ExceptionQueuedEvent> it
= getUnhandledExceptionQueuedEvents().iterator();
if (fctx == null
|| fctx.getExternalContext().isResponseCommitted()
|| !it.hasNext()) {
return;
}
Throwable t = it.next().getContext().getException();
Throwable tc = t;
while ((tc instanceof FacesException || tc instanceof ELException)
&& tc.getCause() != null) {
tc = tc.getCause();
}
renderErrorPageView(fctx, t);
it.remove();
while (it.hasNext()) {
it.next();
it.remove();
}
}
private void renderErrorPageView(FacesContext fctx, Throwable t) {
ExternalContext ctx = fctx.getExternalContext();
String uri = ctx.getRequestContextPath()
+ ctx.getRequestServletPath();
Map<String, Object> requestMap = ctx.getRequestMap();
requestMap.put(RequestDispatcher.ERROR_REQUEST_URI, uri);
requestMap.put(RequestDispatcher.ERROR_EXCEPTION, t);
String viewId = "/view/stop_error.xhtml";
Application app = fctx.getApplication();
ViewHandler viewHandler = app.getViewHandler();
UIViewRoot viewRoot = viewHandler.createView(fctx, viewId);
fctx.setViewRoot(viewRoot);
try {
ctx.responseReset();
if (!fctx.getPartialViewContext().isAjaxRequest()) {
ctx.setResponseStatus(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
ViewDeclarationLanguage vdl
= viewHandler.getViewDeclarationLanguage(fctx, viewId);
vdl.buildView(fctx, viewRoot);
fctx.getPartialViewContext().setRenderAll(true);
vdl.renderView(fctx, viewRoot);
fctx.responseComplete();
}
catch (IOException e) {
throw new FacesException(e);
}
finally {
requestMap.remove(RequestDispatcher.ERROR_EXCEPTION);
}
}
}
}
In web.xml I have
<error-page>
<exception-type>javax.faces.application.ViewExpredException</exception-type>
<location>/faces/view/expired.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/faces/view/stop_error.xhtml</location>
</error-page>
For regular requests it works like a charm. However, if there is a (Runtime)Exception in an ajax request, I only get the java script alert box that the asynchronous request returned nothing (in Development mode) or nothing (in Production mode). The above code runs fully through, however, the error page is not displayed. I am using Tomcat 9 and Mojarra 2.3.8 under Java 11. What I am doing wrong?
I tested using the composite component described in http://balusc.omnifaces.org/2013/01/composite-component-with-multiple-input.html where I throw an IllegalStateException within the updateDaysIfNecessary method which is triggered by changing the month in the repective drop down box.
ajax jsf exceptionhandler
So you are not using the OmniFaces exceptionhandler (or OmniFaces at all)? Why not use that one?
– Kukeltje
Jan 2 at 9:33
The main reason is to minimize dependencies (jar hell) and another one is to see what is going on behind the scenes, i.e., to learn something. Hope these are reasons enough :)
– chris21k
Jan 2 at 9:52
Jar 'hell' not really since one very good/relevant jar does not make a hell (using full spring does ;-), learning... well... I can understand in some way, but I usually take the approach to learn things behind the scenes if something else that is readily available (omnifaces) does not work). But if you don't use OmniFaces, the tag on the question is not correct, since it is for where problems are. Cheers (the book is a great one btw, bought it too)
– Kukeltje
Jan 2 at 10:13
And did you read stackoverflow.com/questions/26588797/…
– Kukeltje
Jan 2 at 10:20
I have red the provided link. Unfortunetly, I cannot see why its contetns relate to the problem, as it is about redirecting. Please note that the above code does no redirect but builds the view as suggested in the book.
– chris21k
Jan 2 at 10:48
|
show 5 more comments
As described in BalusC's and A. Tijms' book "The Definitive Guide to JSF in Java EE 8" (and similarly coded in Omnifaces) I have constructed the following CustomException handler for exceptions occuring in regular and ajax requests.
public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
public CustomExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
super(wrapped);
}
@Override
public ExceptionHandler getExceptionHandler() {
return new CustomExceptionHandler(getWrapped().getExceptionHandler());
}
private class CustomExceptionHandler extends ExceptionHandlerWrapper {
private CustomExceptionHandler(ExceptionHandler wrapped) {
super(wrapped);
}
@Override
public void handle() throws FacesException {
handleException(FacesContext.getCurrentInstance());
getWrapped().handle();
}
private void handleException(FacesContext fctx) {
Iterator<ExceptionQueuedEvent> it
= getUnhandledExceptionQueuedEvents().iterator();
if (fctx == null
|| fctx.getExternalContext().isResponseCommitted()
|| !it.hasNext()) {
return;
}
Throwable t = it.next().getContext().getException();
Throwable tc = t;
while ((tc instanceof FacesException || tc instanceof ELException)
&& tc.getCause() != null) {
tc = tc.getCause();
}
renderErrorPageView(fctx, t);
it.remove();
while (it.hasNext()) {
it.next();
it.remove();
}
}
private void renderErrorPageView(FacesContext fctx, Throwable t) {
ExternalContext ctx = fctx.getExternalContext();
String uri = ctx.getRequestContextPath()
+ ctx.getRequestServletPath();
Map<String, Object> requestMap = ctx.getRequestMap();
requestMap.put(RequestDispatcher.ERROR_REQUEST_URI, uri);
requestMap.put(RequestDispatcher.ERROR_EXCEPTION, t);
String viewId = "/view/stop_error.xhtml";
Application app = fctx.getApplication();
ViewHandler viewHandler = app.getViewHandler();
UIViewRoot viewRoot = viewHandler.createView(fctx, viewId);
fctx.setViewRoot(viewRoot);
try {
ctx.responseReset();
if (!fctx.getPartialViewContext().isAjaxRequest()) {
ctx.setResponseStatus(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
ViewDeclarationLanguage vdl
= viewHandler.getViewDeclarationLanguage(fctx, viewId);
vdl.buildView(fctx, viewRoot);
fctx.getPartialViewContext().setRenderAll(true);
vdl.renderView(fctx, viewRoot);
fctx.responseComplete();
}
catch (IOException e) {
throw new FacesException(e);
}
finally {
requestMap.remove(RequestDispatcher.ERROR_EXCEPTION);
}
}
}
}
In web.xml I have
<error-page>
<exception-type>javax.faces.application.ViewExpredException</exception-type>
<location>/faces/view/expired.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/faces/view/stop_error.xhtml</location>
</error-page>
For regular requests it works like a charm. However, if there is a (Runtime)Exception in an ajax request, I only get the java script alert box that the asynchronous request returned nothing (in Development mode) or nothing (in Production mode). The above code runs fully through, however, the error page is not displayed. I am using Tomcat 9 and Mojarra 2.3.8 under Java 11. What I am doing wrong?
I tested using the composite component described in http://balusc.omnifaces.org/2013/01/composite-component-with-multiple-input.html where I throw an IllegalStateException within the updateDaysIfNecessary method which is triggered by changing the month in the repective drop down box.
ajax jsf exceptionhandler
As described in BalusC's and A. Tijms' book "The Definitive Guide to JSF in Java EE 8" (and similarly coded in Omnifaces) I have constructed the following CustomException handler for exceptions occuring in regular and ajax requests.
public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {
public CustomExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
super(wrapped);
}
@Override
public ExceptionHandler getExceptionHandler() {
return new CustomExceptionHandler(getWrapped().getExceptionHandler());
}
private class CustomExceptionHandler extends ExceptionHandlerWrapper {
private CustomExceptionHandler(ExceptionHandler wrapped) {
super(wrapped);
}
@Override
public void handle() throws FacesException {
handleException(FacesContext.getCurrentInstance());
getWrapped().handle();
}
private void handleException(FacesContext fctx) {
Iterator<ExceptionQueuedEvent> it
= getUnhandledExceptionQueuedEvents().iterator();
if (fctx == null
|| fctx.getExternalContext().isResponseCommitted()
|| !it.hasNext()) {
return;
}
Throwable t = it.next().getContext().getException();
Throwable tc = t;
while ((tc instanceof FacesException || tc instanceof ELException)
&& tc.getCause() != null) {
tc = tc.getCause();
}
renderErrorPageView(fctx, t);
it.remove();
while (it.hasNext()) {
it.next();
it.remove();
}
}
private void renderErrorPageView(FacesContext fctx, Throwable t) {
ExternalContext ctx = fctx.getExternalContext();
String uri = ctx.getRequestContextPath()
+ ctx.getRequestServletPath();
Map<String, Object> requestMap = ctx.getRequestMap();
requestMap.put(RequestDispatcher.ERROR_REQUEST_URI, uri);
requestMap.put(RequestDispatcher.ERROR_EXCEPTION, t);
String viewId = "/view/stop_error.xhtml";
Application app = fctx.getApplication();
ViewHandler viewHandler = app.getViewHandler();
UIViewRoot viewRoot = viewHandler.createView(fctx, viewId);
fctx.setViewRoot(viewRoot);
try {
ctx.responseReset();
if (!fctx.getPartialViewContext().isAjaxRequest()) {
ctx.setResponseStatus(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
ViewDeclarationLanguage vdl
= viewHandler.getViewDeclarationLanguage(fctx, viewId);
vdl.buildView(fctx, viewRoot);
fctx.getPartialViewContext().setRenderAll(true);
vdl.renderView(fctx, viewRoot);
fctx.responseComplete();
}
catch (IOException e) {
throw new FacesException(e);
}
finally {
requestMap.remove(RequestDispatcher.ERROR_EXCEPTION);
}
}
}
}
In web.xml I have
<error-page>
<exception-type>javax.faces.application.ViewExpredException</exception-type>
<location>/faces/view/expired.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/faces/view/stop_error.xhtml</location>
</error-page>
For regular requests it works like a charm. However, if there is a (Runtime)Exception in an ajax request, I only get the java script alert box that the asynchronous request returned nothing (in Development mode) or nothing (in Production mode). The above code runs fully through, however, the error page is not displayed. I am using Tomcat 9 and Mojarra 2.3.8 under Java 11. What I am doing wrong?
I tested using the composite component described in http://balusc.omnifaces.org/2013/01/composite-component-with-multiple-input.html where I throw an IllegalStateException within the updateDaysIfNecessary method which is triggered by changing the month in the repective drop down box.
ajax jsf exceptionhandler
ajax jsf exceptionhandler
edited Jan 17 at 13:05
chris21k
asked Jan 2 at 8:55
chris21kchris21k
715
715
So you are not using the OmniFaces exceptionhandler (or OmniFaces at all)? Why not use that one?
– Kukeltje
Jan 2 at 9:33
The main reason is to minimize dependencies (jar hell) and another one is to see what is going on behind the scenes, i.e., to learn something. Hope these are reasons enough :)
– chris21k
Jan 2 at 9:52
Jar 'hell' not really since one very good/relevant jar does not make a hell (using full spring does ;-), learning... well... I can understand in some way, but I usually take the approach to learn things behind the scenes if something else that is readily available (omnifaces) does not work). But if you don't use OmniFaces, the tag on the question is not correct, since it is for where problems are. Cheers (the book is a great one btw, bought it too)
– Kukeltje
Jan 2 at 10:13
And did you read stackoverflow.com/questions/26588797/…
– Kukeltje
Jan 2 at 10:20
I have red the provided link. Unfortunetly, I cannot see why its contetns relate to the problem, as it is about redirecting. Please note that the above code does no redirect but builds the view as suggested in the book.
– chris21k
Jan 2 at 10:48
|
show 5 more comments
So you are not using the OmniFaces exceptionhandler (or OmniFaces at all)? Why not use that one?
– Kukeltje
Jan 2 at 9:33
The main reason is to minimize dependencies (jar hell) and another one is to see what is going on behind the scenes, i.e., to learn something. Hope these are reasons enough :)
– chris21k
Jan 2 at 9:52
Jar 'hell' not really since one very good/relevant jar does not make a hell (using full spring does ;-), learning... well... I can understand in some way, but I usually take the approach to learn things behind the scenes if something else that is readily available (omnifaces) does not work). But if you don't use OmniFaces, the tag on the question is not correct, since it is for where problems are. Cheers (the book is a great one btw, bought it too)
– Kukeltje
Jan 2 at 10:13
And did you read stackoverflow.com/questions/26588797/…
– Kukeltje
Jan 2 at 10:20
I have red the provided link. Unfortunetly, I cannot see why its contetns relate to the problem, as it is about redirecting. Please note that the above code does no redirect but builds the view as suggested in the book.
– chris21k
Jan 2 at 10:48
So you are not using the OmniFaces exceptionhandler (or OmniFaces at all)? Why not use that one?
– Kukeltje
Jan 2 at 9:33
So you are not using the OmniFaces exceptionhandler (or OmniFaces at all)? Why not use that one?
– Kukeltje
Jan 2 at 9:33
The main reason is to minimize dependencies (jar hell) and another one is to see what is going on behind the scenes, i.e., to learn something. Hope these are reasons enough :)
– chris21k
Jan 2 at 9:52
The main reason is to minimize dependencies (jar hell) and another one is to see what is going on behind the scenes, i.e., to learn something. Hope these are reasons enough :)
– chris21k
Jan 2 at 9:52
Jar 'hell' not really since one very good/relevant jar does not make a hell (using full spring does ;-), learning... well... I can understand in some way, but I usually take the approach to learn things behind the scenes if something else that is readily available (omnifaces) does not work). But if you don't use OmniFaces, the tag on the question is not correct, since it is for where problems are. Cheers (the book is a great one btw, bought it too)
– Kukeltje
Jan 2 at 10:13
Jar 'hell' not really since one very good/relevant jar does not make a hell (using full spring does ;-), learning... well... I can understand in some way, but I usually take the approach to learn things behind the scenes if something else that is readily available (omnifaces) does not work). But if you don't use OmniFaces, the tag on the question is not correct, since it is for where problems are. Cheers (the book is a great one btw, bought it too)
– Kukeltje
Jan 2 at 10:13
And did you read stackoverflow.com/questions/26588797/…
– Kukeltje
Jan 2 at 10:20
And did you read stackoverflow.com/questions/26588797/…
– Kukeltje
Jan 2 at 10:20
I have red the provided link. Unfortunetly, I cannot see why its contetns relate to the problem, as it is about redirecting. Please note that the above code does no redirect but builds the view as suggested in the book.
– chris21k
Jan 2 at 10:48
I have red the provided link. Unfortunetly, I cannot see why its contetns relate to the problem, as it is about redirecting. Please note that the above code does no redirect but builds the view as suggested in the book.
– chris21k
Jan 2 at 10:48
|
show 5 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003518%2fjsf-custom-exceptionhandler-not-working-with-ajax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003518%2fjsf-custom-exceptionhandler-not-working-with-ajax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So you are not using the OmniFaces exceptionhandler (or OmniFaces at all)? Why not use that one?
– Kukeltje
Jan 2 at 9:33
The main reason is to minimize dependencies (jar hell) and another one is to see what is going on behind the scenes, i.e., to learn something. Hope these are reasons enough :)
– chris21k
Jan 2 at 9:52
Jar 'hell' not really since one very good/relevant jar does not make a hell (using full spring does ;-), learning... well... I can understand in some way, but I usually take the approach to learn things behind the scenes if something else that is readily available (omnifaces) does not work). But if you don't use OmniFaces, the tag on the question is not correct, since it is for where problems are. Cheers (the book is a great one btw, bought it too)
– Kukeltje
Jan 2 at 10:13
And did you read stackoverflow.com/questions/26588797/…
– Kukeltje
Jan 2 at 10:20
I have red the provided link. Unfortunetly, I cannot see why its contetns relate to the problem, as it is about redirecting. Please note that the above code does no redirect but builds the view as suggested in the book.
– chris21k
Jan 2 at 10:48