Java/JSTL/Collections

Материал из Java эксперт
Перейти к: навигация, поиск

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>
<form method="post"> </form>

Parse for Tokens

Enter a sentence:
<input width="20" maxwidth="20" name="text" size="50" />
 

<input type="submit" name="parse" value="Parse" />

   <c:if test="${pageContext.request.method=="POST"}">
<c:set var="i" value="1" /> <c:forTokens items="${param.text}" var="word" delims=" ,.?!"> <c:set var="i" value="${i}" /> </c:forTokens>
             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>
<form method="post"> </form>
           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.
         
<input width="20" maxwidth="20" name="item" size="20" />
<input type="submit" name="add" value="Add" /> <input type="submit" name="remove" value="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>