Servlet与Jsp学习笔记--8、JSTL

Choose

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

 

<html>

<head><title>Accessing a Scoped Value</title></head>

<body>

<h2>Hello

 

<c:choose>

 

<c:when test="${empty param.name}">

 Esteemed Visitor

 </c:when>

 

<c:otherwise>

 

<c:out value="${param.name}" />

 

</c:otherwise>

 

</c:choose>

 

</h2>

 

</body>

</html>

input_jstl

<%@ page contentType="text/html" %>

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

 

<html>

 <head>

    <title>User Info Entry Form</title>

 </head>

 <body bgcolor="white">

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

      <table>

        <tr>

          <td>Name:</td>

          <td>

            <input type="text" name="userName">

          </td>

        </tr>

        <tr>

         <td>Birth Date:</td>

          <td>

            <input type="text" name="birthDate">

          </td>

          <td>(Use format yyyy-mm-dd)</td>

        </tr>

        <tr>

          <td>Email Address:</td>

          <td>

            <input type="text" name="emailAddr">

          </td>

          <td>(Use format name@company.com)</td>

        </tr>

        <tr>

          <td>Gender:</td>

          <td>

            <input type="radio" name="gender" value="m" checked>Male<br>

            <input type="radio" name="gender" value="f">Female

          </td>

        </tr>

        <tr>

          <td>Lucky number:</td>

          <td>

            <input type="text" name="luckyNumber">

          </td>

          <td>(A number between 1 and 100)</td>

        </tr>

        <tr>

          <td>Favorite Foods:</td>

          <td>

            <input type="checkbox" name="food" value="z">Pizza<br>

            <input type="checkbox" name="food" value="p">Pasta<br>

            <input type="checkbox" name="food" value="c">Chinese

          </td>

        </tr>

        <tr>

          <td colspan=2>

            <input type="submit" value="Send Data">

          </td>

        </tr>

      </table>

    </form>

 

    You entered:<br>

    Name: <c:out value="${param.userName}" /><br>

   Birth Date: <c:out value="${param.birthDate}" /><br>

    Email Address: <c:out value="${param.emailAddr}" /><br>

    Gender: <c:out value="${param.gender}" /><br>

    Lucky Number: <c:out value="${param.luckyNumber}" /><br>

    Favorite Food:

      <c:forEach items="${paramValues.food}" var="current">

        <c:out value="${current}" />&nbsp;

      </c:forEach>

 </body>

</html>

pageContext.request

<%@ page contentType="text/html" %>

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

 

<html>

  <head>

    <title>Request Info</title>

 </head>

 <body bgcolor="white">

 

    The following information was received:

    <ul>

      <li>Request Method:

        <c:out value="${pageContext.request.method}" />

      <li>Request Protocol:

        <c:out value="${pageContext.request.protocol}" />

      <li>Context Path:

        <c:out value="${pageContext.request.contextPath}" />

      <li>Servlet Path:

        <c:out value="${pageContext.request.servletPath}" />

      <li>Request URI:

        <c:out value="${pageContext.request.requestURI}" />

      <li>Request URL:

        <c:out value="${pageContext.request.requestURL}" />

      <li>Server Name:

        <c:out value="${pageContext.request.serverName}" />

      <li>Server Port:

        <c:out value="${pageContext.request.serverPort}" />

      <li>Remote Address:

        <c:out value="${pageContext.request.remoteAddr}" />

      <li>Remote Host:

        <c:out value="${pageContext.request.remoteHost}" />

      <li>Secure:

        <c:out value="${pageContext.request.secure}" />

      <li>Cookies:<br>

        <c:forEach items="${pageContext.request.cookies}" var="c">

          &nbsp;&nbsp;<b><c:out value="${c.name}" /></b>:

          <c:out value="${c.value}" /><br>

        </c:forEach>

      <li>Headers:<br>

        <c:forEach items="${headerValues}" var="h">

          &nbsp;&nbsp;<b><c:out value="${h.key}" /></b>:

          <c:forEach items="${h.value}" var="value">

            <br>

            &nbsp;&nbsp;&nbsp;&nbsp;<c:out value="${value}" />

          </c:forEach>

          <br>

        </c:forEach>

    </ul>

 </body>

</html>

validate

<%@ page contentType="text/html" %>

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

 

<html>

 <head>

    <title>User Info Entry Form</title>

 </head>

 <body bgcolor="white">

    <jsp:useBean id="userInfo"

      class="com.ora.jsp.beans.userinfo.UserInfoBean">

      <jsp:setProperty name="userInfo" property="*" />

    </jsp:useBean>

 

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

      <input type="hidden" name="submitted" value="true">

      <table>

        <c:if test="${param.submitted && userInfo.userNameValid == false}">

          <tr><td></td>

          <td colspan="2"><font color="red">

            Please enter your Name

          </font></td></tr>

        </c:if>

        <tr>

          <td>Name:</td>

          <td>

            <input type="text" name="userName"

              value="<c:out value="${userInfo.userName}" />">

          </td>

        </tr>

        <c:if test="${param.submitted && !userInfo.birthDateValid}">

          <tr><td></td>

          <td colspan="2"><font color="red">

            Please enter a valid Birth Date

          </font></td></tr>

        </c:if>

        <tr>

          <td>Birth Date:</td>

          <td>

            <input type="text" name="birthDate"

              value="<c:out value="${userInfo.birthDate}" />">

          </td>

          <td>(Use format yyyy-mm-dd)</td>

        </tr>

        <c:if test="${param.submitted && !userInfo.emailAddrValid}">

          <tr><td></td>

          <td colspan="2"><font color="red">

            Please enter a valid Email Address

          </font></td></tr>

        </c:if>

        <tr>

          <td>Email Address:</td>

          <td>

            <input type="text" name="emailAddr"

              value="<c:out value="${userInfo.emailAddr}" />">

          </td>

          <td>(Use format name@company.com)</td>

        </tr>

        <c:if test="${param.submitted && !userInfo.genderValid}">

          <tr><td></td>

          <td colspan="2"><font color="red">

            Please select a valid Gender

          </font></td></tr>

        </c:if>

        <tr>

          <td>Gender:</td>

          <td>

            <c:choose>

              <c:when test="${userInfo.gender == 'f'}">

                <input type="radio" name="gender" value="m">Male<br>

                <input type="radio" name="gender" value="f" checked>Female

              </c:when>

              <c:otherwise>

                <input type="radio" name="gender" value="m" checked>Male<br>

                <input type="radio" name="gender" value="f">Female

              </c:otherwise>

            </c:choose>

          </td>

        </tr>

        <c:if test="${param.submitted && !userInfo.luckyNumberValid}">

          <tr><td></td>

          <td colspan="2"><font color="red">

            Please enter a Lucky Number between 1 and 100

          </font></td></tr>

        </c:if>

        <tr>

          <td>Lucky number:</td>

          <td>

            <input type="text" name="luckyNumber"

              value="<c:out value="${userInfo.luckyNumber}" />">

          </td>

          <td>(A number between 1 and 100)</td>

        </tr>

        <c:if test="${param.submitted && !userInfo.foodValid}">

          <tr><td></td>

          <td colspan="2"><font color="red">

            Please select only valid Favorite Foods

          </font></td></tr>

        </c:if>

        <tr>

          <td>Favorite Foods:</td>

          <td>

            <input type="checkbox" name="food" value="z"

              <c:if test="${userInfo.pizzaSelected}">checked</c:if>>Pizza<br>

            <input type="checkbox" name="food" value="p"

              <c:if test="${userInfo.pastaSelected}">checked</c:if>>Pasta<br>

            <input type="checkbox" name="food" value="c"

              <c:if test="${userInfo.chineseSelected}">checked</c:if>>Chinese

          </td>

        </tr>

        <tr>

          <td colspan=2>

            <input type="submit" value="Send Data">

          </td>

        </tr>

      </table>

    </form>

 </body>

</html>

 

Bean

package com.ora.jsp.beans.userinfo;

 

import java.io.*;

import java.util.*;

import com.ora.jsp.util.*;

 

public class UserInfoBean implements Serializable {

    // Validation constants

    private static String DATE_FORMAT_PATTERN = "yyyy-MM-dd";

    private static String[] GENDER_LIST = {"m", "f"};

    private static String[] FOOD_LIST = {"z", "p", "c"};

    private static int MIN_LUCKY_NUMBER = 1;

    private static int MAX_LUCKY_NUMBER = 100;

 

    // Properties

    private String birthDate;

    private String emailAddr;

    private String[] food;

    private String luckyNumber;

    private String gender;

    private String userName;

 

    /**

     * Returns the birthDate property value

     */

    public String getBirthDate() {

        return (birthDate == null ? "" : birthDate);

    }

 

    /**

     * Sets the birthDate property value

     */

    public void setBirthDate(String birthDate) {

        this.birthDate = birthDate;

    }

 

    /**

     * Validates the birthDate property

     *

     * @return true if the property is set to a valid value, false otherwise.

     */

    public boolean isBirthDateValid() {

        boolean isValid = false;

        if (birthDate != null &&

            StringFormat.isValidDate(birthDate, DATE_FORMAT_PATTERN)) {

            isValid = true;

        }

        return isValid;

    }

 

    /**

     * Returns the emailAddr property value

     */

    public String getEmailAddr() {

        return (emailAddr == null ? "" : emailAddr);

    }

 

    /**

     * Sets the emailAddr property value

     */

    public void setEmailAddr(String emailAddr) {

        this.emailAddr = emailAddr;

    }

 

    /**

     * Validates the emailAddr property

     *

     * @return true if the property is set to a valid value, false otherwise.

     */

    public boolean isEmailAddrValid() {

        boolean isValid = false;

        if (emailAddr != null &&

                 StringFormat.isValidEmailAddr(emailAddr)) {

            isValid = true;

        }

        return isValid;

    }

 

    /**

     * Returns the food property value

     */

    public String[] getFood() {

        return (food == null ? new String[0] : food);

    }

 

    /**

     * Sets the food property value

     */

    public void setFood(String[] food) {

        this.food = food;

    }

 

    /**

     * Validates the food property

     *

     * @return true if the property is set to a valid value, false otherwise.

     */

    public boolean isFoodValid() {

        boolean isValid = false;

        if (food == null ||

                 StringFormat.isValidString(food, FOOD_LIST, true)) {

            isValid = true;

        }

        return isValid;

    }

 

    /**

     * Returns true if the food property includes the marker for

     * pizza

     */

    public boolean isPizzaSelected() {

        return isFoodTypeSelected("z");

    }

 

    /**

     * Returns true if the food property includes the marker for

     * pasta

     */

    public boolean isPastaSelected() {

        return isFoodTypeSelected("p");

    }

 

    /**

     * Returns true if the food property includes the marker for

     * Chinese

     */

    public boolean isChineseSelected() {

        return isFoodTypeSelected("c");

    }

 

    /**

     * Returns the luckyNumber property value

     */

    public String getLuckyNumber() {

        return (luckyNumber == null ? "" : luckyNumber);

    }

 

    /**

     * Sets the luckyNumber property value

     */

    public void setLuckyNumber(String luckyNumber) {

        this.luckyNumber = luckyNumber;

    }

 

    /**

     * Validates the luckyNumber property

     *

     * @return true if the property is set to a valid value, false otherwise.

     */

    public boolean isLuckyNumberValid() {

        boolean isValid = false;

        if (luckyNumber != null &&

                 StringFormat.isValidInteger(luckyNumber, MIN_LUCKY_NUMBER,

                   MAX_LUCKY_NUMBER)) {

            isValid = true;

        }

        return isValid;

    }

 

    /**

     * Returns the gender property value

     */

    public String getGender() {

        return (gender == null ? "" : gender);

    }

 

    /**

     * Sets the gender property value

     */

    public void setGender(String gender) {

        this.gender = gender;

    }

 

    /**

     * Validates the gender property

     *

     * @return true if the property is set to a valid value, false otherwise.

     */

    public boolean isGenderValid() {

        boolean isValid = false;

        if (gender != null &&

                 StringFormat.isValidString(gender, GENDER_LIST, true)) {

            isValid = true;

        }

        return isValid;

    }

 

    /**

     * Returns the userName property value

     */

    public String getUserName() {

        return (userName == null ? "" : userName);

    }

 

    /**

     * Sets the userName property value

     */

    public void setUserName(String userName) {

        this.userName = userName;

    }

 

    /**

     * Validates the gender property

     *

     * @return true if the property is set to a valid value, false otherwise.

     */

    public boolean isUserNameValid() {

        boolean isValid = false;

        if (userName != null) {

            isValid = true;

        }

        return isValid;

    }

 

    /**

     * Returns true if all property values have valid values

     * (they are only set if the value is valid).

     */

    public boolean isValid() {

        return isBirthDateValid() && isEmailAddrValid() &&

            isFoodValid() && isLuckyNumberValid() &&

            isGenderValid() && isUserNameValid();

    }

 

    /**

     * Returns true if the food property includes the specified food

     * type

     */

    private boolean isFoodTypeSelected(String foodType) {

        if (food == null) {

            return false;

        }

        boolean selected = false;

        for (int i = 0; i < food.length; i++) {

            if (food[i].equals(foodType)) {

                selected = true;

                break;

            }

        }

        return selected;

    }

}

 

c:set

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

 

<html>

 <head>

    <title>Counter page</title>

 </head>

 <body bgcolor="white">

 

    <%-- Increment the counter --%>

    <c:set var="sessionCounter" scope="session"

      value="${sessionCounter + 1}" />

 

    <h1>Counter page</h1>

 

    This page has been visited <b>

    <c:out value="${sessionCounter}" />

    </b> times within the current session.

    <p>

    Click here to load the page through a

    <a href="counter2.jsp">regular link</a>.

    <p>

    Click here to load the page through an

    <a href="<c:url value="counter2.jsp" />">encoded link</a>.

 </body>

</html>

 

posted @ 2009-01-16 16:06  昕友软件开发  阅读(861)  评论(1编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯