struts2 jstl iterator tag

て烟熏妆下的殇ゞ 提交于 2020-01-14 18:48:27

问题


I have been looking how to implement a thing in struts2 jstl but it is impossible for me to find the way.

When I load the jsp page from the action, I have a list of String lists.

I want to create as divs as elements have the list, but inside every div, I want to create as links as the third element of the sub-list.

So I use the s:iterator tag to parse the list. But I don't know how to iterate "${item[2]}" times inside the first iterator.

The code would be something like this:

<s:iterator value="functions" var="item" status="stat">
        <span class="operation">${item[1]}</span>
        <div id="${item[0]}">
            <s:for var $i=0;$i<${item[2]};$i++>
                <a href="#" id="link_$i">Link $i</a>
            </s:for>
        </div>
</s:iterator>

Where I have put the s:for tag is where I would like to iterate "${item[2]}" times...

Anyone can help me?

Thank you very much in advance, Aleix


回答1:


Make sure you've got the JSTL core library in scope in your JSP page:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

And simply use <c:forEach>. Something like this:

<c:forEach var="i" begin="0" end="${item[2] - 1}">
    <a href="#" id="link_${i}">Link ${i}</a>
</c:forEach>



回答2:


You should use List of Map if appropriate, example :

Action class

// List of raw type Map
private List<Map> functions = Lists.newArrayList(); // with getter

@Override
public String execute() {
    // loops {
        Map map = Maps.newHashMap();
        map.put("id", id);
        map.put("operation", operation);
        map.put("count", count); // count is int/Integer
        functions.add(map);
    // }

    return SUCCESS;
}

.jsp

<s:iterator value="functions">
    <span class="operation">${operation}</span>
    <div id="${id}">
        <s:iterator begin="0" end="count - 1" var="link">
            <a href="#" id="link_${link}">Link ${link}</a>
        </s:iterator>
    </div>
</s:iterator>


or with <s:a /> (example)

<s:a action="action_name" id="%{link}" anchor="%{link}">Link ${link}</s:a>

output

<a id="[id]" href="/namespace/action#[anchor]">Link [link]</a>

See also

Struts2 Guides -> Tag Reference -> s:iterator



来源:https://stackoverflow.com/questions/6351767/struts2-jstl-iterator-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!