时间:2014-08-01 07:57:58

标签: javascript jquery struts2 ognl valuestack

我有一个迭代属性列表的s:iterator,对于列表中的每个项都有属性键,值和类别。在类别的基础上,我需要将值填充到迭代器中定义的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>

2 个答案:

答案 0 :(得分:0)

更改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是具有相应属性的列表元素类型。

答案 1 :(得分:0)

迭代器工作正常。

您正在为集合中的每个项目创建一个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>