Sorry I forgot that a FacesContext instance is only available if a jsf request is actually processed.
But you have still access in the context listener to the configured jsf application instance. I really suggest you to use something which allows you to initialize a custom action listener only one time. If you do it in a session listener you will end up with a new actionlistener impl everytime a session starts. To get an instance of the JSF Application class use the FactoryFinder class: ApplicationFactory appFactory = (ApplicationFactory)FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY) Application app = appFactory.getApplication(); this is the way how we do it if we must wrap some core functionality in jsf and still being implementation independent. 2005/11/10, Tim Davies <[EMAIL PROTECTED]>: > Im using the latest release of MyFaces. > > Im not really sure what to advise. I had it working okay with this > approach but I dont have the code anymore unfortuately so cant check back. > > Not much more I can suggest really, sorry. > > Robert Parsons wrote: > > > Hi, > > > > I tried doing what you suggested and obtaining the existing > > ActionListener on construction of mine, but: > > > > FacesContext.getCurrentInstance(); > > > > is returning null, so I cant access access the faces application. Are > > you using MyFaces or the RI? > > > > Thanks, > > -Robert. > > > > Tim Davies wrote: > > > >> Seems a reasonable question but this does not seem to be the case. If > >> you initialise your reference to the original ApplicationListener in > >> your constructor then I think you should be fine. > >> > >> I would give it a go and see. This was working fine for me but I > >> admit I didnt look too deeply under the covers. > >> > >> > >> Robert Parsons wrote: > >> > >>> Yes, > >>> But if you do it in the faces-config how do you wrap the default > >>> action listener? Wouldnt your action listener replace the default > >>> one before you get a chance to wrap it? > >>> > >>> Thanks, > >>> -Robert > >>> > >>> > >>> Tim Davies wrote: > >>> > >>>> Sorry that was a bit vague. > >>>> > >>>> Mathias suggested initialising the custom ApplicationListener in a > >>>> ServletContextListener object. However if this is registered as a > >>>> listener in web.xml then its contextInitialized() method will be > >>>> called before the FacesContext is initilised and so it will not work. > >>>> > >>>> I think that faces.config is probably the best place to set this as > >>>> then you know that it will always be set when faces is used and do > >>>> not have to worry about maintaining it in your own code somewhere. > >>>> > >>>> > >>>> Robert Parsons wrote: > >>>> > >>>>> Thanks for the quick reply. > >>>>> > >>>>> I thought you said that at context initialisation the FacesContext > >>>>> was not initialised? I have not tried it myself however. I decided > >>>>> to wrap the handler when the first session is created instead. Is > >>>>> there a better place to create the wrapper? > >>>>> > >>>>> Thanks, > >>>>> -Robert. > >>>>> > >>>>> Tim Davies wrote: > >>>>> > >>>>>> Mathias wrote a version that will do this earlier in this thread. > >>>>>> > >>>>>> Basically you need to get and store a reference to the original > >>>>>> actionlistener in your actionlistener. Then in your processaction > >>>>>> method you perform your work or set up your try catch block and > >>>>>> then call processAction on the original actionlistener. > >>>>>> > >>>>>> > >>>>>> > >>>>>> Robert Parsons wrote: > >>>>>> > >>>>>>> I'm a little confused. If I register it in the faces-config.xml, > >>>>>>> how do I then pass on the action to the existing action > >>>>>>> listener? (the one that actually does something) Or is that not > >>>>>>> how it works. I can only find information on action listeners > >>>>>>> for components, not global ones like this. > >>>>>>> > >>>>>>> Tim Davies wrote: > >>>>>>> > >>>>>>>> Just as an update to this, if you want to register your own > >>>>>>>> ActionListener then you can do so by adding the following > >>>>>>>> element to your faces-config.xml file. > >>>>>>>> > >>>>>>>> <application> > >>>>>>>> <action-listener> > >>>>>>>> com.example.MyActionListener > >>>>>>>> </action-listener> > >>>>>>>> </application> > >>>>>>>> > >>>>>>>> I tried it in the context listener but the FacesContext will > >>>>>>>> not have been initialised at the point when it is run. > >>>>>>>> > >>>>>>>> Thanks for the tips on this though. Has proved interesting. > >>>>>>>> > >>>>>>>> Tim > >>>>>>>> > >>>>>>>> > >>>>>>>> > >>>>>>>> Mathias Brökelmann wrote: > >>>>>>>> > >>>>>>>>> the actionlistener which is accessed/registered through > >>>>>>>>> Application is > >>>>>>>>> responsible for handling actions. > >>>>>>>>> > >>>>>>>>> 2005/11/6, Mike Kienenberger <[EMAIL PROTECTED]>: > >>>>>>>>> > >>>>>>>>> > >>>>>>>>>> That's good to know. Does it work for action as well as > >>>>>>>>>> actionListener? > >>>>>>>>>> > >>>>>>>>>> On 11/6/05, Mathias Brökelmann <[EMAIL PROTECTED]> > >>>>>>>>>> wrote: > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>>> It´s quite easy to change the handling for invoking action > >>>>>>>>>>> methods. > >>>>>>>>>>> Simply wrap existing ActionListener Implementation of > >>>>>>>>>>> processAction > >>>>>>>>>>> with an try and catch block: > >>>>>>>>>>> > >>>>>>>>>>> FacesContext context = FacesContext.getInstance(); > >>>>>>>>>>> final ActionListener actionListener = > >>>>>>>>>>> context.getApplication().getActionListener(); > >>>>>>>>>>> ActionListener wrappedActionListener = new ActionListener() > >>>>>>>>>>> { > >>>>>>>>>>> public void processAction(ActionEvent actionEvent) throws > >>>>>>>>>>> AbortProcessingException > >>>>>>>>>>> { > >>>>>>>>>>> try > >>>>>>>>>>> { > >>>>>>>>>>> actionListener.processAction(actionEvent); > >>>>>>>>>>> } > >>>>>>>>>>> catch(Throwable t) > >>>>>>>>>>> { > >>>>>>>>>>> // do generic action exception handling here > >>>>>>>>>>> } > >>>>>>>>>>> } > >>>>>>>>>>> } > >>>>>>>>>>> context.getApplication().setActionListener(wrappedActionListener); > >>>>>>>>>>> > >>>>>>>>>>> > >>>>>>>>>>> You can implement it in a > >>>>>>>>>>> javax.servlet.ServletContextListener.contextInitialized() > >>>>>>>>>>> method. and > >>>>>>>>>>> register the listener in your web.xml file. > >>>>>>>>>>> > >>>>>>>>>>> 2005/11/5, Mike Kienenberger <[EMAIL PROTECTED]>: > >>>>>>>>>>> > >>>>>>>>>>> > >>>>>>>>>>>> It doesn't appear that there's an easy way to do this. > >>>>>>>>>>>> The events are triggered from UIComponentBase.broadcast() > >>>>>>>>>>>> which calls > >>>>>>>>>>>> each event.processListener() method which calls > >>>>>>>>>>>> ActionListener.processAction() which calls > >>>>>>>>>>>> methodBinding.invoke(). > >>>>>>>>>>>> > >>>>>>>>>>>> Ideally, you'd want to specify a custom > >>>>>>>>>>>> methodBinding.invoke() that > >>>>>>>>>>>> wrapped the error for you. Facelets does things > >>>>>>>>>>>> differently -- maybe > >>>>>>>>>>>> there's a way to create alternate MethodBinding rules for > >>>>>>>>>>>> ActionSources which create your subclass of MethodBinding > >>>>>>>>>>>> rather than > >>>>>>>>>>>> the default MethodBinding instances. You could try asking > >>>>>>>>>>>> about that > >>>>>>>>>>>> on the facelets mailing list. > >>>>>>>>>>>> > >>>>>>>>>>>> Another possiblity is to use aspect-oriented-programming > >>>>>>>>>>>> (AOP) to > >>>>>>>>>>>> intercept methodBinding.invoke(). However, I don't use > >>>>>>>>>>>> AOP, so I > >>>>>>>>>>>> can't tell you anything beyond that it appears to do what > >>>>>>>>>>>> you need. > >>>>>>>>>>>> > >>>>>>>>>>>> On 11/5/05, Robert Parsons <[EMAIL PROTECTED]> wrote: > >>>>>>>>>>>> > >>>>>>>>>>>> > >>>>>>>>>>>>> Hi, > >>>>>>>>>>>>> > >>>>>>>>>>>>> thanks for the reply. the wording of my question was a > >>>>>>>>>>>>> little bit off. I > >>>>>>>>>>>>> was looking for an automatic way for Exceptions that were > >>>>>>>>>>>>> thrown in any > >>>>>>>>>>>>> action method to automatically be added as a message > >>>>>>>>>>>>> (instead of the > >>>>>>>>>>>>> horrible error screens i get from facelets at the moment). > >>>>>>>>>>>>> > >>>>>>>>>>>>> The only other option other than an automatic method would > >>>>>>>>>>>>> be to wrap a > >>>>>>>>>>>>> try-catch around all the code of every action I have and > >>>>>>>>>>>>> generate a message > >>>>>>>>>>>>> when an exception is caught. Sounds like that might have > >>>>>>>>>>>>> to be the way I do > >>>>>>>>>>>>> it. > >>>>>>>>>>>>> > >>>>>>>>>>>>> Thanks anyway, > >>>>>>>>>>>>> -Robert. > >>>>>>>>>>>>> > >>>>>>>>>>>>> > >>>>>>>>>>>>> Volker Weber wrote: > >>>>>>>>>>>>> Hi, > >>>>>>>>>>>>> > >>>>>>>>>>>>> you can add a Message to FacesContect. > >>>>>>>>>>>>> > >>>>>>>>>>>>> See: > >>>>>>>>>>>>> http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/javax/faces/context/FacesContext.html#addMessage(java.lang.String,%20javax.faces.application.FacesMessage) > >>>>>>>>>>>>> > >>>>>>>>>>>>> > >>>>>>>>>>>>> regards > >>>>>>>>>>>>> Volker > >>>>>>>>>>>>> > >>>>>>>>>>>>> Robert Parsons wrote: > >>>>>>>>>>>>> > >>>>>>>>>>>>> > >>>>>>>>>>>>> Hi, > >>>>>>>>>>>>> > >>>>>>>>>>>>> Is there an easy way to make exceptions thrown by action > >>>>>>>>>>>>> methods (on > >>>>>>>>>>>>> backing beans) to generate messages? Or would this only be > >>>>>>>>>>>>> possible by > >>>>>>>>>>>>> modifying the MyFaces code. > >>>>>>>>>>>>> > >>>>>>>>>>>>> Thanks heaps, > >>>>>>>>>>>>> -Robert > >>>>>>>>>>>>> > >>>>>>>>>>>>> > >>>>>>>>>>>>> > >>>>>>>>>>>>> > >>>>>>>>>>>>> > >>>>>>>>>>>>> > >>>>>>>>>>>> > >>>>>>>>>>>> > >>>>>>>>>>>> > >>>>>>>>>>>> > >>>>>>>>>>>> > >>>>>>>>>>>> > >>>>>>>>>>>> > >>>>>>>>>>>> > >>>>>>>>>>> -- > >>>>>>>>>>> Mathias > >>>>>>>>>>> > >>>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>> > >>>>>>>>> > >>>>>>>>> -- > >>>>>>>>> Mathias > >>>>>>>>> > >>>>>>>>> > >>>>>>>> > >>>>>>> > >>>>>> > >>>>> > >>>> > >>> > >> > > > > -- > Tim Davies > Analyst Developer > > KTS PLC: Service you can bank on > 8th Floor, Finsbury Tower, > 103-105 Bunhill Row, > London EC1Y 8TY > tel: +44 (0)20 7256 2300 > fax: +44 (0)20 7256 2301 > > email: [EMAIL PROTECTED] > web: http://www.ktsplc.com > > -- Mathias