[英]<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.