2

I know that S2 provides a clean way to fetch the request parameters in you action class all you need to follow these simple rules.

  1. Create a property with same name as request parameter name.
  2. Create getter and setters for this property or make property public (for S2.1+)

However, when I do this in an AJAX call like this:

 $.ajax({
    url: '/gma/getJSONData.action?tspName='+tspName+'&thresholdType='+thresholdType,

I don't get the tspName parameter inside action class. I created the getter/setter for it. It's displaying null value.

Am I wrong somewhere?

EDIT: 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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Siddharth Trikha
  • 2,648
  • 8
  • 57
  • 101
  • Can you verify that the name of the parameter matches exactly as the one you are passing as parameter ? Also verify that this paramater is infact present in the request URL and its name matches with the one you want to receive in your action class – Saif Asif Dec 02 '13 at 06:46
  • What is the request url being sent ? Check in chrome-developer tools-> Network tab. – coding_idiot Dec 02 '13 at 06:46
  • @coding_idiot: /gma/getJSONData.action?tspName=RELIANCE&thresholdType=undefined – Siddharth Trikha Dec 02 '13 at 06:51
  • @coding_idiot: Inside the url it's filling the data properly. Is it that in an AJAX call this approach doesn't work whereas it working in a normal url submit??? – Siddharth Trikha Dec 02 '13 at 07:03
  • Can you show your getter/setter code? – Anupam Dec 02 '13 at 08:06
  • 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 initialze values?? – Siddharth Trikha Dec 02 '13 at 08:20
  • -1 I recommend reading java OOP concepts. Those fields are class variables, there's no way they can be initialized before the class itself. – coding_idiot Dec 02 '13 at 08:25
  • +1 Sometimes we make silly mistakes, this question will be helpful to those who are doing the same. – Anupam Dec 02 '13 at 08:27

1 Answers1

2

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176