2015-09-23 JSTL標籤(二)

來源:互聯網
上載者:User

標籤:

2015-09-23  JSTL標籤(二)一:Fmt標籤:formatDate在jsp頁面中使用fmt標籤:<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>1>fmt標籤庫用來格式化輸出的格式化日期的標籤是:<fmt:formatDate value="xxx" pattern="xxx"/>其中value指定一個Date類型的變數;pattern用來指定輸出的日期模板,例如:yyyy-MM-dd HH:mm:ss<%Date date = new Date();request.setAttribute("date",date);%><p>正常輸出目前時間的格式:</p>${date}<p>使用fmt格式化後的時間格式輸出:</p><fmt:formatDate value="${date}" pattern="yyyy-MM-dd HH:mm:ss"/>正常輸出目前時間的格式:Wed Sep 23 21:09:37 CST 2015使用fmt格式化後的時間格式輸出:2015-09-23 21:09:37fmt標籤庫還提供了格式化數位標籤,如下:<fmt:formatNumber value=”xxx” pattern=”xxx”/>標籤中value:指定數字類型的變數或常量;pattern:指定格式化數位類型,還四捨五入   例如:0.00,#.## 使用0和#是有區別的<p>使用fmt格式化輸出數字:</p><fmt:formatNumber value="3.1415926" pattern="0.00"/>使用fmt格式化輸出數字:3.14二:自訂標籤傳統標籤實現tag介面簡單標籤實現SimpleTag介面自訂標籤用於封裝java代碼自訂標籤是動作標籤,每一個標籤就是一個對象自訂標籤的功能在對應類的方法中實現建立自訂標籤的步驟:1:定義一個標籤處理類
2:建立一個tld檔案,該檔案本身是一個xml檔案
3:在頁面中使用<%@ taglib%>指令來指定tld檔案的位置。傳統標籤類tag介面:Tag是經典標記處理常式的介面,定義了標記處理常式與 JSP 頁面實作類別之間的基本協議,也定義了生命週期以及在開始和結束標記時調用的方法。但是相對於其他標籤處理介面來說比較複雜。簡單標籤類SimpleTag介面:因此我們定義的標籤處理類不會去實現Tag介面,而是去實現SimpleTag介面,該介面的父介面是JspTag,它和Tag

是兄弟關係

 實際中建立自訂標籤時,我們extends SimpleTagSupport

SimpleTag介面的常用方法:void doTag():每次執列標籤時都會調用這個方法;JspTag getParent():返回當前標籤的父標籤(很少使用);void setParent(JspTag parent):設定父標籤;void setJspBody(JspFragment jspBody):設定標籤體;void setJspContext(JspContext pc):設定jsp內容物件,其中JspContext類的子類是PageContext(獲得內建對象和範圍)setParent(JspTag parent)、setJspBody(JspFragment jspBody)、setJspContext(JspContext pc)方法是由

Tomcat服務器調用以上三個方法執行後,doTag()方法才會被Tomcat伺服器調用。

建立tld檔案在WEB-INF目錄下:不能建立在lib和class子目錄下,建立一個子目錄tld儲存JSP要求2.0以上版本預設建立好後修改Schema路徑--複製http://java.sun.com/xml/ns/j2ee到後面的檔案路徑上前加”/”empty:表示標籤中沒有標籤體;JSP:jsp2.0已經不在支援這個類型了,它表示標籤體內容可以是:java指令碼,標籤,也可以是el運算式;scriptless:表示標籤體內容可以是EL運算式也可以是其他標籤,包括動態標籤和HTML標籤;tagdependent:表示標籤體內容不會被執行,而是直接賦值給標籤處理類。1.標籤處理類:package com.cissst.simpleTag;import java.io.IOException;import java.io.Writer;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class MyTag extends SimpleTagSupport {@Overridepublic void doTag() throws JspException, IOException {Writer writer = this.getJspContext().getOut();writer.write("<br/>執列標籤體之前...<br/>");this.getJspBody().invoke(writer);writer.write("<br/>執列標籤體之後...<br/>");}}2.tld檔案:<?xml version="1.0" encoding="UTF-8"?><taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version> <short-name>MyTag</short-name> <uri>http://www.mariahcat.com/MyTag</uri>  <tag> <name>MyTag</name> <tag-class>com.cissst.simpleTag.MyTag</tag-class> <body-content>scriptless</body-content> </tag></taglib>3.JSP檔案:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="http://www.mariahcat.com/MyTag" prefix="aoao" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP ‘SimpleTag.jsp‘ starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    <p>使用自訂標籤:</p>    <%    request.setAttribute("name", "dodo");     %>    <aoao:MyTag>${requestScope.name}</aoao:MyTag>  </body></html>使用自訂標籤:執列標籤體之前...dodo執列標籤體之後...可以自訂一個帶有屬性的標籤,具體步驟如下:1.首先在標籤處理類中添加屬性package com.cissst.simpleTag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class MyTag2 extends SimpleTagSupport {private int counts;public void setCounts(int counts) {this.counts = counts;}@Overridepublic void doTag() throws JspException, IOException {for (int i = 0; i < counts; i++) {getJspBody().invoke(null);}}}2.然後在tld檔案中添加對屬性的配置<?xml version="1.0" encoding="UTF-8"?><taglib version="2.1" 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-jsptaglibrary_2_1.xsd"> <tlib-version>1.0</tlib-version> <short-name>MyTag2</short-name> <uri>http://www.mariahcat.com/MyTag2</uri>  <tag> <name>MyTag2</name> <tag-class>com.cissst.simpleTag.MyTag2</tag-class> <body-content>scriptless</body-content>  <attribute> <name>counts</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag></taglib>3.JSP檔案<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib uri="http://www.mariahcat.com/MyTag2" prefix="aoao" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP ‘SimpleTag.jsp‘ starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    <p>使用帶屬性的自訂標籤:</p>    <%    request.setAttribute("name", "dodo");     %>    <aoao:MyTag2 counts="10">${requestScope.name}<br/></aoao:MyTag2>  </body></html>使用帶屬性的自訂標籤:dododododododododododododododododododododoTag()拋出SkipPageException自訂標籤可以控制頁面中當前自訂標籤後的內容是否執行 

該異常是跳過頁面異常,當伺服器調用doTag()方法時,捕獲了SkipPageException異常,那麼頁面中的以下內容就

不再執行

2015-09-23 JSTL標籤(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.