Java Tutorial/JSTL/Form Input
Содержание
Check Parameter Value and Output Error Message
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.ru/jstl/fmt" %>
<h1>Peter"s Junk-Mail Service</h1>
<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}"/>" />
<br />
<c:if test="${noName}">
<small><font color="red">
Note: you must enter a name
</font></small>
</c:if>
</p>
Enter your email address:
<input type="text" name="email"
value="<c:out value="${param.email}"/>" />
<br />
<c:if test="${noEmail}">
<small><font color="red">
Note: you must enter an email address
</font></small>
</c:if>
</p>
Enter your age:
<input type="text" name="age" size="3"
value="<c:out value="${param.age}"/>" />
<br />
<c:choose>
<c:when test="${noAge}">
<small><font color="red">
Note: you must enter your age
</font></small>
</c:when>
<c:when test="${badAge}">
<small><font color="red">
Note: I couldn"t decipher the age you typed in
</font></small>
</c:when>
<c:when test="${youngAge}">
<small><font color="red">
Please grow older and try again.
</font></small>
</c:when>
</c:choose>
</p>
<input type="submit" value="Sign up" />
</form>
Get Date value from Form
index.jsp
<%@ 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>
JSTL Form Error Check and Forward
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;
}
}
Parse input from Form
<%@ 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">
<table>
<tr>
<td width="100%" colspan="2">
Number Formatting
</td>
</tr>
<tr>
<td width="47%">Enter a number to be parsed:</td>
<td width="53%">
<input type="text" name="num" size="20" />
</td>
</tr>
<tr>
<td width="100%" colspan="2">
<p align="center">
<input type="submit" value="Submit" name="submit" />
<input type="reset" value="Reset" name="reset" />
</p>
</td>
</tr>
</table>
<P> </p>
</form>
<c:if test="${pageContext.request.method=="POST"}">
<table>
<tr>
<td width="100%" colspan="2">
Formatting: <c:out value="${param.num}" escapeXml="false" />
</td>
</tr>
<tr>
<td width="51%">type="number"</td>
<td width="49%">
<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" />
</td>
</tr>
<tr>
<td width="51%">type="currency"</td>
<td width="49%">
<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" />
</td>
</tr>
<tr>
<td width="51%">type="percent"</td>
<td width="49%">
<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" />
</td>
</tr>
<tr>
<td width="51%">type="number" integerOnly="true"</td>
<td width="49%">
<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" />
</td>
</tr>
</table>
</c:if>
</body>
</html>
Set Parameter Value
index.jsp
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.ru/jstl/fmt" %>
<h1>Peter"s Junk-Mail Service</h1>
<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" />
<P>
Enter your name:
<input type="text" name="name"
value="<c:out value="${param.name}"/>" />
<br />
<c:if test="${noName}">
<small><font color="red">
Note: you must enter a name
</font></small>
</c:if>
</p>
<P>
Enter your email address:
<input type="text" name="email"
value="<c:out value="${param.email}"/>" />
<br />
<c:if test="${noEmail}">
<small><font color="red">
Note: you must enter an email address
</font></small>
</c:if>
</p>
<P>
Enter your age:
<input type="text" name="age" size="3"
value="<c:out value="${param.age}"/>" />
<br />
<c:choose>
<c:when test="${noAge}">
<small><font color="red">
Note: you must enter your age
</font></small>
</c:when>
<c:when test="${badAge}">
<small><font color="red">
Note: I couldn"t decipher the age you typed in
</font></small>
</c:when>
<c:when test="${youngAge}">
<small><font color="red">
Note: You"re too young to receive pornographic
junk mail. Please grow older and try again.
</font></small>
</c:when>
</c:choose>
</p>
<input type="submit" value="Sign up" />
</form>
Use ForEach to List All Form Parameters
index.jsp
<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">
<table>
<tr><td>Enter an adjective:</td>
<td><input type="text" name="adjective" /></td>
</tr>
<tr><td>Enter a noun:</td>
<td><input type="text" name="noun" /></td>
</tr>
<tr><td>Enter a color:</td>
<td><input type="text" name="color" /></td>
</tr>
</table>
<input type="submit" value="Send parameters" />
</form>
</body>
</html>
Use JSTL to Create URL From Form Input
index.jsp
<%@ 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">
<table>
<tr><td>Enter name:</td>
<td><input type="text" name="name" /></td></tr>
<tr><td>Enter age:</td>
<td><input type="text" name="age" /></td></tr>
<tr><td>Enter gender:</td >
<td><input type="text" name="gender" /></td></tr>
</table>
<input type="submit" value="Submit details" />
</form>
</body>
</html>