Interceptors in Struts2

Interceptors in Struts2

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.

Default Interceptors:
Struts 2 has some in-built default interceptors, also we can build our own custom interceptors. In this post, we are going to explore default interceptors.

Interceptor
Description
alias
Parameters to have different name aliases
checkbox
Managing checkboxes to check which is checked and unchecked
conversionError Error message when converting string to parameter value
createsession
creating new session
execAndWait
Sends intermediary waiting page when process executes background
Exception
Throwing exceptions
fileupload
for uploading files
logger
simple logging by displaying name
params
request parameters on the action
Prepare
Pre-preocessing works
timer
Shows how long the it takes for action to execute
token
checks for valid token to prevent duplication
validation
provides validation

Lets see an example,

Lets check the “timer” interceptor to check the time taken for executing the action.

Structure:

structure

first.jsp

display.jsp

Sdao.java

struts.xml

Output:
op1
op2
In your log file you can see the output of timer interceptor,
op3

 


 

 

3 thoughts on “Interceptors in Struts2

Leave a Reply

Your email address will not be published. Required fields are marked *