Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we need Preparable Interface in Struts2?

We have Interceptor, we have custom interceptor where we can do all that we want to do before or after our action executes.
Then what is the need to use Preparable interface and implement prepare method for it?
Is this another option or there is some specific aim to do like that?

like image 717
NIVESH SENGAR Avatar asked Feb 14 '12 08:02

NIVESH SENGAR


1 Answers

Well Preparable interface act in conjunction with Prepare Interceptor.This interface has one method defined prepare() and as its name suggest this method is responsible to allow the action to prepare itself.

Prepare interceptor calls prepare() on actions which implement Preparable. This interceptor is very useful for any situation where you need to ensure some logic runs before the actual execute method runs.So if you see this is some kind if init for your action class and it makes sure that before the Action's execute or any other method get called, this method prepare your execute method to work fine.

If you see the default-stack define in core, you will come to know that this interceptor is being called before params or workflow interceptor which indicates its purpose.

A typical use of this is to run some logic to load an object from the database so that when parameters are set they can be set on this object. For details read the doc of Prepare interceptor for details how it work closely with Preparable interface.In short Prepare interceptor will come in to act only when action implements Preparable.

Prepare-Interceptor

like image 190
Umesh Awasthi Avatar answered Oct 22 '22 19:10

Umesh Awasthi