您的位置:首页 > Web前端 > JavaScript

使用JSTL进行页面服务器端校验

2007-05-14 17:06 232 查看
验证JSP(包含用户输入)

验证条件为,姓名和年龄均为必填字段,并且年龄必须大于18岁




<%...@ 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" %>




<html>


<head>




<title>Currency Formatting</title>


</head>


<body>




<c:if test="${param.submitted}">


<c:if test="${empty param.name}" var="noName"/>


<c:if test="${empty param.age}" var="noAge"/>


<c:catch var="errorAge">


<fmt:parseNumber var="paraseAge" value="${param.age}"/>


<c:if test="${paraseAge < 18}" var="youngAge"/>


</c:catch>




<c:if test="${not(noName or noAge or youngAge)}">





<c:set value="${param.name}" var="name" scope="request"></c:set>


<c:set value="${param.age}" var="age" scope="request"></c:set>


<jsp:forward page="result.jsp"></jsp:forward>


</c:if>


</c:if>








<form action="validate.jsp">


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


输入姓名(必填):<input type="text" name="name"/><br>


<c:if test="${noName}">请输入姓名<br></c:if>


输入年龄(必填,大于18岁)<input type="text" name="age"/><br>


<c:if test="${noAge}">请输入年龄<br></c:if>


<c:if test="${youngAge}">年龄必须大于18<br></c:if>


<input type="submit" value="submit"/>


</form>




</body>


</html>



验证通过后的页面为result.jsp,如果验证不通过,则跳转会原输入页面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: