Java Tutorial/JSTL/Form Input
Содержание
Check Parameter Value and Output Error Message
<source lang="java">
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.ru/jstl/fmt" %>
Peter"s Junk-Mail Service
<c:if test="${param.submitted}">
<c:if test="${empty param.name}" var="noName" /> <c:if test="${empty param.email}" var="noEmail" /> <c:if test="${empty param.age}" var="noAge" /> <c:catch var="ageError"> <fmt:parseNumber var="parsedAge" value="${param.age}" /> <c:if test="${parsedAge < 13}" var="youngAge" /> </c:catch> <c:if test="${not empty ageError}" var="badAge" /> <c:if test="${not (noName or noEmail or noAge or badAge or youngAge)}"> <c:set value="${param.name}" var="name" scope="request"/> <c:set value="${param.email}" var="email" scope="request"/> <c:set value="${param.age}" var="age" scope="request"/> <jsp:forward page="spamFormHandler.jsp" /> </c:if>
</c:if> <form method="post">
<input type="hidden" name="submitted" value="true" /> Enter your name: <input type="text" name="name" value="<c:out value="${param.name}"/>" />
<c:if test="${noName}"> Note: you must enter a name </c:if> </p> Enter your email address: <input type="text" name="email" value="<c:out value="${param.email}"/>" />
<c:if test="${noEmail}"> Note: you must enter an email address </c:if> </p>
Enter your age: <input type="text" name="age" size="3" value="<c:out value="${param.age}"/>" />
<c:choose> <c:when test="${noAge}"> Note: you must enter your age </c:when> <c:when test="${badAge}"> Note: I couldn"t decipher the age you typed in </c:when> <c:when test="${youngAge}"> Please grow older and try again. </c:when> </c:choose> </p> <input type="submit" value="Sign up" />
</form></source>
Get Date value from Form
index.jsp
<source lang="java">
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <form method="post" action="dateHandler.jsp">
Please enter your birthday: <select name="month"> <option value="Jan">January</option> <option value="Feb">February</option> <option value="Mar">March</option> <option value="Apr">April</option> <option value="May">May</option> <option value="Jun">June</option> <option value="Jul">July</option> <option value="Aug">August</option> <option value="Sep">September</option> <option value="Oct">October</option> <option value="Nov">November</option> <option value="Dec">December</option> </select> <select name="day"> <c:forEach begin="1" end="31" var="day"> <option><c:out value="${day}"/></option> </c:forEach> </select> <select name="year"> <c:forEach begin="1930" end="2003" var="year"> <option><c:out value="${year}"/></option> </c:forEach> </select> <input type="submit" value="Submit" />
</form></source>
JSTL Form Error Check and Forward
<source lang="java">
package beans; public class Bid {
/** Holds value of property price. */ private long price; /** Holds value of property item. */ private String item; /** Creates a new instance of Bid */ public Bid() { } /** Getter for property price. * @return Value of property price. * */ public long getPrice() { return this.price; } /** Setter for property price. * @param price New value of property price. * */ public void setPrice(long price) { this.price = price; } /** Getter for property item. * @return Value of property item. * */ public String getItem() { return this.item; } /** Setter for property item. * @param item New value of property item. * */ public void setItem(String item) { this.item = item; }
}</source>
Parse input from Form
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %> <html>
<head> <title>Parse Number</title> </head> <body> <form method="POST">
Number Formatting |
|
Enter a number to be parsed: |
<input type="text" name="num" size="20" /> |
<input type="submit" value="Submit" name="submit" /> <input type="reset" value="Reset" name="reset" /> |
</form> <c:if test="${pageContext.request.method=="POST"}">
Formatting: <c:out value="${param.num}" escapeXml="false" /> |
|
type="number" |
<c:catch var="e"> <fmt:parseNumber var="i" type="number" value="${param.num}" /> <c:out value="${i}" escapeXml="false" /> </c:catch> <c:out value="${e}" escapeXml="false" /> |
type="currency" |
<c:catch var="e"> <fmt:parseNumber var="i" type="currency" value="${param.num}" /> <c:out value="${i}" escapeXml="false" /> </c:catch> <c:out value="${e}" escapeXml="false" /> |
type="percent" |
<c:catch var="e"> <fmt:parseNumber var="i" type="percent" value="${param.num}" /> <c:out value="${i}" escapeXml="false" /> </c:catch> <c:out value="${e}" escapeXml="false" /> |
type="number" integerOnly="true" |
<c:catch var="e"> <fmt:parseNumber var="i" integerOnly="true" type="number" value="${param.num}" /> <c:out value="${i}" escapeXml="false" /> </c:catch> <c:out value="${e}" escapeXml="false" /> |
</c:if> </body>
</html></source>
Set Parameter Value
index.jsp
<source lang="java">
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.ru/jstl/fmt" %>
Peter"s Junk-Mail Service
<c:if test="${param.submitted}">
<c:if test="${empty param.name}" var="noName" /> <c:if test="${empty param.email}" var="noEmail" /> <c:if test="${empty param.age}" var="noAge" /> <c:catch var="ageError"> <fmt:parseNumber var="parsedAge" value="${param.age}" /> <c:if test="${parsedAge < 13}" var="youngAge" /> </c:catch> <c:if test="${not empty ageError}" var="badAge" /> <c:if test="${not (noName or noEmail or noAge or badAge or youngAge)}"> <c:set value="${param.name}" var="name" scope="request"/> <c:set value="${param.email}" var="email" scope="request"/> <c:set value="${param.age}" var="age" scope="request"/> <jsp:forward page="spamFormHandler.jsp" /> </c:if>
</c:if> <form method="post">
<input type="hidden" name="submitted" value="true" />
Enter your name:
<input type="text" name="name"
value="<c:out value="${param.name}"/>" />
<c:if test="${noName}">
Note: you must enter a name
</c:if>
Enter your email address:
<input type="text" name="email"
value="<c:out value="${param.email}"/>" />
<c:if test="${noEmail}">
Note: you must enter an email address
</c:if>
Enter your age:
<input type="text" name="age" size="3"
value="<c:out value="${param.age}"/>" />
<c:choose>
<c:when test="${noAge}">
Note: you must enter your age
</c:when>
<c:when test="${badAge}">
Note: I couldn"t decipher the age you typed in
</c:when>
<c:when test="${youngAge}">
Note: You"re too young to receive pornographic
junk mail. Please grow older and try again.
</c:when>
</c:choose>
<input type="submit" value="Sign up" />
</form></source>
Use ForEach to List All Form Parameters
index.jsp
<source lang="java">
<html>
<head> <title>Set page parameters</title> </head> <body> This page allows you to enter information that is sent as request parameters to another page. The next page lists them. <P /> <form action="listPageParameters.jsp" method="get">
Enter an adjective: | <input type="text" name="adjective" /> |
Enter a noun: | <input type="text" name="noun" /> |
Enter a color: | <input type="text" name="color" /> |
<input type="submit" value="Send parameters" /> </form> </body>
</html></source>
Use JSTL to Create URL From Form Input
index.jsp
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <c:set var="originalURL" value="http://localhost:8080/chapter05/core/url/url.jsp" /> <html>
<head> <title>the c:url action </title> </head> <body> This page takes 3 values that you specify, and forwards them to another JSP. That JSP will create a URL to another page, that then extracts the parameters and displays them. <p /> <form action="createURL.jsp" method="post">
Enter name: | <input type="text" name="name" /> |
Enter age: | <input type="text" name="age" /> |
Enter gender: | <input type="text" name="gender" /> |
<input type="submit" value="Submit details" /> </form> </body>
</html></source>