[英]<s:iterator not working properly for a list, i am using struts2
我有as:iterator,它正在迭代一个属性列表,因为列表中的每个项目都有属性key,value和category。在category的基础上,我需要将值填充到迭代器内部定义的div中。 我正在使用jquery标签。 此处的迭代器未正确迭代。请看一下易于理解的代码
<div id="tabs">
<ul>
<li><a href="#channel">Channel</a></li>
<li><a href="#connection">Connection</a></li>
</ul>
<s:iterator value="property" status="rowstatus">
<div id="channel">
<s:if test="(property[#rowstatus.index].category=='Channel')">
<table>
<tr><s:hidden name="property[%{#rowstatus.index}].key" />
<td><s:label value="%{property[#rowstatus.index].key}"> </s:label></td>
<td> <s:textfield name="property[%{#rowstatus.index}].value">
</s:textfield>
</td>
</tr>
</table>
</s:if>
</div>
<div id="connection">
<s:if test="(property[#rowstatus.index].category=='Connection')">
<table>
<tr><s:hidden name="property[%{#rowstatus.index}].key" />
<td><s:label value="%{property[#rowstatus.index].key}"></s:label></td>
<td> <s:textfield name="property[%{#rowstatus.index}].value">
</s:textfield>
</td>
</tr>
</table>
</s:if>
</div>
</s:iterator>
</div>
变更JSP
<div id="tabs">
<ul>
<li><a href="#channel">Channel</a></li>
<li><a href="#connection">Connection</a></li>
</ul>
<div id="channel">
<table>
<s:subset source="property" decider="channelDecider">
<s:iterator var="channel">
<tr><s:hidden name="property[%{property.indexOf(#channel)}].key" />
<td><s:label value="%{#channel.key}"> </s:label></td>
<td> <s:textfield name="property[%{property.indexOf(#channel)}].value" value="%{#channel.value}">
</s:textfield>
</td>
</tr>
</s:iterator>
</s:subset>
</table>
</div>
<div id="connection">
<table>
<s:subset source="property" decider="connectionDecider">
<s:iterator var="connection">
<tr><s:hidden name="property[%{property.indexOf(#connection)}].key" />
<td><s:label value="%{#connection.key}"> </s:label></td>
<td> <s:textfield name="property[%{property.indexOf(#connection)}].value" value="%{#connection.value}">
</s:textfield>
</td>
</tr>
</s:iterator>
</s:subset>
</table>
</div>
</div>
创建决策者
public Decider getChannelDecider() {
return new Decider() {
public boolean decide(Object element) throws Exception {
return ((MyElement)element).category.equals("Channel");
}
};
}
public Decider getConnectionDecider() {
return new Decider() {
public boolean decide(Object element) throws Exception {
return ((MyElement)element).category.equals("Connection");
}
};
}
注意, MyElement
是具有相应属性的列表元素类型。
迭代器工作正常。
您正在为集合中的每个项目而不是每个类似项目组创建一个div。
尽管<s:subset>
可以工作,但我还是可以提出一种更简单, 以S2为中心的解决方案。
其他MyElement
代码:
public class MyElement {
public boolean isChannel() { return category.equals("Channel"); }
public boolean isConnection() { return category.equals("Connection"); }
}
我不喜欢使用字符串来指示类型。
可以采用多种方法来拆分列表。 手动操作,轻松快捷; 通过实用程序类,很高兴在多个地方需要此功能; 等等
可能的实用程序类:
public class MyElementSplitter {
// Also getters for both of these; possibly wrapped with an immutable collection.
private List<MyElement> channels = new ArrayList<>();
private List<MyElement> connections = new ArrayList<>();
public MyElementSplitter(List<MyElement> elements) {
for (MyElement element: elements) {
if (element.isChannel()) {
channels.add(element);
} else if (element.isConnection()) {
connections.add(element);
}
}
}
}
注意:可以大大增强此功能,尤其是在您有其他类型的情况下。 如果类型不是由字符串确定的,则可以通过使代码具有通用性来公开其他类型来玩很多不错的游戏。
在操作中,您将元素分类并将拆分器显示给视图:
private MyElementSplitter elements; // And getter.
public String execute() {
// And any additional processing, list retrieval, etc.
elements = new MyElementSplitter(allElements);
return SUCCESS;
}
然后在视图中:
<div id="channel">
<s:iterator value="elements.channels" status="stat">
<table>
<tr>
<s:hidden name="property[%{#stat.index}].key" />
<td><s:label value="%{property[#stat.index].key}" /></td>
<td><s:textfield name="property[%{#stat.index}].value" /></td>
</tr>
</table>
</s:iterator>
</div>
<div id="connection">
<s:iterator value="elements.connections" status="stat">
<table>
<tr>
<s:hidden name="property[%{#stat.index}].key" />
<td><s:label value="%{property[#stat.index].key}" /></td>
<td><s:textfield name="property[%{#stat.index}].value" /></td>
</tr>
</table>
</s:iterator>
</div>
如果这些部分始终相同,那么我会将它们包装在基于JSP的自定义标记中。
那将给我们留下以下内容:
<div id="tabs">
<ul>
<li><a href="#channel">Channel</a></li>
<li><a href="#connection">Connection</a></li>
</ul>
<app:elementTab id="channel" values="elements.channels" />
<app:elementTab id="connection" values="elements.connections" />
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.