简体   繁体   中英

struts2 jstl iterator tag

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM