Interceptor
Interceptor are like filters which get processed just before and after action called.
debugging, logging, i18n, params, validation are the default interceptors provided by Struts.
Note: Before Interceptor code practice, please create Struts project using this Link.
Follow below steps once project setup is done and started working in local Eclipse.
Creation an interceptor require implementation of Interceptor interface from com.opensymphony.xwork2.interceptor.Interceptor package.
- Create an interceptor name as UserInterceptor.java class as shown below.
init() → This method will get all only once at the time of server starts.
destroy() → This method will get call once this interceptor or application is undeployed
intercept(ActionInvocation invocation) → This will get call every time interceptor gets call.
2. Update struts.xml file to define interceptor using <interceptor> tag.
<interceptors> → Used to define all individual interceptors.
<interceptor> → Define actual interceptor with class and name attribute.
<interceptor-ref> → This is to call above created interceptor with the help of name attribute.
3. Run application with Apache tomcat server and hit below url will give below output:
http://localhost:8081/struts-demo/user.action
OUTPUT:
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.