Friday, May 20, 2011

Struts 2 Interceptors - before, between and after

Struts 2 Interceptors is one of the greatest features of Struts 2 to handle regular routing logic and special logic at special occassion on demand the AOP way.
In most cases, you want to intercept the running application before action invocation, between action invocation and result generation and after view rendered.

I found a great tutorial on how to handle the above 3 senario at Benjamin J. McCann's blog post.

Below is quick summary:

To create a Struts 2 interceptor, you need to implement Interceptor interface or extend one of the build-in interceptor class. E.g. AbstractInterceptor

Intercept Before Action

public class MyInterceptor extends AbstractInterceptor {
  private static final long serialVersionUID = 5065298925572763728L;
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
      // do something before invoke
      try{
        if(o instanceof MyAction){
          ((MyAction) o).someLogicBeforeAction();
        }
      }catch(Exception e){
        return "error";
      } 

      // invocation continue    
      return invocation.invoke();
    }
  }
}

Intercept Between Action and Result

(after action is executed but before result is rendered)

Register a PreResultListener to the invocation instance. Sample code as below:
public class MyInterceptor extends AbstractInterceptor {
  private static final long serialVersionUID = 5065298925572763728L;
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
      // Register a PreResultListener and implement the beforeReslut method
      invocation.addPreResultListener(new PreResultListener() { 
        @Override
        public void beforeResult(ActionInvocation invocation, String resultCode) {
          Object o = invocation.getAction();
          try{
            if(o instanceof MyAction){
              ((MyAction) o).someLogicAfterActionBeforeView();
            }
          }catch(Exception e){
            invocation.setResultCode("error");
          }
        }
      });
      
      // Invocation Continue
      return invocation.invoke();
    }
  }
}

Intercept After View Rendered


public class MyInterceptor extends AbstractInterceptor {
  private static final long serialVersionUID = 5065298925572763728L;
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
      // do something before invoke
      try{
        // invocation continue    
        return invocation.invoke();
      }catch(Exception e){
        // You cannot change the result code here though, such as:
        // return "errorr"; 
        // This line will not work because view is already generated  
        // You can do something like database cleanup or logging at this point
        doSomeLogicAfterView();
      } 
    }
  }
}

124 comments :

  1. Thanks for the wonderful information.Its really very helpful..Coding makes us better understanding for the concept.Its really good one.

    Java Training in Chennai

    ReplyDelete
  2. if share valuable information about hadoop training courses, certification, online resources, and private training for Developers, Administrators, and Data Analysts may visit Hadoop Training in Chennai

    ReplyDelete
  3. let's Jump Start Your Career & Get Ahead. Choose sas training method that works for you. This course is designed for professionals looking to move to a role as a business analyst, and students looking to pursue business analytics as a career. SAS Training in Chennai

    ReplyDelete
  4. Hybernet is a framework Tool. If you are interested in hybernet training, our real time working.
    Hibernate Training in Chennai,hibernate training in Chennai

    ReplyDelete
  5. Job oriented form_reports training in Chennai is offered by our institue is mainly focused on real time and industry oriented. We provide training from beginner’s level to advanced level techniques thought by our experts.
    forms-reports Training in Chennai

    ReplyDelete
  6. Latest Govt Bank Railway Jobs 2016

    Thanks for providing valuable information in this article by author.........................

    ReplyDelete

  7. Hai if our training additional way as (IT) trained as individual,you will be able to understand other applications more quickly and continue to build your skll set
    which will assist you in getting hi-tech industry jobs as possible in future courese of action..
    visit this blog webMethods-training in chennai



    ReplyDelete
  8. great article!!!!!This is very importent information for us.I like all content and information.I have read it.You know more about this please visit again.
    QTP Training in Chennai

    ReplyDelete
  9. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    Informatica Training in Chennai

    ReplyDelete
  10. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    informatica training in chennai

    ReplyDelete
  11. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    informatica training in chennai

    ReplyDelete
  12. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
    QTP Training in Chennai

    ReplyDelete
  13. This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.. Rac Training In Chennai

    ReplyDelete
  14. GSSSB 2480 Revenue Talati Bharti Recruitment 2016

    Really your blog is very interesting it contains great and unique information It's just amazing Thanks very much. .............

    ReplyDelete
  15. Haryana HSSC Steno Typist Recruitment 2016

    I am actually grateful to the holder of this web page who has shared this wonderful piece of writing at here..............................

    ReplyDelete
  16. great article!!!!!This is very importent information for us.I like all content and information.I have read it.You know more about this please visit again.
    Informatica Training In Chennai
    Hadoop Training In Chennai
    Oracle Training In Chennai
    Pega Training In Chennai

    ReplyDelete
  17. Thanks for sharing informative article on java. It helped me to understand the career prospects in java
    java training in Chennai

    ReplyDelete
  18. Wonderful article. I learned more new things. Thanks for sharing.

    web design training institute in Chennai

    ReplyDelete
  19. This blog is informative. It gives detailed explanation about the oracle concept. It will be helpful for the freshers to understand the concept.Thanks for providing this information.
    Manual Testing Training in Chennai

    ReplyDelete
  20. You have clearly explained about Struts 2 Interceptors - before, between and after.Its very useful and informative.Thanks for your blog.Its very helpful.
    Android Training in Chennai

    ReplyDelete
  21. Great information.But we have theory format for this concept.I appreciate this information.It is too nice.
    Java Training in Chennai

    ReplyDelete
  22. I came to know the concept of java and its usage in the program it is really useful and innovative thanks for sharing.


    dot net training in chennai

    ReplyDelete
  23. Nice...You have clearly explained about Struts 2 Interceptors - before, between and after.Its very useful and informative.Thanks for your blog.Its very helpful.
    Linux training in chennai

    ReplyDelete
  24. very informative blog. clearly explained the concept of struct 2 intercepts. thanks for sharing this information. please update this type of information
    ssas training in chennai

    ReplyDelete
  25. Nice blog.It is explained the intercept and its methods and is easy to understanding.In that coding is awesome.thanks for this blog.

    Sharepoint admin training in chennai

    ReplyDelete



  26. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
    Helpful as always. Every post you write produce a massive value to your readers that is the only reason it is so popular and has great authority.

    Peridot Systems Chennai Reviews

    ReplyDelete
  27. Thanku for sharing this valuable information on struts and the style of writing is very nice ..
    SAS online training in hyderabad

    ReplyDelete
  28. Hadoop training in hyderabad.All the basic and get the full knowledge of hadoop.
    hadoop training in hyderabad

    ReplyDelete
  29. Good concept you have discussed. Thanks for sharing.

    php training in chennai

    ReplyDelete
  30. struts interceptors before and between nice posts..

    Hadoop training in hyderabad.All the basic and get the full knowledge of hadoop.
    hadoop training in hyderabad


    ReplyDelete
  31. very helpful for my site. I always follow your tips....
    ssrs training in chennai

    ReplyDelete
  32. very helpful for my site. I always follow your tips.......
    weblogic training in chennai

    ReplyDelete
  33. Excellent post. This is first time i have visit your post. Its really useful to me.
    thank you for sharing with us. Cloud Computing Training in Chennai | Salesforce Training in Chennai

    ReplyDelete

  34. It was so good to read and useful to improve my knowledge as updated one.Thanks to Sharing


    Mainframe Training In Chennai | Hadoop Training In Chennai | ETL Testing Training In Chennai

    ReplyDelete
  35. Thanks for providing information.It was very nice article and it is very useful for me.

    Final Year Bsc Projects Chennai | Final Year Msc Projects Chennai.

    ReplyDelete
  36. Your blog is nice. I believe this will surely help the readers who are really in need of this vital piece of information. Thanks for sharing and kindly keep updating.

    Selenium Training in Chennai
    Best selenium training in chennai
    ios developer course in chennai
    ios classes in chennai
    JAVA Training Chennai
    JAVA J2EE Training in Chennai

    ReplyDelete
  37. I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating us.
    Hadoop Training in Chennai
    Cloud computing Training
    Cloud computing Training Chennai
    Cloud computing Training centers in Chennai
    Hadoop Course in Chennai
    best big data training in chennai

    ReplyDelete
  38. Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in

    Data Science training in Chennai | Data science training in bangalore

    Data science training in pune | Data science online training

    Data Science Interview questions and answers


    ReplyDelete
  39. This looks absolutely perfect. All these tiny details are made with lot of background knowledge. I like it a lot. 
    online Python training | python training in chennai

    ReplyDelete
  40. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers
    AWS Training in Bangalore | Amazon Web Services Training in Bangalore
    AWS Training in Pune | Best Amazon Web Services Training in Pune
    Amazon Web Services Training in Pune | Best AWS Training in Pune
    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  41. Hi there I am so thrilled I found your website, I really found you by mistake, while I was browsing on Yahoo for something else
    iosh course in chennai

    ReplyDelete
  42. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Python Online training
    python Training in Chennai
    Python training in Bangalore

    ReplyDelete
  43. This comment has been removed by the author.

    ReplyDelete
  44. Excellent article, nice info, must say thanks for the information you added to this post. I appreciate your post and looking forward for more.


    Data Science Courses in Bangalore

    ReplyDelete
  45. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    date analytics certification training courses
    data science courses training
    data analytics certification courses in Bangalore
    ExcelR Data science courses in Bangalore

    ReplyDelete

  46. I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting!!


    Best Devops Training Institute

    ReplyDelete
  47. Thanks for the best blog. it was very useful for me.keep sharing such ideas in the future as well.
    data science course malaysia

    ReplyDelete
  48. I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.

    Data Science Course

    ReplyDelete
  49. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  50. QuickBooks Payroll has emerged the best accounting software that has had changed the meaning of payroll. QuickBooks Payroll Support Phone Number contact number is the team that offer you Quickbooks

    ReplyDelete

  51. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
    www.technewworld.in
    How to Start A blog 2019
    Eid AL ADHA

    ReplyDelete
  52. This comment has been removed by the author.

    ReplyDelete
  53. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Data science training in Electronic City

    ReplyDelete
  54. This is also a very good post which I really enjoyed reading. It is not every day that I have the possibility to see something like this,
    Data science Courses banglore

    ReplyDelete
  55. Because you know how important is the QuickBooks payroll to boost your organization, you need the very best and trustworthy QuickBooks Payroll assist to avoid facing technical glitches and learn many more new stuff like square QuickBooks integration and QuickBooks online accountant to really make the best use of this software using different tools. Just dial QuickBooks Payroll Support Phone Number to operate this unique accounting software without any issue.

    ReplyDelete
  56. business enterprise and then make a lot of errors. You yourself don’t find out how much errors you are QuickBook Support When this occurs it is actually natural to own a loss in operation. But, I will be at your side.

    ReplyDelete
  57. QuickBooks Enterprise offers useful features making it more reliable in addition to efficient. You can run your organization smoothly with great ease and flexibility by using this specialized accounting software. This is one way it can save you your valuable time & money using intuit enterprise support. If you're a QuickBooks user and facing any issue regarding this software then turn to the QuickBooks Enterprise Tehnical Support Number.

    ReplyDelete
  58. You can resolve this error by using the below troubleshooting steps you are able to simply contact our QuickBooks Support Phone Number available at.You should run QuickBooks print and pdf repair tool to ascertain and fix the errors in printer settings prior to starting the troubleshooting.

    ReplyDelete
  59. To have a level of expertise in payroll management through QuickBooks, all of us attempts to strengthen your understanding of the payroll updates occurring in QuickBooks Support, desktop, pro, premier 2019 versions. Intuit Payroll Online exports all of the transactions to QuickBooks Desktop App along with QuickBooks Payroll Support USA as standalone software.

    ReplyDelete

  60. QuickBooks Enterprise Support Number team helps you deal with all the issues of QB Enterprise. Now let’s have a look on the industry versions that it has provided us with. There are six types of industry versions that QB Enterprise offers.

    ReplyDelete
  61. QuickBooks 24/7 Payroll Support Phone Number USA
    helps one to resolve all your valuable technical and functional problems whilst looking after this well known extensive, premium and end-to-end business accounting and payroll management software. Our experts team at QuickBooks payroll support number is going to make you understand its advanced functions and assists someone to lift up your business growth.

    ReplyDelete
  62. You can use QuickBooks to come up with any selection of reports you wish, keeping entries for several sales, banking transactions and plenty of additional. QuickBooks Customer Support Number provides a myriad of options and support services for an equivalent.

    ReplyDelete

  63. QuickBooks Tech Support Number support also extends to those errors when QB Premier is infected by a virus or a spyware. We also handle any type of technical & functional issue faced during installation of drivers for QuickBooks Premier Version.

    ReplyDelete
  64. QuickBooks Support toll free Number advisors are certified Pro-advisors’ and it has forte in furnishing any kind of technical issues for QuickBooks. These are typically expert and certified technicians of their domains like QuickBooks accounting,QuickBooks Support Phone Number, Point of Sales, QuickBooks Merchant Services and Inventory issues to provide 24/7 service to your esteemed customers.

    ReplyDelete
  65. Therefore, QuickBooks is present for users around the world as the best tool to offer creative and innovative features for business account management to small and medium-sized business organizations. If you’re encountering any type of QuickBooks’ related problem, you will get all of that problems solved just by using the QuickBooks Help & Support.

    ReplyDelete

  66. Thanks for writing this great article. I’ve been using some of these techniques on by blog. But I didn’t know the phrase “Social Proof”. Thanks for sharing.
    One data science
    One Machine Learning

    ReplyDelete
  67. Although, QuickBooks Tech Support Number is a robust accounting platform that throws less errors as compared to others. Sometimes you might face technical errors when you look at the software while installation or upgrade related process. To obtain the errors resolved by professionals, contact us at QuickBooks support phone number.

    ReplyDelete
  68. QuickBooks has been recognised around the globe as the most effective and useful accounting software. Quickbooks Technical support customer service executives that really work with you on QuickBooks Tech Support are responsible to manage every Quickbook technical issue that creates in QuickBooks software.

    ReplyDelete
  69. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Data science training in Electronic City

    ReplyDelete
  70. There is occasions as soon as you might face some kind of delay in reaching us, let’s say in the period of filing taxes while there is lots of hush-hush then. We assure you that people will revert for your requirements in a shorter time and work out us accessible to you at QuickBooks Help & Support.

    ReplyDelete
  71. You may want to avoid the QuickBooks Support Phone Number others users from viewing your financial transactions and records. For the, you need to set-up the business preferences at the time of set-up.

    ReplyDelete
  72. QuickBooks Support could be contacted to master the ways to generate an automatic backup to save your entire employee-related data from getting bugged or lost at any circumstances.

    ReplyDelete

  73. You may encounter QuickBooks Error Code 6000-301 when wanting to access/troubleshoot/open the organization file in your QuickBooks. Your workflow gets hindered with a mistake message that says– “QuickBooks Desktop tried to gain access to company file. Please try again.”

    ReplyDelete
  74. QuickBooks Customer Support Number might get some technical glitch. You should have errors also. Where would you turn? Take help from us straight away. We are going to provide full support to you. It is possible to deal with most of the errors. We have to just coach you on something.

    ReplyDelete
  75. Nice Blog It’s a really informative for all. Quickbooks accounting software is helpful for maintain client payroll taxes accounting problems. We are providing technical support in Quickbooks Support Phone Number 1800 . Our technical support team is 24*7 available so if you need any help. Please call us our Toll-free Number + 1-800-986-4607.

    ReplyDelete
  76. Get prominent options for QuickBooks towards you right away! With no doubts, QuickBooks has revolutionized the process of doing accounting that is the core strength for small in addition to large-sized businesses. QuickBooks Support Number is assisted by our customer support representatives who answer your call instantly and resolve all your valuable issues at that moment. It really is a backing portal that authenticates the users of QuickBooks to perform its services in a user-friendly manner.

    ReplyDelete
  77. This is such a great resource that you are providing and you give it away for free.
    data science course

    ReplyDelete
  78. QuickBooks software program is developed in such a manner that it will supply you with the best account management reference to this era. However, you could face the issue with your QuickBooks software and begin trying to find the clear answer. You should not worries, if you should be facing trouble using your software you'll be just a call away to your solution. Reach us at QuickBooks Support Phone Number at and experience our efficient tech support team of many your software related issues. If you're aa QuickBooks enterprise user, it is possible to reach us out immediately at our 247 Tech Support Number. QuickBooks technical help is present at our QuickBooks tech support number dial this and gets your solution from our technical experts.

    ReplyDelete
  79. The team of certified and trained professionals will usually work with you with amazing and quality services round the clock merely to assist you in raising the development of the business and organizations to the next level. To troubleshoot errors and issues, avail services through the QuickBooks Payroll Tech Support Number.

    ReplyDelete

  80. Our company is providing at any hour support services for many possible QuickBooks Errors from our QuickBooks Accounting Support Services. Our Tech Support Executives can create a provisional reference to your QuickBooks Enterprise Support Phone Number and Workstation Servers to run diagnoses and resolves the issues or errors.

    ReplyDelete
  81. The QuickBooks Payroll Tech Support Phone Number team at our end is quite helpful in terms of handling the issues that pop up in QuickBooks Payroll or any of its versions. With regards to problems, every one us understand that these are typically inevitable.

    ReplyDelete
  82. It offers you the facility of automated data backup and recovery. These features are really best for the introduction of an individual's business. QuickBooks Tech Support Phone Number Premier will likely to be two versions Premier and Premier Plus. Both in the versions you'll want to pick the industry type in the period of installation.

    ReplyDelete
  83. We have a Support Service team for Quickbooks tech. Need instant solution of your Quickbooks Accounting related. Instantly Dial quickbooks tech support phone number for USA user only. Call us on +1-800-901-6679.

    ReplyDelete
  84. Basically, this has automated several tasks that have been being done manually for a long time. There are lots of versions of Intuit QuickBooks Support Number and each one has a unique features.

    ReplyDelete
  85. We have a Support Service team for Quickbooks tech. Need instant solution of your Quickbooks Accounting related. If you need quickbooks pro support phone number for USA user only. Call us on +1-800-901-6679.

    ReplyDelete
  86. Good post. Instead of writing theory, sharing code can easily understood and helpful to us. Click here to know more about agile - SAFe Agilist Training

    ReplyDelete
  87. Really useful information. Thank you so much for sharing. It will help everyone. Keep Post.

    Start your journey with RPA Course and get hands-on Experience with 100% Placement assistance from Expert Trainers with 8+ Years of experience @eTechno Soft Solutions Located in BTM Layout Bangalore.

    ReplyDelete
  88. And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
    Digital Marketing Training in Chennai

    ReplyDelete
  89. Nice and very informative blog click here for Quickbooks Payroll Support Phone Number and for more detail dial on 844-908-0801

    ReplyDelete
  90. Well informative blog click here for
    QuickBooks Proadvisor Support Phone Number 844-908-0801 for more detail dial on our support phone number

    ReplyDelete
  91. Office 365 has number of technical prerequisites that you need to be aware of before you migrate. The main one that catches people out is that Windows XP and Office 2003 are not supported. office 365

    ReplyDelete

  92. Very informative and well explained blog click here for
    QuickBooks customer service phone number for more detail dial on our QuickBooks Support phone number 844-908-0801

    ReplyDelete
  93. After I initially commented I appear to have clicked on the -Notify me when new comments are added- checkbox and now each time a comment is added I recieve 4 emails with the same comment. Perhaps there is a means you are able to remove me dev
    from that service? Thanks!

    ReplyDelete
  94. It is an informative blog. I would like to know more information. Anyway thanks a lot for sharing this post. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervour like mine to grasp great deal more around this condition.
    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  95. Time and budget are vital constraints for any new product or service in a company, and more so if it is a startup. One can create a product that differentiates itself from the rest in any language. data science course in india

    ReplyDelete
  96. Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notice that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing.
    data science classes in hyderabad

    ReplyDelete