码迷,mamicode.com
首页 > Web开发 > 详细

jsp引用JSTL核心标签库

时间:2017-05-22 21:30:53      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:springmvc   标签   学习   基本应用   page   string   ref   显示   语句   

一、引用JSTL

1、  JSTL的引入可以让JSP代码中<%%>等代码消失掉,再结合EL表达式,会更加方便以及美观。

2、  各套框架(还没有学习,比如struts,SpringMVC等)都有自己的标签库,这时JSTL可以作为公共、通用的,横行于各框架中。

    操作:

      第一步:下载JSTL包

        下载地址:http://tomcat.apache.org/taglibs/

技术分享

技术分享

技术分享

技术分享

技术分享

第二步,新建一个jsp页面

技术分享

 

  JSTl基本应用:

  1.核心标签库:包括基本的语言功能,变量操作,流程控制等等。

  2.国际化和格式化标签库:进行国际化操作和格式化操作,例如格式化时间等等。

  3.SQL标签库:主要用于对关系型数据库的存储操作。

  4.XML标签库:主要用于对xml格式数据的操作。

  5.函数标签库:主要定义对字符串操作的函数库,在EL表达式中运行。

  在上述五种标签库中,常用的有核心标签库、格式化标签库和函数标签库,在下面的内容中我们将一 一给大家讲解。

二、核心标签库

  1.多用途核心标签

    1.1用于显示的<c:out>

      <c:out>标签是一个最常用的标签,用于在JSP中显示数据。

  NewFile.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>JSTL Hello World!</title>
</head>
<body>
<c:out value="Hello World"></c:out>
</body>
</html>

 

运行结果:

技术分享

  1.2用于赋值的<c:set>标签

    <c:set>标签用于为变量或Javabean中的变量属性赋值的工作。

  NewFile.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<c:set value="this is andy" var="oneString"></c:set>
${oneString} //该示例将名为"oneString"的变量赋值为"this is andy",其中作用范围为page。 </body> </html>

 

 运行结果:

技术分享

  1.3用于删除的<C:remove>标签

      <C:remove>标签用于删除存在于scope中的变量。

  NewFile.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<c:remove var="sampleValue" scope="session"></c:remove>
${sessionScope.sampleValue}
//该示例将存在于Session中名为"sampleValue"的变量删除。下一句EL表达式显示该变量时,该变量已经不存在。
</body>
</html>

  1.4异常捕获的<C:catch>标签

    <C:catch>标签允许在jsp也面中捕获异常。它包含一个var属性,是一个描述异常的变量。

    NewFile.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<c:catch>${param.sampleSingleValue[9]==3}</c:catch>
${err}
//当"${param.sampleSingValue[9]==3}"表达式有异常时,可以从var属性"err"得到异常的内容,通常判断"err"是否为null来决定错误信息的提示。
</body>
</html>

 

 运行结果:

技术分享

 

 2.条件控制标签

   2.1用于判断的<c:if>标签

    <c:if>标签用于简单的条件语句。

下面看一个示例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<c:if test="${paramValue.sampleValue[2]==12}" var="visits" >
    It is 12
</c:if><br>
${visits}
//判断request请求提交的传入控件数组参数中,下标为“2”的控件内容是否为“12”,若为12则
//显示“It is 12”。判断结果保存在page范围中的“visits”变量中。
</body> </html>

 

  2.2复杂的判断<c:choose>、<c:when>、<c:otherwise>标签  

    这三个标签实现复杂条件判断语句,类似“if,elseif”的条件语句。

    1.<c:choose>标签没有属性,可以被认为是父标签,<c:when>、<c:otherwise> 

      将作为其子标签来使用。

    2.<c:when>标签等价于“if”语句,它包含一个test属性,该属性表示需要判断的条件。

    3.<c:otherwise>标签没有属性,它等价于“else”语句。

 

下面看一个复杂条件语句的示例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<c:choose>
<c:when test="${paramValues.sampleValue[2]==11}">
    not 12 not 13,it is 11
</c:when>
<c:when test="${paramValues.sampleValue[2]==12}">
    not 11 not 13,it is 12
</c:when>
<c:when test="${paramValues.sampleValue[2]==13}">
   not 11 not 12,it is 13
</c:when>
<c:otherwise>
    not 11、12、13
</c:otherwise>
</c:choose>
//判断request请求提交的传入控件数组参数中,下标为“2”控件内容是否为“11”或“12”或“13”,并根据判断结果显示各自的语句,若都不是则显示“not 11、12、13” </body> </html>

 

运行结果:

技术分享

 

 

未完待续......

 

jsp引用JSTL核心标签库

标签:springmvc   标签   学习   基本应用   page   string   ref   显示   语句   

原文地址:http://www.cnblogs.com/wangpengpeng/p/6889066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!