JSTL基础功能

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

一.处理复选框参数

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

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

<!DOCTYPE html PUBLIC "-//W 3C //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>标签捕捉异常

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

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

属性名

描述

是否必须

默认值

var

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

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值