心得11--jsp自訂標籤案例分析__js

來源:互聯網
上載者:User

1、示範傳統自訂標籤(jsp2.0以前的)

使用自訂標籤控制頁面內容(標籤體)是否輸出,利用doStartTag()的傳回值控制

return this.SKIP_BODY; //忽略標籤體

return this.EVAL_BODY_INCLUDE; //執列標籤體

控制整個jsp的輸出利用doEndTag()的傳回值控制

return this.SKIP_PAGE;  //跳過頁面標籤後餘下的jsp代碼

return this.EVAL_PAGE; //繼續執行餘下jsp代碼

自訂標籤實現內容(標籤體)迴圈輸出利用Tag子介面Iteration中定義的doAfterBody()和傳回值EVAL_BODY_AGAIN,SKIP_BODY實現

先覆蓋doStartTag()方法,返回EVAL_BODY_INCLUDE

覆蓋doAfterBody()

public int doAfterBody() throws JspException {times++;int result = this.EVAL_BODY_AGAIN;if(times>4){result = this.SKIP_BODY;}return result;}自訂標籤修改內容(標籤體)EVAL_BODY_BUFFERED;標籤處理類:繼承BodyTagSupport覆蓋doStartTag(),並返回EVAL_BODY_BUFFERED;覆蓋doEndTag()public int doEndTag() throws JspException {BodyContent bc = this.getBodyContent();String c = bc.getString();c = c.toUpperCase();JspWriter out = this.pageContext.getOut();try {out.write(c);} catch (IOException e) {throw new RuntimeException(e);}return this.EVAL_PAGE;}2、需求:實現一個自訂標籤功能:判斷一個YYYY-MM-DD格式的日期修改為下面格式輸出年:YYYY月:MM日:DD  分析:自訂標籤處理類(Demo5.java)package com.csdn.web.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.BodyContent;import javax.servlet.jsp.tagext.BodyTagSupport;import javax.servlet.jsp.tagext.Tag;public class Demo5 extends BodyTagSupport {@Overridepublic int doEndTag() throws JspException {BodyContent bc = this.getBodyContent();String b = bc.getString();String[] data = b.split("-");JspWriter out = this.pageContext.getOut();try {out.println("年:"+data[0]+"<br>");out.println("月:"+data[1]+"<br>");out.println("日:"+data[2]+"<br>");} catch (IOException e) {e.printStackTrace();}return Tag.EVAL_PAGE;}}要轉換的顯示時間6.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/csdn" prefix="csdn"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>轉換日期格式並輸出</title>  </head>    <body>    <csdn:demo5>2012-11-17</csdn:demo5>  </body></html>對應的標籤模版csdn.tld<?xml version="1.0" encoding="UTF-8" ?><taglib 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"version="2.0"><tlib-version>1.0</tlib-version><short-name>csdn</short-name><uri>/csdn</uri><tag><name>demo5</name><tag-class>com.csdn.web.tag.Demo5</tag-class><body-content>JSP</body-content></tag></taglib>


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.