Java Tutorial/JSTL/Form Select — различия между версиями

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

Версия 17:44, 31 мая 2010

Add/Remove Form Select Option Item

<%@ 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>
    <table border="0">
      <form method="post">
        <tr bgcolor="blue">
          <td colspan="2">
            <font color="white">Updatable Collections</font>
          </td>
        </tr>
        <tr>
          <td valign="top">
            <select NAME="choice" SIZE="5" width="20">
              <c:forEach var="item" items="${list}">
                <option>
                  <c:out value="${item}" />
                </option>
              </c:forEach>
            </select>
          </td>
          <td valign="top">Enter a item to add or remove.
          <br />
          <input width="20" maxwidth="20" name="item" size="20" />
          <br />
          <input type="submit" name="add" value="Add" />
          <input type="submit" name="remove" value="Remove" />
          </td>
        </tr>
      </form>
    </table>
  </body>
</html>





JSTL Form Select and CheckBox Group

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<html>
<head>
<title>EL Implicit Object Example</title>
</head>
<body>
<h1>EL Implicit Object Examples</h1>
<form action="formproc.jsp" method="post">
<table>
<tr>
 <td colspan="2"><h3>Design a Cake</h3></td>
</tr>
<tr>
<td>Cake shape:</td>
<td>
  <select name="shape">
     <option>round</option>
     <option>square</option>
     <option>heart</option>
   </select>
</td>
</tr>
<tr>
<td valign="top">Toppings</td>
<td>
  <input type="checkbox" name="topping" value="choc">Chocolate</input><br/>
  <input type="checkbox" name="topping" value="cane">Candy Cane</input><br/>
  <input type="checkbox" name="topping" value="flower">Flower</input><br/>
</td>
</tr>
<tr>
<td colspan="2">
  <center><input type="submit" value="Send"/></center>
</td>
</tr>
</table>
</form>
</body>
</html>





Use JSTL to Check Selected Value From Form Select Option

<html>
<head><title>Select Your Portal</title></head>
<body>
<h1>Select your preferred portal:</h1>
<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>