JSPタグ開発--詳細

14712 ワード

ラベル開発のいくつかの大きなステップ:
1、ラベル類を開発し、TagSupport類を継承し、
package org.lxh.tagdemo;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag extends TagSupport{
	public int doStartTag()throws JspException{
	 JspWriter out =super.pageContext.getOut();
	 try{
         out.println("<h1>hello World!!</h1>");
	 }catch(Exception e){}
     return TagSupport.SKIP_BODY;
	}
};

注意点:コンパイル中にエラーが発生する可能性があります.tomcatパスの下、jsp-apiに注意してください.JArのパスの問題、このフォルダをjava環境の下に置いて、jdk/jre/lib/extパッケージの下で、
2、ラベルの記述ファイルを定義し、ラベルの記述ファイルの接尾辞「*.tld」を定義し、このファイルもXML構文規則に合致する.
<?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_1.xsd"
    version="2.1">
<tlib-version>1.0<tlib-version>  <!-- -->
<short-name>firsttag</short-name> <!-- TLD -->
<tag>
   <name>hello</name>
   <tag-class>org.lxh.tagdemo.HelloTag</tag-class>
   <body-content>empty</body-content>
</tag>
</taglib>

3,JSPファイルでこのラベルを使用します.
 
4、ラベルコンポーネント
 
 4.1>.ラベル処理クラス:HelloTag.java;
 4.2>.ラベル記述ファイル:hellotag.tlc
 4.3>.JSPページ:<%@taglib%>でラベルを定義する.
4.4>(オプション)web.xmlファイルにマッピング名を設定.
 
********************************************************************************
1、属性付きラベルの定義
例:カスタム日付フォーマット
package org.lxh.tagdemo;
import java.text.*;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class DateTag extends TagSupport{
    private String format;  // setter 
	public int doStartTag()throws JspException{
	 SimpleDateFormat sdf = new SimpleDateFormat(this.format);
	 // 
	 try{
	 	super.pageContext.getOut().write(sdf.format(new Date()));
	 }catch (Exception e){
        e.printStackTrace();
	 }
	 
	 return TagSupport.SKIP_BODY;
	}
    public void setFormat(String format){
	  this.format = format;
	}
	public String getFormat(){
	  return this.format;
	}
};
 
2、編集*.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_1.xsd"
    version="2.1">
<tlib-version>1.0<tlib-version>  <!-- -->
<short-name>datetag</short-name> <!-- TLD -->
<tag>
   <name>date</name>
   <tag-class>org.lxh.tagdemo.DateTag</tag-class>
   <body-content>empty</body-content>
   <attribute>    <!--   -->
          <name>format</name>
	  <required>true</required>
	  <rtexprvalue>true</rtexprvalue><!--   -->
   </attribute>
</tag>
</taglib>
 
3,使いやすいように、そのまま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>mldn_date</taglib-uri>
	  <taglib-location>/WEB-INF/datetag.tld</taglib-location>
	</taglib>
  </jsp-config>

 </web-app>

 ************************************************************************************
 
TagSupportクラスの簡単な紹介:
1, doStartTag ()
2, int doAfterBody () Default processing for a body.

SKIP_BODY

static final int SKIP_BODY :

表の前提内容が無視する、実行権をdoEndTag()メソッドに渡すことを示す.

EVAL_BODY_INCLUDE

static final int EVAL_BODY_INCLUDE

繰り返し実行ラベル体を表す内容は、doAfterBody()メソッドでdoAfterBody()メソッドがSKIP_を返すまで繰り返し実行されます.BODYまで.
 

doEndTag

int doEndTag
()
             throws JspException
static int SKIP_PAGE
 
JSPページはすぐに実行を停止し、すべての出力をすぐにブラウザに戻すべきであることを示します.
  static int EVAL_PAGE
 
JSPが正常に動作することを示します.
 
1.1作成*.JAvaファイル:
package org.lxh.tagdemo;
import java.text.*;
import java.util.*;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class AttributeTag extends TagSupport{
   private String name;
   private String scope;

   public int doStartTag() throws JspException{
      // 
	  Object value=null;
	  if("page".equals(this.scope)){  // page 
	     value = super.pageContext.getAttribute(this.name,PageContext.PAGE_SCOPE);
	  }
	  if("request".equals(this.scope)){  // page 
	     value = super.pageContext.getAttribute(this.name,PageContext.REQUEST_SCOPE);
	  }
	  if("session".equals(this.scope)){  // page 
	     value = super.pageContext.getAttribute(this.name,PageContext.SESSION_SCOPE);
	  }
	  if("application".equals(this.scope)){  // page 
	     value = super.pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE);
	  }
	  if(value == null){
	    return TagSupport.SKIP_BODY;  // 
	  } else {
	    return TagSupport.EVAL_BODY_INCLUDE ;  // 
	  }
   }
   public void setName(String name){
     this.name  = name;
   }
   public void setScope(String scope){
     this.scope  =scope;
   }
   public String getName(){
     return this.name;
   }
   public String getScope(){
     return this.scope;
   }
};
 
1.2,作成*.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_1.xsd"
    version="2.1">
<tlib-version>1.0</tlib-version>  
<short-name>mldntag</short-name> 
<tag>
   <name>present</name>
   <tag-class>org.lxh.tagdemo.AttributeTag</tag-class>
   <body-content>JSP</body-content>
   <attribute>
      <name>name</name>
      <required>true</required>
      <rtexprvalue>true<rtexprvalue>
   </attribute>
   <attribute>
      <name>scope</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
   </attribute>
</tag>
</taglib>

1.3,WEB-INF/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>mldn_date</taglib-uri>
	  <taglib-location>/WEB-INF/datetag.tld</taglib-location>
	</taglib>
	<taglib>
	  <taglib-uri>mldn_page</taglib-uri>
	  <taglib-location>/WEB-INF/mldntag.tld</taglib-location>
	</taglib>
  </jsp-config>

 </web-app>

1.4ページの書き込み開始
 
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="mytag" uri="mldn_page"%>
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
</head>
<body>
<h1>
  <%
    String scope = "session";
	session.setAttribute("username","zhangsan");
  %>
 <mytag:present name="username" scope="<%=scope%>">
 <h2><%=scope%> , :"${sessionScope.username}"</h2>
 </mytag:present>
 <mytag:present name="allusers" scope="request">
 <h2><%=scope%> , :"${requestScope.allusers}"</h2>
 </mytag:present>
</h1>
</body>
</html>

<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 
BodyTagSupport:TagSupportクラスのサブクラスであり、BodyTagSupportクラスを統合することで実現されるラベルは、ラベル体の内容の数を直接処理することができる
BodyTagSupportクラスの定義は次のとおりです.
 public class BodyTagSupport extends TagSupport implements BodyTag
 
ラベルの開発は実際の開発で使用されることは少なく、主にサードパーティが提供するプラグイン(重点)がある.
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
JSP 2.0以降はSimpleTagSupportクラスを提供し、中のdoTag()メソッドを直接上書きすれば完成します.