-->

支柱逻辑:迭代输入字段(Struts logic:iterate input field)

2019-06-23 19:08发布

目前,我有下面的代码和数据显示的罚款。

<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>  

但现在我需要做“量”的一部分修改。 用户应该能够更新字段,按提交并在其发送到服务器,“myList中”应自动与新的数量更新。

我试图寻找这个帮助,但所有我不断寻找的是如何只显示数据,而不是修改它的实例。 任何帮助,将不胜感激。

Answer 1:

所以这是棘手的,因为有很多事情会按顺序做了它的工作。 首先,声明你的变量与HTML标记的迭代器内,用属性INDEXED = TRUE和ID不同于这个名字,我还拿出了“IndexID为”属性使用索引的简单的“指数”二字:

<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>  

之后,为了使支柱能够获取和设置bean的属性,你需要声明EXTRA get和set方法您的收藏对象的内部,使用您在iterate标签的ID写了名字。 在这种情况下,你会写为“myListI” 2种额外的get和set方法:

public void setMyListI(int index, myBean value){
    this.myList.add(value);
}
public myBean getMyListI(int index){
    return this.myList.get(index);
}


Answer 2:

我认为Th0rndikes答案多半是正确的。 我的实现略有不同,所以它可能是值得一试这一点。

形成

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> 

总之,我并没有在iterate标签使用类型,使用属性标签来代替。 在bean加入吸气剂与在JSP(PARAM)的迭代ID的匹配名字与索引作为方法参数并获得成功。



Answer 3:

从理论上讲, indexed的支柱html标签的属性可以被用于此:

只有有效的内部logic:iterate标签。 如果为真,则HTML标记的名称将呈现为“ID [34] .propertyName”。 括号中的数字将用于每一迭代中生成并从祖先逻辑采取:iterate标签。

但是,有没有相应的indexed的属性html:errors的标签,这限制了它的实用性。 此外,所需的组合idnameproperty属性可以是相当混乱。

我发现它更容易使用JSP脚本小程序来生成属性名,包括迭代指数。 下面的代码需要在窗体中有一个字符串数组属性“量”。

<% int idx=0; %>
<logic:iterate ...>
    <html:text property='<%= "quantity[" + idx + "]" %>'/>
    <html:errors property='<%= "quantity[" + idx + "]" %>'/>
    <% i++; %>
</logic:iterate>


Answer 4:

看看这个: http://wiki.apache.org/struts/StrutsCatalogLazyList

索引属性

Struts的html标签有一个索引的属性,这将产生相应的HTML表单提交时填充豆的集合。 诀窍是命名id属性一样的 索引属性

例如,下面的JSP ...

   <logic:iterate name="skillsForm" property="skills" id="skills">

       <html:text name="skills" property="skillId" indexed="true"/>

   </logic:iterate>

...会生成以下HTML

<input type="text" name="skills[0].skillId value="..."/>
<input type="text" name="skills[1].skillId value="..."/>
....
<input type="text" name="skills[n].skillId value="..."/>

当表单提交的BeanUtils首先调用getSkills(指数)方法检索索引豆接着setSkillId(..)所检索的豆。



文章来源: Struts logic:iterate input field