简体   繁体   中英

Struts2 how to bind Set<Object> from view back to controller

Let's say my controller look like this:

public class myController {

    private MyCustomItem acte;

    ...
    // getter and setter
}

and the MyCustomItem class have a Set of another class, like this

public class MyCustomItem {

    private Set<AnotherClass> signataires;

    ...
    // getter and setter
}

Finally, the AnotherClass item have some String attributes.

What I want to do is, from the view linked to my controller, set those String attributes when I submit a form, so I wrote my view.jsp like this:

<!-- some html before -->
<s:form namespace="/my/namespace" action="MyController_execute">
<s:iterator value="acte.signataires" status="signaStatus">
    <s:hidden name="id" value="%{id}" />
    <s:property value="collectivite.nom"/>
    <s:textfield name="acte.signataires(%{#signaStatus.index}).commentaire" cssStyle="width:250px;"/>
</s:iterator>
<s:submit/>
</s:form>

Afte I submitted the form, in my controller if I try to get some values from my Set<> acte.signataires , they are null:

for (AnotherClass signataire : acte.getSignataires()) {
    System.out.println(signataire.getCommentaire()); // this print NULL
}

any help on this? Is my jsp mapping bad? I also tried a very simple syntax like <s:textfield name="commentaire" cssStyle="width:250px;"/> but it won't work either

Do you need the property signataires to be a Set? I suggest to you to use an ArrayList so that you can access each element by the index (signataires[0], signataires[1], etc...). Using an ArrayList then you could do in this way:

<s:form namespace="/my/namespace" action="MyController_execute">
<s:iterator value="acte.signataires" status="signaStatus">
    <s:hidden name="id" value="%{id}" />
    <s:property value="collectivite.nom"/>
    <INPUT type="text" name="acte.signataires[<s:property value="%{#signaStatus.index}"/>].commentaire" cssStyle="width:250px;"/>
</s:iterator>
<s:submit/>
</s:form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM