You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Robert Parsons <ro...@optushome.com.au> on 2005/11/05 11:44:40 UTC

Cause exceptions from action methods to generate messages

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

Re: Cause exceptions from action methods to generate messages

Posted by Robert Parsons <ro...@optushome.com.au>.
Hi,

Good idea. I managed to catch the exceptions ok, however I still having 
a little trouble actually dispalying a page when it happens. Will write 
what i've got though.

-Robert

Martin Marinschek wrote:

>Robert,
>
>can you write up a WIKI page on how you did it on
>
>http://wiki.apache.org/myfaces
>
>I think this might be interesting for other users as well.
>
>regards,
>
>Martin
>
>On 11/10/05, Robert Parsons <ro...@optushome.com.au> wrote:
>  
>
>> Hi,
>>
>> I was actually checking to ensure that the ActionListener was not already
>>wrapped, but your method worked much better. Thankyou!
>>
>> -Robert
>>
>>
>>
>> Mathias Brökelmann wrote:
>> 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 <ti...@ktsplc.com>:
>>
>>
>> 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 <mk...@gmail.com>:
>>
>>
>>
>>
>> That's good to know. Does it work for action as well as
>>actionListener?
>>
>>On 11/6/05, Mathias Brökelmann <mb...@googlemail.com>
>>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 <mk...@gmail.com>:
>>
>>
>>
>>
>> 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 <ro...@optushome.com.au> 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: tim.davies@ktsplc.com
>>web: http://www.ktsplc.com
>>
>>
>>
>>
>>--
>>Mathias
>>
>>
>>
>>
>>
>>    
>>
>
>
>--
>
>http://www.irian.at
>
>Your JSF powerhouse -
>JSF Consulting, Development and
>Courses in English and German
>
>Professional Support for Apache MyFaces
>
>
>  
>


Re: Cause exceptions from action methods to generate messages

Posted by Martin Marinschek <ma...@gmail.com>.
Robert,

can you write up a WIKI page on how you did it on

http://wiki.apache.org/myfaces

I think this might be interesting for other users as well.

regards,

Martin

On 11/10/05, Robert Parsons <ro...@optushome.com.au> wrote:
>  Hi,
>
>  I was actually checking to ensure that the ActionListener was not already
> wrapped, but your method worked much better. Thankyou!
>
>  -Robert
>
>
>
>  Mathias Brökelmann wrote:
>  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 <ti...@ktsplc.com>:
>
>
>  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 <mk...@gmail.com>:
>
>
>
>
>  That's good to know. Does it work for action as well as
> actionListener?
>
> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com>
> 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 <mk...@gmail.com>:
>
>
>
>
>  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 <ro...@optushome.com.au> 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: tim.davies@ktsplc.com
> web: http://www.ktsplc.com
>
>
>
>
> --
> Mathias
>
>
>
>
>


--

http://www.irian.at

Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German

Professional Support for Apache MyFaces

Re: Cause exceptions from action methods to generate messages

Posted by Robert Parsons <ro...@optushome.com.au>.
Hi,

I was actually checking to ensure that the ActionListener was not 
already wrapped, but your method worked much better. Thankyou!

-Robert


Mathias Brökelmann wrote:

>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 <ti...@ktsplc.com>:
>  
>
>>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 <mk...@gmail.com>:
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>                      
>>>>>>>>>>>
>>>>>>>>>>>>That's good to know.   Does it work for action as well as
>>>>>>>>>>>>actionListener?
>>>>>>>>>>>>
>>>>>>>>>>>>On 11/6/05, Mathias Brökelmann <mb...@googlemail.com>
>>>>>>>>>>>>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 <mk...@gmail.com>:
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>                          
>>>>>>>>>>>>>
>>>>>>>>>>>>>>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 <ro...@optushome.com.au> 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: tim.davies@ktsplc.com
>>web: http://www.ktsplc.com
>>
>>
>>    
>>
>
>
>--
>Mathias
>
>
>  
>


Re: Cause exceptions from action methods to generate messages

Posted by Mathias Brökelmann <mb...@googlemail.com>.
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 <ti...@ktsplc.com>:
> 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 <mk...@gmail.com>:
> >>>>>>>>>
> >>>>>>>>>
> >>>>>>>>>> That's good to know.   Does it work for action as well as
> >>>>>>>>>> actionListener?
> >>>>>>>>>>
> >>>>>>>>>> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com>
> >>>>>>>>>> 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 <mk...@gmail.com>:
> >>>>>>>>>>>
> >>>>>>>>>>>
> >>>>>>>>>>>> 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 <ro...@optushome.com.au> 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: tim.davies@ktsplc.com
> web: http://www.ktsplc.com
>
>


--
Mathias

Re: Cause exceptions from action methods to generate messages

Posted by Tim Davies <ti...@ktsplc.com>.
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 <mk...@gmail.com>:
>>>>>>>>>  
>>>>>>>>>
>>>>>>>>>> That's good to know.   Does it work for action as well as 
>>>>>>>>>> actionListener?
>>>>>>>>>>
>>>>>>>>>> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 
>>>>>>>>>> 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 <mk...@gmail.com>:
>>>>>>>>>>>  
>>>>>>>>>>>
>>>>>>>>>>>> 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 <ro...@optushome.com.au> 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: tim.davies@ktsplc.com
web: http://www.ktsplc.com 


Re: Cause exceptions from action methods to generate messages

Posted by Robert Parsons <ro...@optushome.com.au>.
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 <mk...@gmail.com>:
>>>>>>>>  
>>>>>>>>
>>>>>>>>> That's good to know.   Does it work for action as well as 
>>>>>>>>> actionListener?
>>>>>>>>>
>>>>>>>>> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 
>>>>>>>>> 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 <mk...@gmail.com>:
>>>>>>>>>>  
>>>>>>>>>>
>>>>>>>>>>> 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 <ro...@optushome.com.au> 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
>>>>>>>>  
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>


Re: Cause exceptions from action methods to generate messages

Posted by Tim Davies <ti...@ktsplc.com>.
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 <mk...@gmail.com>:
>>>>>>>  
>>>>>>>
>>>>>>>> That's good to know.   Does it work for action as well as 
>>>>>>>> actionListener?
>>>>>>>>
>>>>>>>> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 
>>>>>>>> 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 <mk...@gmail.com>:
>>>>>>>>>  
>>>>>>>>>
>>>>>>>>>> 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 <ro...@optushome.com.au> 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: tim.davies@ktsplc.com
web: http://www.ktsplc.com 


Re: Cause exceptions from action methods to generate messages

Posted by Robert Parsons <ro...@optushome.com.au>.
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 <mk...@gmail.com>:
>>>>>>  
>>>>>>
>>>>>>> That's good to know.   Does it work for action as well as 
>>>>>>> actionListener?
>>>>>>>
>>>>>>> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 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 <mk...@gmail.com>:
>>>>>>>>  
>>>>>>>>
>>>>>>>>> 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 <ro...@optushome.com.au> 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
>>>>>>  
>>>>>>
>>>>>
>>>>
>>>
>>
>


Re: Cause exceptions from action methods to generate messages

Posted by Tim Davies <ti...@ktsplc.com>.
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 <mk...@gmail.com>:
>>>>>  
>>>>>
>>>>>> That's good to know.   Does it work for action as well as 
>>>>>> actionListener?
>>>>>>
>>>>>> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 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 <mk...@gmail.com>:
>>>>>>>  
>>>>>>>
>>>>>>>> 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 <ro...@optushome.com.au> 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: tim.davies@ktsplc.com
web: http://www.ktsplc.com 


Re: Cause exceptions from action methods to generate messages

Posted by Robert Parsons <ro...@optushome.com.au>.
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 <mk...@gmail.com>:
>>>>  
>>>>
>>>>> That's good to know.   Does it work for action as well as 
>>>>> actionListener?
>>>>>
>>>>> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 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 <mk...@gmail.com>:
>>>>>>   
>>>>>>
>>>>>>> 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 <ro...@optushome.com.au> 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
>>>>  
>>>>
>>>
>>
>


Re: Cause exceptions from action methods to generate messages

Posted by Tim Davies <ti...@ktsplc.com>.
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 <mk...@gmail.com>:
>>>  
>>>
>>>> That's good to know.   Does it work for action as well as 
>>>> actionListener?
>>>>
>>>> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 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 <mk...@gmail.com>:
>>>>>    
>>>>>
>>>>>> 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 <ro...@optushome.com.au> 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: tim.davies@ktsplc.com
web: http://www.ktsplc.com 


Re: Cause exceptions from action methods to generate messages

Posted by Robert Parsons <ro...@optushome.com.au>.
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 <mk...@gmail.com>:
>>  
>>
>>> That's good to know.   Does it work for action as well as 
>>> actionListener?
>>>
>>> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 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 <mk...@gmail.com>:
>>>>     
>>>>
>>>>> 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 <ro...@optushome.com.au> 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
>>  
>>
>


Re: Cause exceptions from action methods to generate messages

Posted by Tim Davies <ti...@ktsplc.com>.
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 <mk...@gmail.com>:
>  
>
>>That's good to know.   Does it work for action as well as actionListener?
>>
>>On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 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 <mk...@gmail.com>:
>>>      
>>>
>>>>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 <ro...@optushome.com.au> 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: tim.davies@ktsplc.com
web: http://www.ktsplc.com 


Re: Cause exceptions from action methods to generate messages

Posted by Mathias Brökelmann <mb...@googlemail.com>.
the actionlistener which is accessed/registered through Application is
responsible for handling actions.

2005/11/6, Mike Kienenberger <mk...@gmail.com>:
> That's good to know.   Does it work for action as well as actionListener?
>
> On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 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 <mk...@gmail.com>:
> > > 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 <ro...@optushome.com.au> 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

Re: Cause exceptions from action methods to generate messages

Posted by Mike Kienenberger <mk...@gmail.com>.
That's good to know.   Does it work for action as well as actionListener?

On 11/6/05, Mathias Brökelmann <mb...@googlemail.com> 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 <mk...@gmail.com>:
> > 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 <ro...@optushome.com.au> 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
>

Re: Cause exceptions from action methods to generate messages

Posted by Robert Parsons <ro...@optushome.com.au>.
Awesome! Thanks, I will try that right away.

Mathias Brökelmann 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 <mk...@gmail.com>:
>  
>
>>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 <ro...@optushome.com.au> 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
>
>
>  
>


Re: Cause exceptions from action methods to generate messages

Posted by Mathias Brökelmann <mb...@googlemail.com>.
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 <mk...@gmail.com>:
> 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 <ro...@optushome.com.au> 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

Re: Cause exceptions from action methods to generate messages

Posted by Mike Kienenberger <mk...@gmail.com>.
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 <ro...@optushome.com.au> 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
>
>
>
>
>

Re: Cause exceptions from action methods to generate messages

Posted by Robert Parsons <ro...@optushome.com.au>.
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
>>
>>    
>>
>
>  
>


Re: Cause exceptions from action methods to generate messages

Posted by Volker Weber <us...@weber-oldenburg.de>.
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
> 

-- 
Don't answer to From: address!
Mail to this account are droped if not recieved via mailinglist.
To contact me direct create the mail address by
concatenating my forename to my senders domain.