abderrahim_05
abderrahim_05

Reputation: 465

How to update struts 2 List from a TextField Tag

I want to have my collection a List < Pair < Integer, String>> gets updated from the corresponding JSP containing an iterator generating a list of < s:TextFiedl >.

Here is what i did, but the list is empty or containing null values.

The Action :

public class ManageRegleArithmetiqueAction extends ActionSupport implements Preparable, JspDataPovider {
    private List<Pair<Integer, String>> propositionNumLabelList = new ArrayList<Pair<Integer,String>>();

    public void setPropositionNumLabelList(List<Pair<Integer, String>> propositionNumLabelList) {
    this.propositionNumLabelList = propositionNumLabelList;
    }

    public List<Pair<Integer, String>> getPropositionNumLabelList() {
        return propositionNumLabelList;
    }

}

The JSP :

<s:iterator value="propositionNumLabelList" var="pair" status="status">
    <tr>
    <td CLASS="IHMText">
    <s:textfield key="propositionNumLabelList[%{#status.index}].cle" id="%{'propositionNum_' + #status.index}" />
    </td>
    <td CLASS="IHMText">
    <s:textfield key="propositionNumLabelList[%{#status.index}].valeur" id="%{'propositionLabel_' + #status.index}" />
    </td>
    </tr>
</s:iterator>

The Pair Class:

public class Pair<S, T> implements Serializable {


public Pair(S cle, T valeur) {
    super();
    this.cle = cle;
    this.valeur = valeur;
}


private static final long serialVersionUID = 7231580297094672707L;

private S cle;

private T valeur;

public S getCle() {
    return cle;
}

public void setCle(S cle) {
    this.cle = cle;
}

public T getValeur() {
    return valeur;
}

public void setValeur(T valeur) {
    this.valeur = valeur;
}

public static <S, T> Pair<S, T> ofKeyAndValue(S cle, T valeur) {
    return new Pair<S, T>(cle, valeur);
}
}

Upvotes: 1

Views: 329

Answers (1)

Roman C
Roman C

Reputation: 1

You should add a default constructor to Pair class. Without it Struts is not able to instantiate a bean when populating your action properties. Struts can populate a property of action if it knows how to instantiate a class that this property is defined. This is also true if you need to set nested properties. Struts by default instantiate a class if the property is not initialized before setting its value.

public Pair() {
}

Upvotes: 1

Related Questions