본문 바로가기

WEB_Programming/JSTL

15. JSTL 폼 Select 예제

1. JSTL의 Form Select와 CheckBox Group 예제
1.1 form.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/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
>

1.2 result.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<html>
<head>
<title>EL Implicit Object Example Form Processor</title>
</head>
<body>
<h1>EL Implicit Object Example Form processor</h1>

<b>Shape of cake selected:</b>
<c:out value="${param.shape}"/>
 
 <br/>
<b>Toppings selected:</b>

<c:forEach var="aTopping" items="${paramValues.topping}">
 <c:out value="${aTopping}"/>
</c:forEach>
<br/>


<small><a href="index.jsp">back to form</a></small>
</body>
</html>


2. JSTL의 Selected 값을 폼으로 부터 얻어오기
2.1 potal.jsp
<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
>

2.2 showpotal.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
                           
<html>
 <c:choose>
    <c:when test="${param.portchoice == 'news'}">
      <head><title>News Portal</title></head>
      <body>
       <h1>Welcome to the News Portal!</h1>  
      </body>
    </c:when>
    <c:when test="${param.portchoice == 'weather'}">
       <head><title>Weather Portal</title></head>
       <body>
        <h1>You Get the Latest Weather!</h1>  
       </body>
    </c:when>
    <c:when test="${param.portchoice == 'entertainment'}">
       <head><title>Entertainment Portal</title></head>
       <body>
       <h1>Entertainment News Just for You!</h1>  
       </body>
    </c:when>
    <c:otherwise>
       <head><title>System Portal</title></head>
       <body>
       <h1>Application logic problem detected!</h1>  
       </body>
    </c:otherwise>
</c:choose>
</html>



3. Select 옵션 아이템을 추가하고 삭제하기
<%@ taglib uri="http://java.sun.com/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>

'WEB_Programming > JSTL' 카테고리의 다른 글

17. JSTL Form Action  (0) 2008.06.24
16. JSTL Form TextField  (0) 2008.06.24
14. JSTL Input 폼 처리  (0) 2008.06.20
13. JSTL CheckBox 폼 처리  (0) 2008.06.20
12. JSTL 예외 처리  (0) 2008.06.20