Custom Interceptor in Struts 2

Custom Interceptor in Struts 2

What are Interceptors?
Interceptors are used for performing operations like Validation, file upload, exception handling etc.

Advantages of Interceptors:
Separation of Concerns, If we need to remove any of the concerns like validation or exception handling etc.. We can remove them from struts.xml without disturbing the application.

Struts 2 provide a set of default interceptors (default interceptors), we can also create and implement our own interceptors. Creating and implementing own interceptors are known as Custom Interceptors.

Lets see an example for custom interceptors, We are going to check the time for the process.

Structure:
struct

Lets create 2 JSP files – input.jsp and showdetails.jsp

input.jsp

showdetails.jsp

login.java

MyInterceptor.java

struts.xml

Output:
input
show
Console output to check the timing function,

[2015-09-18T20:52:01.795+0530] [glassfish 4.0] [INFO] [] [] [tid: _ThreadID=101 _ThreadName=Thread-3] [timeMillis: 1442589721795] [levelValue: 800] [[
Start time is: 1442589721795 ms]]

 

 

 

 

 

Read More