Javatpoint Logo

91-9990449935

 0120-4256464

Struts 2 prepare interceptor example

The prepare interceptor calls prepre() method on the action if it implements Preparable interface. It calls prepare() method before the execute() method.

To use the prepare interceptor, you need to implement Preparable interface in your action class and override its method prepare.

It is found in the default stack bydefault. So you don't need to specify it explicitely.

Parameters of prepare interceptor

There is only 1 parameter defined for prepare interceptor.

ParameterDescription
alwaysInvokePrepare It is set to true bydefault.

Example of prepare interceptor


Action class

The action class must implement the Preparable interface and override its method prepare().

You can see output on the console, preparation logic is printed before the actual logic.

Output

struts 2 prepare interceptor example output

Now remove the entry of prepare interceptor from the struts.xml file, preparation logic will not be printed.