I am working with an application involving Struts2 in web layer and Spring in 
business layer. I also have BusinessException class which will be used by all 
business services to create business related validation failures which must go 
up to web layer and should be shown to users as validation message(s). I can 
easily do this by writing in my Action class:

      ClientAction extends ActionSupport throws Exception{

         ....

        try{

            clientService.searchClient();

        }catch(InvalidClientSearchCriteriaException e){

              addActionMessage("Invlid Search Criteria");

        }

        ...

And similar code in every action class. However i dont want to pollute my 
action classes with try catch blocks. Instead it would be much better if i can 
write on try catch block at one place and catch all the exceptions there as 
BusinessExceptions and create messages /errors from embedded messages/errors in 
those exceptions. One approach i could think of was to use interceptor or 
preresult listener. But I cannot use interceptor like below which catches 
BusinessExceptions thrown from action classes...

    ExceptionInterceptor extends AbstractInterceptor(ActionInvocation 
ivocation,...){

          try{

                    invocation.invoke();

          }catch(Exception e){

              if(e instanceof BusinessException){

                   ActionSupport as = (ActionSupport)invocation.getAction();

                   String message = extractMessagefromException()//--custom 
method to extract message embedded in exception.

               as.addActionMessages(message);

      //-- above will not work result has already been rendered right? and 
hence it wouldn't matter if i add action messages now.

           }

         }

   }

Second approach of using pre-result listener is to add action messages just 
like above in pre-result listener's method, as result is yet to be rendered and 
I can safely change that. However I am not sure if pre-result listener will 
ever execute, if exception is thrown in action ? And even if it does, how i can 
get the exception object thrown by the action ?

Please let me know any other approach by which i don't have to clutter my 
classes with try-catch block




******************************************************
This message and any files or attachments sent with this message contain 
confidential information and is intended only for the individual named.  If you 
are not the named addressee, you should not disseminate, distribute, copy or 
use any part of this email.  If you have received this message in error, please 
delete it and all copies from your system and notify the sender immediately by 
return Email.

Email transmission cannot be guaranteed to be secure or error-free as 
information can be intercepted, corrupted, lost, destroyed, late, incomplete or 
may contain viruses.  The sender, therefore, does not accept liability for any 
errors or omissions in the contents of this message, which arise as a result of 
email transmission.
******************************************************

Reply via email to