1

I'm building a JSF2.0 application on TomEE1.7.3, and I've created a custom ExceptionHander, and overrided handle() method, which looks like below:

@Override
public void handle() {
    for (Iterator<ExceptionQueuedEvent> it = getUnhandledExceptionQueuedEvents().iterator(); it.hasNext();) {
        ExceptionQueuedEventContext eventContext = it.next().getContext();
        FacesContext facesContext = eventContext.getContext();
        ExternalContext externalContext = facesContext.getExternalContext();
        Throwable throwable = eventContext.getException();
        if (throwable instanceof ViewExpiredException) {
            facesContext.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "",
                    "oops, view state error! This FacesMessage is sent to error.xhtml, just as expected."));
        } else {
            facesContext.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "",
                    "oops, some OTHER error! This FacesMessage is NOT sent to error.xhtml, " + 
                    "I want full stacktrace here, string length is irrelevant to the problem ..."));
        }
        externalContext.getFlash().setKeepMessages(true);
        externalContext.redirect("error.xhtml");
        it.remove();
    }
    wrapped.handle();
}

When I catch a throwable of ViewExpiredException, I CAN get FacesMessages at @PostConstruct method of "ErrorBean"(which is attached to "error.xhtml"). But if any other type of throwable comes, I can NOT get any FacesMessages. "ErrorBean" is a backing bean class with these 2 annotations:

@javax.faces.bean.ManagedBean
@javax.enterprise.context.RequestScoped

I'm assuming that its relevant to the "Life Cycle of JSF", OR maybe because I have 2 beans attached to 1 page, but I can't figure out why this is happening.

BTW, what I really want to do is output stacktrace on the same url (without http 3xx redirect), I tried something like below:

final ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)             
        facesContext.getApplication().getNavigationHandler();
nav.performNavigation("error.xhtml");

but did not work out either (another bean for this page will show up and no faces messages), so at this moment, I decide to go with redirect.

Please help me out! Thanks.

Hirofumi Okino
  • 834
  • 1
  • 8
  • 20
  • Try a `dispatch` instead of a `redirect`: http://stackoverflow.com/questions/9496430/exception-handling-in-jsf-to-print-error-message-in-the-new-page – Xtreme Biker Dec 25 '15 at 17:18
  • 1
    Uh! `@javax.faces.bean.ManagedBean`, `@javax.enterprise.context.RequestScoped`? – Tiny Dec 25 '15 at 17:31
  • @Xtreme Biker I tried it right now, but unfortunately it did not work. The ViewStateException went worse: setKeepMessages returns NullPointerException from inner classes, and there's tons of more logs flooding, so I have to stop TomEE... – Hirofumi Okino Dec 25 '15 at 17:35
  • 1
    Which version/implementation of JSF are you exactly using? There used to be some problems with the flash scope in earlier releases. I would go with [2.1.27 or 2.2.5 at least](http://stackoverflow.com/a/21277621/1199132). – Xtreme Biker Dec 25 '15 at 17:38
  • @Tiny oh, yes indeed.. I started with javax.enterprise.context.RequestScoped and javax.inject.Named but it did not work good with javax.faces.bean.ManagedProperty 's, initiating orders. I hate these similar annotation. – Hirofumi Okino Dec 25 '15 at 17:42
  • @Xtreme Biker - I thought JSF 2.1 or 2.2 are not supported in TomEE1.7.3 (JavaEE6 based) . I want to use TomEE because I and my co-workers are familiar with Tomcat. – Hirofumi Okino Dec 25 '15 at 17:48
  • `@ManagedProperty` is a JSF artifact and is only supposed to be used in JSF managed beans. If it is a bean / EJB injection point, then use `@Inject` instead. – Tiny Dec 25 '15 at 17:53
  • @Tiny - I use `@ManagedProperty` for injecting some common backing bean to another backing bean, both are `@javax.enterprise.context.RequestScoped`, thanks for tips. – Hirofumi Okino Dec 25 '15 at 17:59
  • 1
    `@Inject` fits to that requirement. – Tiny Dec 25 '15 at 18:02
  • 1
    @HirofumiOkino I'm not familiar with JavaEE environments (I am with servlet containers), but I think you could give it a try. – Xtreme Biker Dec 25 '15 at 18:03
  • @Xtreme Biker - I checked my pom.xml and there are `org.glassfish#javax.faces#2.2.12`, `org.apache.myfaces.core#myfaces-api#2.1.8`, `org.apache.myfaces.core#myfaces-impl#2.1.8`, `org.apache.myfaces.tomahawk#tomahawk20#1.1.13`, I think I'm totally fxxcked up!! I didn't intend to put any of these to my project, so it came from some samples from TomEE or others. – Hirofumi Okino Dec 25 '15 at 18:07
  • @Tiny - ok, I replaced all `@ManagedProperty(value="#{commonBean}")` to `@javax.inject.Inject` and it's working fine thank you. And is `@javax.faces.bean.ManagedBean` bad in my case? – Hirofumi Okino Dec 25 '15 at 18:14
  • 1
    `@javax.faces.bean.ManagedBean` is a partially deprecated JSF artifact. Managed beans decorated with this annotation are governed by the JSF framework itself - not CDI which is another bean management framework. New projects should use CDI as a bean management framework avoiding JSF managed beans which are considered to be semi-deprecated. – Tiny Dec 25 '15 at 18:21
  • http://stackoverflow.com/a/4347707/1391249 – Tiny Dec 25 '15 at 18:30
  • @Tiny ok, I replaced `@ManagedBean` back to `@Named` , except for one application scoped backing bean with `@javax.faces.bean.ManagedBean(eager=true)` and `@javax.faces.bean.ApplicationScoped`, I couldn't find `@Eager` annotation in OmniFaces1.12.1 ( which was also in my pom.xml). BTW, my first problem STILL EXISTS.... Do JSF beginners all have to deal with these dependencies, (semi-)deprecated, minor version stuffs? omg. I felt Spring or Struts was a lot easier to start with. – Hirofumi Okino Dec 25 '15 at 18:55
  • 1
    Equivalent to `eager=true` in application scoped JSF managed beans is not available in CDI. A stereotype `org.omnifaces.cdi.Startup` annotation can be used to get around the situation which is available, since OmniFaces 1.8 (You are already using a higher version and can thus make it available). Managing dependencies appropriately also affects other environments like Spring MVC, Struts etc, by the way. It is not related to JSF only. – Tiny Dec 25 '15 at 19:12
  • Well, every article I see says about `@Eager` or @Startup under org.omnifaces.cdi package but I don't see this package in extracted files of http://central.maven.org/maven2/org/omnifaces/omnifaces/1.12.1/omnifaces-1.12.1.jar. So, I downgraded it to 1.8.3 which really has that package and annotation. I'm testing with it and no problem so far, but this time, I think I'm gonna ask to BalusC himself, about whether this feature is (semi-)Deprecated or not.... (I googled of course) – Hirofumi Okino Dec 25 '15 at 19:38
  • I really have to get my jobs done with JSF!! I chose JSF for my project by myself, because it looked **standardized** in modern JavaEE ( no Spring required). My question is still open, someone please help me out. – Hirofumi Okino Dec 25 '15 at 19:56
  • There was a description saying that OmniFaces 1.10 - 1.12 doesn't support optional CDI. Version 1.8 supports CDI v1.0 optionally, and over versions 1.2 support CDI v1.1. http://omnifaces.org/ – Hirofumi Okino Dec 26 '15 at 09:03

0 Answers0