I was checking the value of tspName in my Action class constructor, so was printing null. However, in my execute method it displays the value correctly. Why is it so?? It means before constructor call it does not initialize values?
Probably you should learn the basics how Struts2 works. When you make a request a filter is invoked and the dispatcher is handling the request via creating the action context and building action instance.
Then interceptors are invoked on this action. One of the interceptors of the defaultStack
is params
interceptor. It's responsible to populate your action with request parameters, to be more Struts2 action context parameters.
It means you can always get parameters from the action context. See How can we access request parameters passed into an Action.
The constructor of the action is called before any interceptor is invoked, so the action is not populated yet and properties aren't initialized. On the other hand when the action is executed all interceptors are already invoked, so the action is populated. Before constructor or after constructor it doesn't matter. What is matter is params
interceptor in the action configuration.
You can always get parameters like described in the link above, or directly from the servlet request like in this answer. All features of Struts2 framework is available to you.