Java Web 胡说八道 之- JSP标准标签库

Java Web 胡言乱语 之--- JSP标准标签库

JSTL 主要的标签分类:

 1,核心标签库   c:

 2,  SQL标签库  sql

 3,  XML标签库  xml

 4,  函数标签库  fn

 5,I18N格式标签库 fmt

 

1,web.xml 中配置:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  <jsp-config>
  	<taglib>
	  <taglib-uri>http://www.mldn.cn/jstl/core</taglib-uri>
	  <taglib-location>/WEB-INF/c.tld</taglib-location>
	</taglib>
	<taglib>
	  <taglib-uri>http://www.mldn.cn/jstl/fmt</taglib-uri>
	  <taglib-location>/WEB-INF/fmt.tld</taglib-location>
	</taglib>
	<taglib>
	  <taglib-uri>http://www.mldn.cn/jstl/fn</taglib-uri>
	  <taglib-location>/WEB-INF/fn.tld</taglib-location>
	</taglib>
	<taglib>
	  <taglib-uri>http://www.mldn.cn/jstl/core</taglib-uri>
	  <taglib-location>/WEB-INF/c.tld</taglib-location>
	</taglib>
	<taglib>
	  <taglib-uri>http://www.mldn.cn/jstl/sql</taglib-uri>
	  <taglib-location>/WEB-INF/sql.tld</taglib-location>
	</taglib><taglib>
	  <taglib-uri>http://www.mldn.cn/jstl/x</taglib-uri>
	  <taglib-location>/WEB-INF/x.tld</taglib-location>
	</taglib>
  </jsp-config>

 </web-app>

 

一,核心标签库

1, 基本标签        <c:out>      输出属性内容

                        <c:set>     设置属性内容

                        <c:remove>  删除指定属性

                        <c:catch>     异常处理

 

 

2,流程控制标签     <c:if>        条件判断

                          <c:choose>   多条件判断 ,可以设置<c:when>和<c:otherwise>标签

 

 

3,迭代标签           <c:forEach>       输出数组,集合

                          <c:forTokens>    字符串拆分及输出操作

 

4,包含标签             <c:import>  将一个指定的路径包含到当前页进行显示

 

5,生成URL标签       <c:url>     根据路径和参数生成一个新的RUL

 

6,客户端跳转          <c:redirect>     客户端跳转

 

1.1   <c:out >  例子

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
  <%
    pageContext.setAttribute("info","<www.baidu.com>");
  %>
<h3>属性存在:  <c:out value="${info}"/></h3>
<h3>属性不存在:<c:out value="${ref}" default="没有此内容!"/></h3>
<h3>属性不存在:<c:out value="${ref}">没有此内容!</c:out></h3>
</body>
</html>

 1.2  <c:set>例子

 

 

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
  <c:set var="info" value="hello mldn!" scope="request"/>
<h3>属性内容:${info}</h3>
</body>
</html>

 1.3,  <c:remove var="属性名称" [scope="[page|request|session|application]"] />

 

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
  <c:set var="info" value="hello mldn!" scope="request"/>
  <c:remove var="info" scope="request"/>
<h3>属性内容:${info}</h3>
</body>
</html>
 

1.4,  <c:catch>  处理异常操作,

 

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
 <c:catch var="errmsg">
     <%
	    int result = 10 /  0;
	 %>
 </c:catch>
<h3>异常信息:${errmsg}</h3>
</body>
</html>

 1.6,  <c:if> 判断标签

 <c:if test="判断条件"  var="存储判断结构" [scope="[page|request|session|application]"] />

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
<%
  pageContext.setAttribute("num",10);
%>
<c:choose>
  <c:when test="${num == 10}">
     <h3>num属性的内容是 10!</h3>
  </c:when>
  <c:when test="${num == 20}">
     <h3>num属性的内容是 20!</h3>
  </c:when>
  <c:otherwise>
     <h3>没有一个条件满足!</h3>
  </c:otherwise>
</c:choose>
</body>
</html>

 1.7,  <c:forEach>标签的主要功能为循环控制,可以将集合中的成员进行迭代输出,功能与Iterator接口类似,语法如下:

<c:forEach [var="每一个对象的属性名称"] items="集合" [varStatus="保存相关成员信息"] [begin="集合的开始输出位置"] [end="集合的结束输出位置"] [step="每次增长的步长"] >

具体的操作代码

</c:forEach>

 

实例:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
<%
   String info[] = {"hubvdo","3433333","www.baidu.com"};
   pageContext.setAttribute("ref",info);
%>
<h3>输出全部:
<c:forEach items="${ref}" var="mem">
  ${mem},
</c:forEach> </h3>
<h3>输出全部(间隔为2):
<c:forEach items="${ref}" var="mem" step="2">
  ${mem},
</c:forEach> </h3>
<h3>输出前两个:
<c:forEach items="${ref}" var="mem" begin="0" end="1">
  ${mem},
</c:forEach> </h3>
</body>
</html>

 map循环:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
<%
  Map map = new HashMap();
  map.put("zhangsan","www.baidu.com");
  map.put("lisi","www.llcuckcat.com");
  map.put("zhouss","这是一个测试程序");
  pageContext.setAttribute("ref",map);
%>
<c:forEach items="${ref}" var="mem">
   <h3>${mem.key}  --> ${mem.value}</h3>
</c:forEach>
</body>
</html>
 

3,<c:forTokens> :更像是String类中的split()方法和循环输出的一种集合,标签的语法如下:

<c:forTokens items="输出的字符串" delims="字符串分隔符" [var="存放每一个字符串变量"] [varStatus="存放当前对象的相关信息"]

[begin="输出位置"] [end="结束位置"] [step="输出间隔"]>

标签体内容

</c:forTokens>

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
<%
      String info="www.BAIDU.com";
      pageContext.setAttribute("ref",info);
%>
<h3>拆分结果为:
   <c:forTokens item="${ref}" delims="." var="con">
     ${con},
   </c:forTokens>
</h3>
<h3>拆分结果为:
   <c:forTokens item="Hi:Zhou:Lei" delims=":" var="con">
     ${con},
   </c:forTokens>
</h3>
</body>
</html>

  1.9, <c:import>标签:包含的时候只能包含文字,图片类的基本无法包含。

 

      <c:redirect>标签,

      <c:redirect url="跳转的地址" context="上下文路径" >

          <c:param name="参数名称" value="参数内容"/>

      </c:redirect>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

国际化标签:

1,  国际化标签      <fmt:setLocale>      设置一个全局的地区代码

                          <fmt:requestEncoding>    设置统一的请求编码

 

 

2,  信息显示标签     <fmt:bundle>    设置临时的腰读取资源文件的名称

                             <fmt:message>   通过key取得value,通过<fmt:param>向动态文本中设置内容

 

3,  数字及日期格式化      <fmt:formatNumber>       格式化数字

                                   <fmt:parseNumber>        反格式化数字

                                   <fmt:formatDate>  格式化日期,将日期变为字符串

                                   <fmt:parseDage>   反格式化日期,将字符串变为日期

                                   <fmt:setTimeZone>   设置一个全局的时区

                                    <fmt:timeZone>      设置一个临时的时区

 

 

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="fmt" uri="http://www.mldn.cn/jstl/fmt"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
<%
  pageContext.setAttribute("date",new Date());
%>
<h3>中文日期显示为:
    <fmt:setLocale value="zh_CN"/>
	<fmt:formatDate value="${date}"/>
</h3>
<h3>英文日期显示为:
    <fmt:setLocale value="en_US"/>
	<fmt:formatDate value="${date}"/>
</h3>
</body>
</html>
 

2.1  <fmt:requestEncoding value="GBK"/>

      在所有国际化程序中最重要的部分就是资源文件的读取,所有的资源文件就是*.properties 文件,文件应该

保存在 classpath之中,所以直接在 /WEB-INF/class 中建立即可.

 

2.2   数字格式化

<fmt:formatNumber>标签的属性:

1,                 value               要格式化的数字

2,               type               指定格式化的形式,例如:数字,货币,百分比,默认是数字

3,             pattern             要格式化数字的格式

4,              currencyCode     货币编号(ISO 4217编码),例如:人民币(CNY),  美元(USD)

5,              currencySymbol    显示的货币符号,例如:Y或$

6,            groupingUsed       是否在数字中加","

7,              maxIntegerDigits  可以显示的最大整数位

8,              minIntegerDigits   可以显示的最小整数位

9,              maxFractionDigits  可以显示的最大小数位

10 ,           minFractionDigits   可以显示的最小小数位

11,                var                     保存已格式化完的数字的属性名称

12,               scope                 var变量的保存范围,默认是page范围

 

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="fmt" uri="http://www.mldn.cn/jstl/fmt"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
<fmt:formatNumber value="351874.4587458" maxIntegerDigits="7" 
maxFractionDigits="3" groupingUsed="true" var="num"/>
<h3>格式化数字: ${num}</h3>
<fmt:formatNumber value="351874.4587458" pattern="##.###E0" var="num"/>
<h3>科学计数法: ${num}</h3>
</body>
</html>
 
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="fmt" uri="http://www.mldn.cn/jstl/fmt"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
<%
  pageContext.setAttribute("dateref",new Date());
%>
<fmt:formatDate value="${dateref}" type="both" dateStyle="default" 
timeStyle="default" var="date"/>
<h3>default显示日期时间:${date}</h3>
<fmt:formatDate value="${dateref}" type="both" dateStyle="short" 
timeStyle="short" var="date"/>
<h3>default显示日期时间:${date}</h3>
<fmt:formatDate value="${dateref}" type="both" pattern="yyyy年MM月dd日 HH时mm分ss秒SSS毫秒" var="date"/>
<h3>自定义显示日期时间:   ${date}</h3>
</body>
</html>
 

设置时区:<fmt:setTimeZone>

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

sql标签库: <sql:query ..>   执行查询命令的, 

                  <sql:setDataSource  dateSource="jdbc/zzz"

 

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

XML标签库: 

1,核心操作                        <x:out>   输出XPath指定的内容

                                       <x:parse  var=""   doc="">

 

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

 

函数标签库:相当于String中的函数

 

1,             ${fn:contains()}                                 查询某字符串是否存在,区分大小写

2              ${fn:containsIgnoreCase()}               查询某字符串是否存在,忽略大小写

3,             ${fn:startsWith()}                              判断是否以指定的字符串开头

4,             ${fn:endsWiths()}                             判断是否以指定的字符串结尾

5,             ${fn:toUpperCase()}                         全部转为大写显示

6,             ${fn:toLowerCase()}                         全部转为小写显示

7,             ${fn:substring()}                               字符串截取

8,             ${fn:split()}                                       字符串拆分

9,             ${fn:join()}                                        字符串连接

10,           ${fn:escapeXml()}                             将<,>,",'等转换成转义字符

11,           ${fn:trim()}                                        去掉左右空格

12,           ${fn:replace()}                                   字符串替换操作

13,           ${fn:indexOf()}                                  查找指定的字符串位置

14,           ${fn:substringBefore()}                    截取指定字符串之前的内容

15,           ${fn:substringAfter()}                      截取指定字符串之后的内容