JEE_JSTL标签

JSTL标签库:

由5个功能不同的标签库组成,在使用这些标签之前必须在JSP页面中使用<%@taglib uri=标签地址 prefix=调用的标签前缀%>来指定引用标签库。

核心标签库:包含Web应用的常见工作,比如:循环、表达式赋值、基本输入输出等。

国际化标签库:用来格式化显示数据的工作,比如:对不同区域的日期格式化等。

SQL标签库:可以做访问数据库的工作。

XML标签库:用来访问XML文件的工作,这是JSTL标签库的一个特点。

函数标签库:用来读取已经定义的某个函数。

此外,JSTL还提供了EL表达式语言(Expression Language)来进行辅助的工作。

 

标签库
URI
前缀
Core
http://java.sun.com/jsp/jstl/core
c
XML processing
http://java.sun.com/jsp/jstl/xml
x
I18N formatting
http://java.sun.com/jsp/jstl/fmt
fmt
Database access
http://java.sun.com/jsp/jstl/sql
sql
Functions
http://java.sun.com/jsp/jstl/functions
fn

 JSTL使用前要先导包配置环境。

 

核心标签库:
(1)表达式控制标签:out标签、set标签、remove标签、catch标签。
(2)流程控制标签:if标签、choose标签、when标签、otherwise标签。
(3)循环标签:forEach标签、forTokens标签。
(4)URL操作标签:import标签、url标签、redirect标签。

 

表达式标签:
<c:out>标签
是一个最常用的标签,用于在JSP中显示数据。
语法1:<c:out value=”要显示的数据对象” [escapeXml=”true|false”] [default=”默认值”]>
语法2:<c:out value=”要显示的数据对象” [escapeXml=”true|false”]>默认值</c:out>
escapeXml:设定是否转换特殊字符(如&lt、&gt等一些转义字符),在默认值为true的情况下直接在输出&lt的,如果改为false将会进行转义输出“<”等。
 

index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>应用&lt;c:out&gt;标签输出字符串“水平线标记&lt;hr&gt;</title>
</head>
<body>
escapeXml属性为true时:
<c:out value="水平线标记<hr>" escapeXml="true"></c:out>
<br>
escapeXml属性为false时:
<c:out value="水平线标记<hr>" escapeXml="false"></c:out>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>14.1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

<c:set>标签,

主要用于将变量存取于JSP范围中或JavaBean属性中。

1.<c:set value=”1” var=”name1” [scope=”page|request|session|application”]>

2.<c:set var=”name2” [scope=”page|request|session|application”]>2</c:set>

3.<c:set value=”3” target=”JavaBean对象” property=”属性名”/>

4.<c:set target=”JavaBean对象” property=”属性名”>4</c:set>

UserInfo.java
package com.wgh;

public class UserInfo {
    private String name="";    //名称属性

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    
}
index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>应用&lt;c:set&gt;标签的应用</title>
</head>
<body>
<ul>
<li>定义request范围内的变量username</li>
<br>
<c:set var="username" value="明日科技" scope="request"/>
<c:out value="username的值为:${username}"/>
<li>设置UserInfo对象的name属性</li>
<jsp:useBean class="com.wgh.UserInfo" id="userInfo"/>
<c:set target="${userInfo}" property="name">wgh</c:set>
<br>
<c:out value="UserInfo的name属性值为:${userInfo.name}"></c:out>
</ul>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>14.2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

<c:remove>标签

主要用来从指定的JSP范围内移除指定的变量。 

1.<c:remove var=”变量名” [scope=”page|request|session|application”]/>

View Code
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>应用&lt;c:remove&gt;标签移除变量</title>
</head>
<body>
<ul>
<li>定义request范围内的变量username</li>
<br>
<c:set var="username" value="明日科技" scope="request"/>
username的值为:<c:out value="${username}"/>
<li>移除request范围内的变量username</li>
<br>
<c:remove var="username" scope="request"/>
username的值为:<c:out value="${username}" default="空"/>
</ul>
</body>
</html>

 

<c:catch>标签,

用来处理JSP页面中产生的异常,并将异常信息存储。

1.<c:catch var=”name1”>容易产生异常的代码</c:catch>

View Code
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>应用&lt;c:catch&gt;标签捕获异常信息</title>
</head>
<body>
<c:catch var="error">
    <jsp:useBean class="com.wgh.UserInfo" id="userInfo"/>
    <c:set target="${userInfo}" property="pwd">111</c:set>
</c:catch>
<c:out value="${error}"/>
</body>
</html>

 

 

URL相关标签:

<c:import>标签,

该标签可以把其他静态或动态文件包含到本JSP页面。 同<jsp:include>的区别为:只能包含同一个web应用中的文件。而<c:import>可以包含其他web应用中的文件,甚至是网络上的资源。 1.<c:import url=”url” [context=”context”][ value=”value”] [scope=”page|request|session|application”] [charEncoding=”encoding”]>标签体</c:import> 2.<c:import url=”url” varReader=”name” [context=”context”][charEncoding=”encoding”]>

<c:redirect>标签

该标签用来实现了请求的重定向。同时可以在url中加入指定的参数。例如:对用户输入的用户名和密码进行验证,如果验证不成功重定向到登录页面;或者实现web应用不同模块之间的衔接。 1.<c:redirect url=”url” [context=”context”]> 2.<c:redirect url=”url”[context=”context”]>  <c:param name=”name1” value=”value1”> (传递参数)   </c:redirect>  

<c:url>标签

该标签用于动态生成一个String类型的URL,可以同<c:redirect>标签共同使用,也可以使用html的<a>标签实现超链接。 1.<c:url value=”value” [var=”name”][scope=”page|request|session|application”] [context=”context”]/> 2.<c:url value=”value” [var=”name”][scope=”page|request|session|application”] [context=”context”]>          <c:param name=”参数名” value=”值”>  (传递参数)   </c:url>

 

 

流程控制标签:

<c:if>标签

同程序中的if作用相同,用来实现条件控制。

1.<c:if test=”条件1” var=”name” [scope=”page|request|session|application”]/> 

2.<c:if test=”条件2” var=”name”[scope=”page|request|session|application”]>标签体</c:if>

View Code
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>根据是否登录显示不同的内容</title>
</head>
<body>
<c:if var="result" test="${empty param.username}">
  <form name="form1" method="post" action="">
    用户名:
      <input name="username" type="text" id="username">
    <br>
    <br>
    <input type="submit" name="Submit" value="登录">
  </form>
</c:if>
<c:if test="${!result}">
    [${param.username }] 欢迎访问我公司网站!
</c:if>
</body>
</html>

 

 <c:choose>、<c:when>和<c:otherwise>标签,
这3个标签通常情况下是一起使用的,
<c:choose>标签作为<c:when>和<c:otherwise>标签的父标签来使用。
1.
<c:choose>
         <c:when>
…..//业务逻辑1
         <c:when>
…..//业务逻辑2
         <c:otherwise>
….//业务逻辑3
</c:choose>

c:choose
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>根据是否登录显示不同的内容</title>
</head>
<body>
<c:choose>
    <c:when test="${empty param.username}">
      <form name="form1" method="post" action="">
        用户名:
          <input name="username" type="text" id="username">
        <br>
        <br>
        <input type="submit" name="Submit" value="登录">
      </form>
    </c:when>
    <c:otherwise>
        [${param.username }] 欢迎访问我公司网站!
    </c:otherwise>
</c:choose>
</body>
</html>
c:when
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>实现分时问候</title>
</head>
<body>
<!-- 获取小时并保存到变量中 -->
<c:set var="hours">
    <%=new java.util.Date().getHours()%>
</c:set>
<!-- 获取分钟并保存到变量中-->
<c:set var="second">
    <%=new java.util.Date().getMinutes()%>
</c:set>
<c:choose>
    <c:when test="${hours>1 && hours<6}">早上好!</c:when>
    <c:when test="${hours>6 && hours<11}" >上午好!</c:when>
    <c:when test="${hours>11 && hours<17}">下午好!</c:when>
    <c:when test="${hours>17 && hours<24}">晚上好!</c:when>
</c:choose>
 现在时间是:${hours}:${second}
</body>
</html>
c:otherwise
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>幸运大抽奖</title>
</head>
<body>
<%Random rnd=new Random();%>
<!-- 将抽取的幸运数字保存到变量中 -->
<c:set var="luck">
    <%=rnd.nextInt(10)%>
</c:set>
<c:choose>
    <c:when test="${luck==6}">恭喜你,中了一等奖!</c:when>
    <c:when test="${luck==7}" >恭喜你,中了二等奖!</c:when>
    <c:when test="${luck==8}">恭喜你,中了三等奖!</c:when>
    <c:otherwise>谢谢您的参与!</c:otherwise>
</c:choose>
</body>
</html>

 

 

循环标签:

 <c:forEach>标签,
该标签根据循环条件遍历集合(Collection)中的元素。
1.
<c:forEach var=”name” items=”Collection” varStatus=”StatusName” begin=”begin” end=”end” step=”step”>本体内容</c:forEach>
2.
(1)var设定变量名用于存储从集合中取出元素。
(2)items指定要遍历的集合。
(3)varStatus设定变量名,该变量用于存放集合中元素的信息.

1
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>遍历List集合</title>
</head>
<body>
<%
List<String> list=new ArrayList<String>();    //创建List集合的对象
list.add("简单是可靠的先决条件");                //向List集合中添加元素
list.add("兴趣是最好的老师");
list.add("知识上的投资总能得到最好的回报");
request.setAttribute("list",list);            //将List集合保存到request对象中
%>
<b>遍历List集合的全部元素:</b><br>

<c:forEach items="${requestScope.list}" var="keyword" varStatus="id">
    ${id.index }&nbsp;${keyword}<br>
</c:forEach>
<b>遍历List集合中第1个元素以后的元素(不包括第1个元素):</b><br>
<c:forEach items="${requestScope.list}" var="keyword" varStatus="id" begin="1">
    ${id.index }&nbsp;${keyword}<br>
</c:forEach>

</body>
</html>
2
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>应用&lt;c:forEach&gt;列举10以内全部奇数</title>
</head>
<body>

<b>10以内的全部奇数为:</b>
<c:forEach var="i" begin="1" end="10" step="2">
    ${i}&nbsp;&nbsp;
</c:forEach>
</body>
</html>

 

<c:forTokens>,
该标签用于浏览字符串,并根据指定的分隔字符将字符串截取。
1.<c:forTokens items=”strigOfTokens” delims=””delimiters [var=”name” begin=”begin” end=”end” step=”len” varStatus=”statusName”] >
(1)items指定被迭代的字符串。
(2)delims指定使用的分隔符。

View Code
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>应用&lt;c:forTokens&gt;分隔字符串</title>
</head>
<body>
<c:set var="sourceStr" value="Java Web:程序开发范例宝典、典型模块大全;Java:实例完全自学手册、典型模块大全"/>
<b>原字符串:</b>${sourceStr}
<br><b>分割后的字符串:</b><br>
<c:forTokens items="${sourceStr}" delims=":、;" var="item">
    ${item}<br>
</c:forTokens>
</body>
</html>

 

  

 

============================================================================================

 推荐参考: 

http://www.blogjava.net/maverick1003/articles/236575.html 

http://blog.csdn.net/bubei/article/details/2108766 

转载于:https://www.cnblogs.com/wzmmao/archive/2012/08/14/2638688.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值