Java/JSTL/Form TextField

Материал из Java эксперт
Версия от 06:17, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

JSTL Form Action: Get TextField Value

<html>
  <head>
  </head>
  <body>
    <form method="POST" action="form2.jsp">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="42%" id="AutoNumber1">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font size="4" color="#FFFFFF">Please Login</font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td width="19%">User Name</td>
          <td width="81%">
            <input type="text" name="uid" size="20" />
          </td>
        </tr>
        <tr>
          <td width="19%">Password</td>
          <td width="81%">
            <input type="password" name="pwd" size="20" />
          </td>
        </tr>
        <tr>
          <td width="100%" colspan="2">
            <p align="center">
              <input type="submit" value="Submit" name="action" />
              <input type="reset" value="Reset" name="B2" />
            </p>
          </td>
        </tr>
      </table>
    </form>
    <p align="left">
      <i>Note: you may use any ID/Password, security is not
      checked.</i>
    </p>
  </body>
</html>
///////////////////////////////////////////////////////////
//File: form2.jsp
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <h3>Welcome back 
  <c:out value="${param.uid}" />
  !</h3>
</html>





JSTL Form: Using TextField

<%@ 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 Submit Form TextField Action

<html>
  <head>
    <title>Page Data Example</title>
  </head>
  <body>
    <table border="1">
      <form method="POST" action="params2.jsp">
        <tr>
          <td width="33%">
            <b>First Name</b>
          </td>
          <td width="73%">
            <input type="text" name="first" size="40" />
          </td>
        </tr>
        <tr>
          <td width="33%">
            <b>Last Name</b>
          </td>
          <td width="73%">
            <input type="text" name="last" size="40" />
          </td>
        </tr>
        <tr>
          <td width="33%">
            <b>Address</b>
          </td>
          <td width="73%">
            <input type="text" name="address" size="40" />
          </td>
        </tr>
        <tr>
          <td width="33%">
            <b>City</b>
          </td>
          <td width="73%">
            <input type="text" name="city" size="20" />
          </td>
        </tr>
        <tr>
          <td width="33%">
            <b>State</b>
          </td>
          <td width="73%">
            <input type="text" name="state" size="20" />
          </td>
        </tr>
        <tr>
          <td width="33%">
            <b>ZIP</b>
          </td>
          <td width="73%">
            <input type="text" name="zip" size="20" />
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <input type="submit" value="Submit" name="action" />
            <input type="reset" value="Reset" name="action" />
          </td>
        </tr>
      </form>
    </table>
  </body>
</html>
///////////////////////////////////////////////////////////////////////////
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Page Data Example</title>
  </head>
  <body>
    <table border="1" width="310">
      <tr>
        <td bgcolor="#0000FF" width="98">
          <b>
            <font color="#FFFFFF" size="4">Name</font>
          </b>
        </td>
        <td bgcolor="#0000FF" width="196">
          <b>
            <font color="#FFFFFF" size="4">Value</font>
          </b>
        </td>
      </tr>
      <c:forEach var="aItem" items="${paramValues}">
        <tr>
          <td width="98">
            <b>
              <c:out value="${aItem.key}" />
            </b>
          </td>
          <td width="196">&#160;
          <c:forEach var="aValue" items="${aItem.value}">
            <c:out value="${aValue}" />
          </c:forEach>
          </td>
        </tr>
      </c:forEach>
    </table>
  </body>
</html>





Property Access

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Property Access</title>
  </head>
  <body>
    <c:if test="${pageContext.request.method=="POST"}">
    <c:set var="idx" value="name" />
    param.name = 
    <c:out value="${param.name}" />
    <br />
    param[name] = 
    <c:out value="${param[idx]}" />
    <br />
    </c:if>
    <br />
    <form method="post">Please enter your name? 
    <input type="text" name="name" />
    <input type="Submit" />
    <br />
    </form>
  </body>
</html>