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();
      } 
    }
  }
}

78 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. Oracle Training in chennai
    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..

    ReplyDelete
  3. Oracle Training in chennai
    Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    ReplyDelete
  4. Informatica Training in chennai
    Thanks for sharing such a great information..Its really nice and informative..

    ReplyDelete
  5. Pega Training in Chennai
    This post is really nice and informative. The explanation given is really comprehensive and informative..

    ReplyDelete
  6. There are lots of information about latest technology and how to get trained in them, like Hadoop Training in Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies Hadoop Training in Chennai By the way you are running a great blog. Thanks for sharing this blogs..

    ReplyDelete
  7. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
    QTP Training in Chennai

    ReplyDelete
  8. SAS Training in Chennai
    Thanks for sharing this informative blog. I did SAS Certification in Greens Technology at Adyar. This is really useful for me to make a bright career..

    ReplyDelete
  9. 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..
    Greens Technologies In Chennai

    ReplyDelete
  10. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Greens Technologies In Chennai

    ReplyDelete
  11. Thanks for sharing amazing information about pega Gain the knowledge and hands-on experience you need to successfully design, build and deploy applications with pega. Pega Training in Chennai

    ReplyDelete
  12. Who wants to learn Informatica with real-time corporate professionals. We are providing practical oriented best Informatica training institute in Chennai. Informatica Training in chennai

    ReplyDelete
  13. QTP is a software Testing Tool which helps in Functional and Regression testing of an application. If you are interested in QTP training, our real time working. QTP Training in Chennai

    ReplyDelete
  14. Looking for real-time training institue.Get details now may if share this link visit Oracle Training in chennai

    ReplyDelete
  15. Hey, nice site you have here!We provide world-class Oracle certification and placement training course as i wondered Keep up the excellent work experience!Please visit Greens Technologies located at Chennai Adyar Oracle Training in chennai

    ReplyDelete
  16. Awesome blog if our training additional way as an SQL and PL/SQL trained as individual, you will be able to understand other applications more quickly and continue to build your skill set which will assist you in getting hi-tech industry jobs as possible in future courese of action..visit this blog Green Technologies In Chennai

    ReplyDelete
  17. Nice site....Please refer this site also Our vision succes!Training are focused on perfect improvement of technical skills for Freshers and working professional. Our Training classes are sure to help the trainee with COMPLETE PRACTICAL TRAINING and Realtime methodologies. Green Technologies In Chennai

    ReplyDelete
  18. 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
  19. 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
  20. It is really very helpful for us and I have gathered some important information from this blog.
    Oracle Training In Chennai

    ReplyDelete
  21. Oracle Training in Chennai is one of the best oracle training institute in Chennai which offers complete Oracle training in Chennai by well experienced Oracle Consultants having more than 12+ years of IT experience.

    ReplyDelete
  22. There are lots of information about latest technology and how to get trained in them, like Hadoop Training Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies(Hadoop Training in Chennai). By the way you are running a great blog. Thanks for sharing this.

    ReplyDelete
  23. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
    Informatica Training In Chennai

    ReplyDelete
  24. A Best Pega Training course that is exclusively designed with Basics through Advanced Pega Concepts.With our Pega Training in Chennai you’ll learn concepts in expert level with practical manner.We help the trainees with guidance for Pega System Architect Certification and also provide guidance to get placed in Pega jobs in the industry.

    ReplyDelete
  25. Our HP Quick Test Professional course includes basic to advanced level and our QTP course is designed to get the placement in good MNC companies in chennai as quickly as once you complete the QTP certification training course.

    ReplyDelete
  26. Thanks for sharing this nice useful informative post to our knowledge, Actually SAS used in many companies for their day to day business activities it has great scope in future.

    ReplyDelete
  27. Greens Technologies Training In Chennai Excellent information with unique content and it is very useful to know about the information based on blogs.

    ReplyDelete
  28. Greens Technology offer a wide range of training from ASP.NET , SharePoint, Cognos, OBIEE, Websphere, Oracle, DataStage, Datawarehousing, Tibco, SAS, Sap- all Modules, Database Administration, Java and Core Java, C#, VB.NET, SQL Server and Informatica, Bigdata, Unix Shell, Perl scripting, SalesForce , RedHat Linux and Many more.

    ReplyDelete

  29. have to learned to lot of information about java Gain the knowledge and hands-on experience you need to successfully design, build and deploy applications with java.
    Java Training in Chennai

    ReplyDelete

  30. have to learned to lot of information about java Gain the knowledge and hands-on experience you need to successfully design, build and deploy applications with java.
    Java Training in Chennai

    ReplyDelete
  31. Looking for real-time training institue.Get details now may if share this link visit
    Spring Training in chennai
    oraclechennai.in:

    ReplyDelete
  32. 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
  33. Awesome blog if our training additional way as an SQL and PL/SQL trained as individual, you will be able to understand other applications more quickly and continue to build your skill set which will assist you in getting hi-tech industry jobs as possible in future courese of action..visit this blog
    plsql in Chennai
    greenstechnologies.in:

    ReplyDelete
  34. 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


  35. hai you have to learned to lot of information about c# .net Gain the knowledge and hands-on experience you need to successfully design, build and deploy applications with c#.net.
    C-Net-training-in-chennai

    ReplyDelete


  36. hai If you are interested in asp.net training, our real time working.
    asp.net Training in Chennai.
    Asp-Net-training-in-chennai.html

    ReplyDelete

  37. Amazing blog if our training additional way as an silverlight training trained as individual, you will be able to understand other applications more quickly and continue to build your skill set which will assist you in getting hi-tech industry jobs as possible in future courese of action..visit this blog
    silverlight-training.html
    greenstechnologies.in:

    ReplyDelete

  38. awesome Job oriented sharepoint 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.
    if you have more details visit this blog.
    SharePoint-training-in-chennai.html

    ReplyDelete


  39. if share valuable information about cloud computing training courses, certification, online resources, and private training for Developers, Administrators, and Data Analysts may visit
    Cloud-Computing-course-content.html

    ReplyDelete
  40. Latest Govt Bank Railway Jobs 2016

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

    ReplyDelete

  41. 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
  42. Latest Govt Bank Jobs Notification 2016

    This is really informative post. very unique and informative thanks for update........

    ReplyDelete
  43. 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
  44. 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
  45. 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
  46. 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
  47. Hai you have to learned to lot of information about selenium Gain the knowledge and hands-on experience you need to successfully design,so you have more details visit this site..
    selenium Training in Chennai

    ReplyDelete
  48. 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
  49. hai you have to learned to lot of information about oracle rac training Gain the knowledge and hands-on experience you need to successfully design, you have more information visit this site...
    oracle training in chennai

    ReplyDelete
  50. 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
  51. 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
  52. 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
  53. 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
  54. Thanks for sharing informative article on java. It helped me to understand the career prospects in java
    java training in Chennai

    ReplyDelete
  55. Nice article. I like your blog. This is very useful post.

    IELTS classes in Kuwait

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

    web design training institute in Chennai

    ReplyDelete
  57. 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
  58. 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
  59. Great information.But we have theory format for this concept.I appreciate this information.It is too nice.
    Java Training in Chennai

    ReplyDelete
  60. 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
  61. 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
  62. 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
  63. 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



  64. 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

  65. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.


    Java training in Adyar

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

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

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

    php training in chennai

    ReplyDelete
  69. 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
  70. very helpful for my site. I always follow your tips....
    ssrs training in chennai

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

    ReplyDelete
  72. 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
  73. The share you really give us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!Python Online Training | Learn Python Online

    ReplyDelete
  74. Dear, I like all your post. Everything looks so sweet, I admire this kind of life and the best wishes for you. Hope that we can communicate with each other. By the way, anybody want to boost app ranking ? This one is useful.

    ReplyDelete