user1073616
user1073616

Reputation:

How do Action class constructors work with Struts2?

I am directly forwarded to a method within my action class (by struts.xml) but I do not actually create a new instance of my Action Class. I ask because I have some variables to set before anything can be done with the ActionClass, and I thought constructors might help me do just that.

Upvotes: 2

Views: 2899

Answers (1)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

Struts 2 provides a Prepare Interceptor to prepare your data before the actual execute method runs.This interceptor calls prepare() on actions which implement Preparable. Some of the use cases for using the prepare method in your action class are

  1. Loading an object from the database so that when parameters are set they can be set on this object.
  2. Preparing/init any data which is needed before the actual method call.

In short this interceptor is like an init/constructor for the Action class and you have all control to initialize any required parameters or do any initial lookup.

For details refer to the interceptor page prepare-interceptor

Upvotes: 4

Related Questions