打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
JSTL基础功能
这里主要讨论了JSTL的一些基础功能,主要包括:处理复选框参数、从表单中读取日期、错误处理、验证用户输入。

一.处理复选框参数

这个功能比较简单,主要是利用JSTL中paramValues这个隐式变量,它和param是相对应个,只不过前者用来处理多个同名参数,而后者是用来处理单个参数的。看下面的例子:

    country.jsp给出了一组多选框,用于选择国家

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

    <head>

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

       <title>Hello World</title>

    </head>

    <body>

       ---------------------处理多选框的表单-------------------<br>

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

           <input value="中国" type="checkbox" name="country">

           中国

           <br>

           <input value="英国" type="checkbox" name="country">

           英国

           <br>

           <input value="美国" type="checkbox" name="country">

           美国

           <br>

           <input value="法国" type="checkbox" name="country">

           法国

           <br>

           <input value="德国" type="checkbox" name="country">

           德国

           <br>

           <input type="submit">

       </form>

    </body>

</html>

checkbox.jsp:处理复选框参数

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%@ taglib uri="fmt" prefix="fmt"%>

<%@ taglib uri="core" prefix="c"%>

<html>

    <head>

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

       <title>处理复选框参数</title>

    </head>

    <body>

       <c:choose>

           <c:when test="${not empty paramValues.country}">

               您选择了下列国家:

                <ul>

                  <c:forEach items="${paramValues.country}" var="adj">

                     <li>

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

                     </li>

                  </c:forEach>

                </ul>

           </c:when>

           <c:otherwise>

           没有选中任何国家

            </c:otherwise>

       </c:choose>

    </body>

</html>

二.错误处理

当JSTL发生错误时,我们可以有3中方法来处理它:

● 不作任何处理,此时JSP容器会给我们打印出详细的错误信息,对开发人员而言有助于debug,但是对用户而言不是很好

● 使用<c:catch>标签捕捉异常

● 使用JSP的errorPage属性,指定一个通用的错误处理页面

第一种和第三种处理方式都比较常见,这里主要说一下如何使用<c:catch>标签来处理页面异常,它的属性如下表所示:

属性名

描述

是否必须

默认值

var

将错误信息作为变量存储起来

当<c:catch>标签体里面的内容发生错误时,它并不会导致整个页面的终止执行,它只会让<c:catch>标签中剩下的内容终止执行,而<c:catch>标签之外的内容仍然会继续执行。如果没有给<c:catch>标签指定var属性,那么<c:catch>标签体中的所有错误将会被忽略,页面继续执行;如果指定了var属性,那么错误信息讲话存储在一个变量中,这个变量名就由var属性指定。如果想得到关于错误的更多详细信息,可以通过如下的代码:<c:out value="${parseError.message}"/>带打印错误信息,其中parseError就是我们在<c:catch&gt;标签之中通过var属性指定的值。

三.验证输入

JSTL不能用来做很复杂的数据验证,比如验证电话号码的合法性之类的。它主要用于如下几种简单的验证:

     确保用户在一个表单域中输入了内容

     检查一个表单域中有内容时另一个域是否有或者没有

     检查用户输入的数字是否超过了某个限制

    这通常是一个应用的表示层进行验证的顺序。对输入进行验证时,有两个要注意的地方:首先就是在页面中的什么地方对输入进行验证,其次就是当输入非法时如何提示用户,一遍他能够修正错误。下图给出了一个利用JSTL来进行简单验证的模型:


 

当输入没有通过验证时重复显示表单,仅当输入通过验证之后才将表单中的数据进行提交,它是通过重复循环同一个页面来实现的,下面给出了一个例子,代码如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%@ taglib uri="fmt" prefix="fmt"%>

<%@ taglib uri="core" prefix="c"%>

<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">

    <p>

       Thanks for signing up for our junk-mail service. Once you submit your information on the form below, you'll begin to receive all the "spam"

you ever wanted.

    </p>

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

    <p>

       Enter your name:

       <input type="text" name="name" 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 adult junk mail. Please grow older and try again. </font> </small>

           </c:when>

       </c:choose>

    </p>

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

</form>

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
转帖工具(放到首页左栏随时调用)
暴jsp源码的网页
JSTL使用 - - JavaEye技术网站
form表单中的input type=“submit”
用JavaScript制作在线测试题
DWR3.0 dwr 返回值(数组,集合,Map)[转] - K.Y.Chill的日志 -...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服