Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts logic:iterate input field

I currently have the following code and the data is displayed fine.

<logic:iterate name="myList" id="product"  indexId="iteration" type="com.mycompany.MyBean">  
    <tr>  
        <td> <bean:write name="product" property="weight"/> </td>  
        <td> <bean:write name="product" property="sku"/> </td>  
        <td> <bean:write name="product" property="quantity"/> </td>  
    </tr>  
</logic:iterate>  

But now I need to make the "quantity" part modifiable. The user should be able to update that field, press submit and when its sent to the server, "myList" should automatically update with the new quantities.

I've tried searching for help on this but all I keep finding is examples on how to display data only, not modify it. Any help would be appreciated.

like image 501
LatinCanuck Avatar asked Jan 16 '23 21:01

LatinCanuck


2 Answers

So this is tricky, because there are many things to get done in order for it to work. First, declare your tags inside the iterator with the html tags, with attribute INDEXED=TRUE and an ID DIFFERENT THAN THE NAME, i also took out the "indexId" attribute to use the simple "index" word for the index:

<logic:iterate name="myList" id="myListI"   type="com.mycompany.MyBean">  
<tr>  
    <td> <html:input name="myListI" property="weight"  indexed="true"/> </td>  
    <td> <html:input name="myListI" property="sku"  indexed="true"/> </td>  
    <td> <html:input name="myListI" property="quantity"  indexed="true"/> </td>  
</tr>  

after that, in order for struts to be able to get and set the attributes of your beans, you need to declare EXTRA get and set methods inside your collection object, using the name you wrote in the id of the iterate tag. In this case, you would write 2 extra get and set methods for the "myListI" :

public void setMyListI(int index, myBean value){
    this.myList.add(value);
}
public myBean getMyListI(int index){
    return this.myList.get(index);
}
like image 88
Th0rndike Avatar answered Jan 23 '23 15:01

Th0rndike


I think Th0rndikes answer is mostly correct. My implementation is slightly different, so it might be worth trying this as well.

Form

private List<Parameter> activeParameters;

public List<Parameter> getActiveParameters() {
    return activeParameters;
}

public Parameter getParam(int index){
    return this.activeParameters.get(index);
}

JSP

<logic:iterate name="MyForm" property="activeParameters" id="param">
  <tr>
    <td><bean:write name="param" property="prompt"/></td>
    <td><html:text name="param" property="value" indexed="true"/></td>
  </tr>
</logic:iterate>

In summary, I didn't use Type in the iterate tag, using the property tag instead. In the bean adding a getter with matched the name of the iterate ID in the JSP (param) with an index as a method parameter did the trick.

like image 22
javatestcase Avatar answered Jan 23 '23 13:01

javatestcase