Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Catch Exceptions In JSF Page (View) : Problems  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two Classes that extend from ExceptionHandlerWrapper & ExceptionHandlerFactory for catching any exceptions that occur in the view layer and navigate to error page and show this error. So I need store or save this exceptions somewhere but I can`t get this exception message. I tried to get this message in session but I need another way to save this message with out ViewScope and PageFlowScope and RequestScope.


[code=java][public class CustomExceptionHandler extends ExceptionHandlerWrapper {
   private ExceptionHandler wrapped;


   public CustomExceptionHandler(ExceptionHandler wrapped) {
       this.wrapped = wrapped;
   }

   @Override
   public ExceptionHandler getWrapped() {
       return wrapped;
   }

   @Override
   public void handle() throws FacesException {
       Iterator iterator = getUnhandledExceptionQueuedEvents().iterator();

       while (iterator.hasNext()) {
           ExceptionQueuedEvent event = (ExceptionQueuedEvent) iterator.next();
           ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();


           Throwable throwable = context.getException();
           FacesContext fc = FacesContext.getCurrentInstance();


            Map<String, Object> sessionMap = fc.getExternalContext().getSessionMap();
            NavigationHandler navigator = fc.getApplication().getNavigationHandler();
           
           
           AdfFacesContext adfc = AdfFacesContext.getCurrentInstance();
           
        // Map<String,Object> valueViewScope =   adfc.getViewScope();
           

           try {
               //  Flash flash = fc.getExternalContext().getFlash();
               // Put the exception in the flash scope to be displayed in the error
               // page if necessary ...
               //  flash.put("errorDetails", throwable.getMessage());

              // ADFContext.getCurrent().getSessionScope().put("errorDetails", throwable.getMessage());

               sessionMap.put("errorDetails", throwable.getMessage());

               System.out.println("the error is put in the Session: " + throwable.getMessage());

               //NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler();
               navigator.handleNavigation(fc, null, "ErrorPage?faces-redirect=true");
               // navigationHandler.handleNavigation(fc, null, "ErrorPage?faces-redirect=true");

               fc.renderResponse();
           } finally {
             
           }
       }

       // Let the parent handle the rest
       getWrapped().handle();
   }
}
]


[code=java][public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {

   private ExceptionHandlerFactory parent;
   
   public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) {
     this.parent = parent;
   }
   
   @Override
   public ExceptionHandler getExceptionHandler() {
     ExceptionHandler result = new CustomExceptionHandler(parent.getExceptionHandler());
     return result;
   }
}]


My Problems I need to save This Exception Message And Show This Message Any Time
I tried Message In SessionScope And Already occur This Error In Error Page
But I need Another Way To Save This Message
Please WithOut PageFlowScope and ViewScope And RequestScope
 
Mohamed Essmat
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohamed Essmat wrote:I have two Classes that extend from ExceptionHandlerWrapper & ExceptionHandlerFactory for catching any exceptions that occur in the view layer and navigate to error page and show this error. So I need store or save this exceptions somewhere but I can`t get this exception message. I tried to get this message in session but I need another way to save this message with out ViewScope and PageFlowScope and RequestScope.


public class CustomExceptionHandler extends ExceptionHandlerWrapper {
   private ExceptionHandler wrapped;


   public CustomExceptionHandler(ExceptionHandler wrapped) {
       this.wrapped = wrapped;
   }

   @Override
   public ExceptionHandler getWrapped() {
       return wrapped;
   }

   @Override
   public void handle() throws FacesException {
       Iterator iterator = getUnhandledExceptionQueuedEvents().iterator();

       while (iterator.hasNext()) {
           ExceptionQueuedEvent event = (ExceptionQueuedEvent) iterator.next();
           ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();


           Throwable throwable = context.getException();
           FacesContext fc = FacesContext.getCurrentInstance();


            Map<String, Object> sessionMap = fc.getExternalContext().getSessionMap();
            NavigationHandler navigator = fc.getApplication().getNavigationHandler();
           
           
           AdfFacesContext adfc = AdfFacesContext.getCurrentInstance();
           
        // Map<String,Object> valueViewScope =   adfc.getViewScope();
           

           try {
               //  Flash flash = fc.getExternalContext().getFlash();
               // Put the exception in the flash scope to be displayed in the error
               // page if necessary ...
               //  flash.put("errorDetails", throwable.getMessage());

              // ADFContext.getCurrent().getSessionScope().put("errorDetails", throwable.getMessage());

               sessionMap.put("errorDetails", throwable.getMessage());

               System.out.println("the error is put in the Session: " + throwable.getMessage());

               //NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler();
               navigator.handleNavigation(fc, null, "ErrorPage?faces-redirect=true");
               // navigationHandler.handleNavigation(fc, null, "ErrorPage?faces-redirect=true");

               fc.renderResponse();
           } finally {
             
           }
       }

       // Let the parent handle the rest
       getWrapped().handle();
   }
}



[code=java][public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory {

   private ExceptionHandlerFactory parent;
   
   public CustomExceptionHandlerFactory(ExceptionHandlerFactory parent) {
     this.parent = parent;
   }
   
   @Override
   public ExceptionHandler getExceptionHandler() {
     ExceptionHandler result = new CustomExceptionHandler(parent.getExceptionHandler());
     return result;
   }
}]


My Problems I need to save This Exception Message And Show This Message Any Time
I tried Message In SessionScope And Already occur This Error In Error Page
But I need Another Way To Save This Message
Please WithOut PageFlowScope and ViewScope And RequestScope

 
Bartender
Posts: 20098
101
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We appreciate your attempting to format your code for better reading, but you're doing it wrong. It's not [ code] ..... ], it's [ code] .... [/code] just like the angle-bracketed HTML and XML tags.

You cannot store stuff in the FacesContext for transmission between JSF pages. The FacesContext is completely destroyed between one JSF page request and another. It only exists while the current JSF page is being processed and it never gets created at all when your webapp uses non-JSF servlets and JSPs.

JSF isn't really designed for tossing Exceptions around. It expects that bean (and other JSF component) methods that expect Exceptions should catch those exceptions themselves and deal with them appropriately. In an action method, this would be to put a message in the FacesContext message collection and return null, which would re-display the failing page with a message on it. For a validator, you'd just consider it a validation problem and wrap the Exception in a ValidationException. And so forth.

Exceptions that are well and truly fatal can be wrapped and thrown as a FacesException, but that should never be done if you can help it. If you do have to do that, a general J2EE Exception page would be the place to catch and display it. Unchecked Exceptions will percolate up to the FacesServlet, but if they're allowed to do that without being intercepted and handled in application code, I consider that to be a bug. Exceptions really shouldn't ever be thrown out of application code back towards the FacesServlet.

Because JSF is designed as a user-friendly framework, however, Exceptions should generally be dealt with intelligently and not presented raw to the application user. Any dirty details should be written to the log, not the user.

And, despite your preferences/constraints, dealing with an exception by forwarding data in Flash scope is about the best way to deal with stuff like where your app discovers that your JDBC Connection is dead and you need to forward to a "panic page". Before that you'd have to have kept that information in session scope.
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!