Dankwansere
Dankwansere

Reputation: 61

How to pass parameters from textfields inside of <s:iterator> tag to the action in Struts 2?

I have a JSP with an <s:iterator> tag's list that iterates over an object created in the action with getters/setters.

I'm able to pull the mapped value from the action to the <s:iterator> tag's textfields, now I want to be able to pass the textfields values from <s:iterator> back to the action class to map the object, but I realized that it does not call my setter method.

Here is my JSP:

<s:iterator  status="stat" value="parameterList" >
    <tr id="<s:property value="#stat.index"/>">
        <td><s:textfield id="parameterList[%{#stat.index}].queryParameter" name="parameterList[%{#stat.index}].queryParameter"  cssClass="size30" labelposition="left"  ></s:textfield> </td>
        <td><s:textfield id="parameterList[%{#stat.index}].parameterValue" name="parameterList[%{#stat.index}].parameterValue"  cssClass="size30" labelposition="left"  ></s:textfield> </td>
    </tr>
</s:iterator>

and this is a snippet of the action:

private List<QueryParameter> parameterList = new ArrayList<QueryParameter>();

public void setParameterList(List<QueryParameter> parameterList) {
    this.parameterList = parameterList;
}

public DQA getDqaObject() {
    return dqa;
}

The getters are called fine when I navigate to the JSP, but when I submit a form from the JSP, I expectd the values that I put into the textfields inside <s:iterator> tag should pass and update the object in the action class, but the setter is not being called.

Upvotes: 0

Views: 1185

Answers (2)

Roman C
Roman C

Reputation: 1

OGNL doesn't call a setter method when new object is created. Also you don't need it if you want to update existing object. It should be initialized before the action is populated.

prepare interceptor can call your action if it implements Preparable to execute prepare() method where you can initialize the object to be updated.

If you need a parameter to retrieve your object from the database you can use paramsPrepareParams interceptor's stack. You can see description Changing parameters after bind in Struts 2.

Also don't use ModelDriven interface because it doesn't work correctly by default with the above stack. It requires reconfiguring the default stack to make the model pushed before params interceptor fires or otherwise you have to handle parameters manually, i.e. from the action context or ParametersAware which is already deprecated.

Upvotes: 1

Dankwansere
Dankwansere

Reputation: 61

I resolved the issue, the problem was that in my getter(Which I forgot to post in the question) there were some logic that was preventing the getter to proceed to actually call the objects setter to set the parameter.

Upvotes: 1

Related Questions