Java Tutorial/JSTL/Form Select

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

Add/Remove Form Select Option Item

   <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>





JSTL Form Select and CheckBox Group

   <source lang="java">

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <html> <head> <title>EL Implicit Object Example</title> </head> <body>

EL Implicit Object Examples

<form action="formproc.jsp" method="post">

Design a Cake

Cake shape:
 <select name="shape">
    <option>round</option>
    <option>square</option>
    <option>heart</option>
  </select>
Toppings
 <input type="checkbox" name="topping" value="choc">Chocolate</input>
<input type="checkbox" name="topping" value="cane">Candy Cane</input>
<input type="checkbox" name="topping" value="flower">Flower</input>
<input type="submit" value="Send"/>

</form> </body> </html></source>





Use JSTL to Check Selected Value From Form Select Option

   <source lang="java">

<html> <head><title>Select Your Portal</title></head> <body>

Select your preferred portal:

<form action="showportal.jsp" method="get"> <select name="portchoice"> <option>news</option> <option>weather</option> <option>entertainment</option> </select> <input type="submit" value="Select"/> </form> </body> </html></source>