现在的位置: 首页 > 综合 > 正文

使用 EL、JSTL 处理表单数据(转载)

2014年01月14日 ⁄ 综合 ⁄ 共 2324字 ⁄ 字号 评论关闭

常见输入类型  <input type="text">

Text(文本框)

name:字段名称

maxlength:最多输入字符

size:文本框宽度

value:默认值

Password(密码框)

name:字段名称

maxlength:最多输入字符

size:文本框宽度

value:默认值

Radio(单选按钮)

checked:默认选取

name:按钮名称

value:按钮值

Check(复选框)

checked:默认选取

name:按钮名称

value:按钮值

Select(下拉框)

multiple:多重选择

size:字段大小

name:名称

option value:值

 

综合示例:

 

Form.html

 

<html>

<head>

<title>CH9 - Form.html</title>

<meta http-equiv="Content-Type" content="text/html; charset=GB2312">

</head>

<body>

 

<form name="Example" method="post" action="Form.jsp">

<p> 姓名:<input type="text" name="Name" size="15" maxlength="15"></p>

<p>密码:<input type="password" name="Password" size="15" maxlength="15"></p>

<p>性别:<input type="radio" name="Sex" value="Male" checked>

<input type="radio" name="Sex" value="Female"></p>

<p>年龄:

<select name="Old">

<option value="10">10 ~ 20</option>

<option value="20" selected>21 ~ 30</option>

<option value="30">31 ~ 40</option>

<option value="40">41 ~ 65</option>

</select>

</p>

<p> 兴趣:

<input type="checkbox" name="Habit" value="Read">

看书

<input type="checkbox" name="Habit" value="Game">

电玩

<input type="checkbox" name="Habit" value="Travel">

旅游

<input type="checkbox" name="Habit" value="Music">

听音乐

<input type="checkbox" name="Habit" value="Tv">

看电视</p>

<p>

<input type="submit" value="传送">

<input type="reset" value="清除">

</p>

</form>

 

</body>

</html>

 

Form.jsp

 

<%@ page contentType="text/html;charset=GB2312" %>

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

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

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

 

<html>

<head>

<title>CH9 - Form.jsp</title>

</head>

<body>

 

<h2>使用 ELJSTL 处理表单数据</h2>

<fmt:requestEncoding value="GB2312" />

 

姓名:<c:out value="${param.Name}" default="Nothing" /> <br>

密码:<c:out value=" ${param.Password} " default="Nothing" /><br>

性别:<c:if test="${param.Sex == 'Male'}"><br></c:if>

<c:if test="${param.Sex == 'Female'}"><br></c:if>

年龄:<c:choose>

<c:when test="${param.Old == 10}">10 ~ 20<br></c:when>

<c:when test="${param.Old == 20}">21 ~ 30<br></c:when>

<c:when test="${param.Old == 30}">31 ~ 40<br></c:when>

<c:otherwise>41 ~ 65<br></c:otherwise>

兴趣:<c:forEach items="${paramValues.Habit}" var="habit">

<c:choose>

<c:when test="${habit == 'Read'}"><li>看书</li></c:when>

<c:when test="${habit == 'Game'}"><li>电玩</li></c:when>

<c:when test="${habit == 'Travel'}"><li>旅游</li></c:when>

<c:when test="${habit == 'Music'}"><li>听音乐</li></c:when>

<c:when test="${habit == 'Tv'}"><li>看电视</li></c:when>

</c:choose>

</c:forEach>

</body>

</html>

 

 

抱歉!评论已关闭.