struts-default.xml
file. We can create our own custom Interceptors and plugin into a Struts2 based web application. Framework creates an object of ActionInvocation that encapsulates the action and all the interceptors configured for that action. Each interceptors are called before the action gets called. Once the action is called and result is generated, each interceptors are again called in reverse order to perform post processing work. Interceptors can alter the workflow of action. It may prevent the execution of action. MyLoggingInterceptor
in package net.viralpatel.struts2.interceptors
and copy following content into it. package net.viralpatel.struts2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class MyLoggingInterceptor implements Interceptor{
private static final long serialVersionUID = 1L;
public String intercept(ActionInvocation invocation) throws Exception {
String className = invocation.getAction().getClass().getName();
long startTime = System.currentTimeMillis();
System.out.println("Before calling action: " + className);
String result = invocation.invoke();
long endTime = System.currentTimeMillis();
System.out.println("After calling action: " + className
+ " Time taken: " + (endTime - startTime) + " ms");
return result;
}
public void destroy() {
System.out.println("Destroying MyLoggingInterceptor...");
}
public void init() {
System.out.println("Initializing MyLoggingInterceptor...");
}
}
Code language: Java (java)
struts.xml
file and use it with actions. To configure newly created interceptor, add following code in struts.xml <interceptors>
<interceptor name="mylogging"
class="net.viralpatel.struts2.interceptor.MyLoggingInterceptor">
</interceptor>
<interceptor-stack name="loggingStack">
<interceptor-ref name="mylogging" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
Code language: HTML, XML (xml)
This code has to be added after <result-types > tag in <package ></package> Here we have configured a new interceptor mylogging with tag <interceptor >. Also note that we have defined an interceptor-stack with name loggingStack. This is to make sure Sturts2 calls all the default interceptors as well while calling our custom interceptor. This is very important as the validation logic will not work in our struts2 application if we ignore the default stack of interceptors. We can make the new loggingStack as default interceptor stack or can configure it at each action level. In order to make it default stack, we should add following in struts.xml <default-interceptor-ref name="loggingStack"></default-interceptor-ref>
Code language: HTML, XML (xml)
Once we add above code in Struts.xml, the logginStack will be applied to all the actions in that package. Also we may want to apply the custom interceptor stack to only certain actions. To do so, we must add interceptor-ref tag in action. <action name="login"
class="net.viralpatel.struts2.LoginAction">
<interceptor-ref name="loggingStack"></interceptor-ref>
<result name="success" type="tiles">/welcome.tiles</result>
<result name="error">Login.jsp</result>
</action>
Code language: HTML, XML (xml)
Code language: HTML, XML (xml)Initializing MyLoggingInterceptor... .. .. .. Before calling action: net.viralpatel.struts2.LoginAction .. .. After calling action: net.viralpatel.struts2.LoginAction Time taken: 313 ms .. .. .. Destroying MyLoggingInterceptor...
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Dear Viral,
Above is the code snippet from "Configuring Interceptor in struts.xml" section.
I want to know what if i swap the order of interceptor declaration in strut.xml like this
In other words, if i am creating my own interceptor, how to decide the order of precedence for interceptor.
Thanks
Arpit
Hi Viral,
Are there any JAR files required for running interceptors?
If so, what are they.
I have used interceptors after creating the login application hello world and have just added the MyLoggingInterceptor.java and made the changes in struts.xml.
Now, my struts.xml looks like this:-
Welcome.jsp
Login.jsp
I am gettign Error 404, please help as in where am I missing simething.
Hi Viral,
Are there any JAR files required for running interceptors?
If so, what are they.
I have used interceptors after creating the login application hello world and have just added the MyLoggingInterceptor.java and made the changes in struts.xml.
Now, my struts.xml looks like this:-
Welcome.jsp
Login.jsp
I am gettign Error 404, please help as in where am I missing simething.
Dear Viral,
I have done the interceptor on the code for Hello world applicationa dn have added the MyLoggingInterceptor and corresponding code in struts.xml.
I am getting error 404.
Are there any jar files to be added for this?
If so, what are they?
Dear Viral,
The issue is solved.
I was using the after in struts.xml and hence the error 404 - resource not found was getting displayed.
It vanished when I used before in the in struts.xml.
Thanks again.
Dear Viral,
Under whcih event (on doign what is the destroy() called whcih will print :-
public void destroy() {
System.out.println("Destroying MyLoggingInterceptor...");
}
Hi Viral,
i am facing problem in implementtaion of <sx:autocompleter tag. Actually our reuirement is onselect of autocomplter we have to refresh a div having two ot three dropdown. in refreshing of div i am including one mode jsp that have only target Div .because in the same jsp we are not getting div refresh using Ajax updater as well so we have two jsp one have all the jsp contents and other one have only that div which to be refresh.
Can you pls provide me any complete example that will help me .
Tuitorial is Excellent for the Beginner thanx
Thanks Viral ji
Hi,
I am having a problem with session, when passing through interceptor, when I get to my action, the session is null. Any ideas?
thanks
Hi,
How are you accessing
request
object from your Interceptor?[code language="java"]
final HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
Session session = request.getSession();
[/code]
Check if you are accessing session correctly!
Consider following class declaration declared above
[code language="java"]
public class MyLoggingInterceptor implements Interceptor
[/code]
Implement another Interface which is Sub Interface of Aware like following
[code language="java"]
public class MyLoggingInterceptor implements Interceptor,ServletRequestAware {
public void getServletRequest(HttpServletRequest request) { }
}
[/code]