Java/JSTL/Collections — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 09:17, 1 июня 2010
Содержание
JSTL Iteration over tokens
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>
<head> <title>Updatable Collections</title> </head> <body>
Parse for Tokens |
Enter a sentence:
|
<input type="submit" name="parse" value="Parse" /> |
<c:if test="${pageContext.request.method=="POST"}">
Word <c:out value="${i}" /> |
<c:out value="${word}" /> |
</c:if> </body>
</html>
</source>
JSTL Modify a collection
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <c:if test="${pageContext.request.method=="POST"}">
<c:choose> <c:when test="${param.add!=null}"> <c:if test="${list!=null}"> <c:set var="list" value="${list}," scope="session" /> </c:if> <c:set var="list" value="${list}${param.item}" scope="session" /> </c:when> <c:when test="${param.remove!=null}"> <c:set var="list2" value="" /> <c:forEach var="item" items="${list}"> <c:if test="${item!=param.item}"> <c:if test="${list2!=""}"> <c:set var="list2" value="${list2}," /> </c:if> <c:set var="list2" value="${list2}${item}" /> </c:if> </c:forEach> <c:set var="list" value="${list2}" scope="session" /> <c:remove var="list2" /> </c:when> </c:choose>
</c:if> <html>
<head> <title>Updatable Collections</title> </head> <body>
Updatable Collections |
|
<select NAME="choice" SIZE="5" width="20"> <c:forEach var="item" items="${list}"> <option> <c:out value="${item}" /> </option> </c:forEach> </select> |
Enter a item to add or remove.
|
</body>
</html>
</source>
String Collection Examples
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>
<head> <title>String Collection Examples</title> </head> <body>
String Collection Example
<c:set var="str" value="Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday" /> Input String:
<c:out value="${str}" />
Iterating:
<c:forEach var="day" items="${str}"> <c:out value="${day}" />
</c:forEach> </body>
</html>
</source>