taglib自定义标签的使用

构建结构:

访问入口:

处理层:重写两个方法:doStartTag和doEndTag

带参写法:

入口:

标签定义:

业务层处理:

<?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">
    <description><![CDATA["自定义标签"]]></description>
    <tlib-version>1.0</tlib-version><!-- 文件版本 -->
    <short-name>helloWord</short-name><!-- 库名 -->
    <!-- 定义该标签库的URI 必须添加但可以空-->
    <uri></uri><!-- 唯一标识符 -->
    <tag>
        <!-- 定义标签说明 -->
        <description><![CDATA["第一个自定义标签"]]></description>
        <!-- 定义标签名 -->
        <name>helloWorld</name>
        <!-- 定义标签处理类 -->
        <tag-class>com.layne.tag.HellowordTag</tag-class>
        <!-- 定义标签体为空 -->
        <body-content>JSP</body-content>
           <attribute>
               <description><![CDATA["属性align"]]></description>
               <name>align</name><!-- 属性 -->
               <required>false</required><!--jsp页面是否必须传值-->
               <rtexprvalue>true</rtexprvalue><!--表示是否接受jsp语法或者el语言或其他动态语言,默认false-->
           </attribute>
           <attribute>
               <description><![CDATA["属性color"]]></description>
               <name>color</name>
               <required>false</required>
               <rtexprvalue>true</rtexprvalue>
           </attribute>
           <attribute>
               <description><![CDATA["属性bordercolor"]]></description>
               <name>bordercolor</name>
               <required>false</required>
               <rtexprvalue>true</rtexprvalue>
           </attribute>
           <attribute>
               <name>margin</name>
               <required>false</required>
               <rtexprvalue>true</rtexprvalue>
           </attribute>
           <attribute>
               <name>borderWidth</name>
               <required>false</required>
               <rtexprvalue>true</rtexprvalue>
           </attribute>
           <attribute>
               <name>title</name>
               <required>true</required>
               <rtexprvalue>true</rtexprvalue>
           </attribute>
           <attribute>
               <name>titleColor</name>
               <required>false</required>
               <rtexprvalue>true</rtexprvalue>
           </attribute>
           <attribute>
               <name>titleAlign</name>
               <required>false</required>
               <rtexprvalue>true</rtexprvalue>
           </attribute>    
       </tag>
</taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="h" uri="/WEB-INF/tld/helloWorld.tld" %>
<html>
  <head>
    <title>index.jsp</title>
  </head>
  <body>
      <%--  <% Integer a = 10;%> --%>
      <h:helloWorld title="Alibaba"></h:helloWorld>
  </body>
</html>
package com.layne.tag;


import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class HellowordTag extends TagSupport {
    private static final long serialVersionUID = -3992117262827534196L;
    //这些字段用来控制外观
    String align;//对齐方式
    String title;//标题
    String titleColor;//标题前景色
    String titleAlign;//标题对齐方式
    String color;//方框背景色
    String borderColor;//边框颜色
    String margin;//边框与内容之间的像素
    String borderWidth;//边框宽度的像素值
    public void setPageContext(PageContext context){
        //以超类保存页面环境对象,下面的Tag()要用到,这很重要
        super.setPageContext(context);
        //设置属性的默认值
        align = "center";
        title = null;
        titleColor = "White";
        titleAlign = "left";
        color = "lightblue";
        borderColor = "black";
        margin ="20";
        borderWidth = "4";
    }
    /**此方法再遇到<h:helloWorld>标签时调用
     **/
  public int doStartTag() throws JspException{
      try{//从PageContext对象获取输出流并传给setPageContext()方法
          JspWriter out = pageContext.getOut();
          out.print("<div align='"+align+"'>"+"<table bgcolor='"+borderColor+"'"+"border='0' cellspacing='0'"+"cellpadding='"+borderWidth+"'>");
          if(title != null)
              out.print("<tr><td align='"+titleAlign+"'>"+"<font face='helvetica' size='50'"+"color='"+titleColor+"'><br>"+title+"</b></font></td><td>");
              out.print("<tr><table bgcolor='"+color+"'"+"border='0' cellspacing='0'"+"cellpadding='"+margin+"'><tr><td></td></tr></table></tr></td>");
      }catch(IOException e){
          throw new JspException(e.getMessage());
      }
      //返回值告诉Jsp类处理标签主题
      return EVAL_BODY_INCLUDE;
  }
   /**
    * 在遇到</h:helloWorld>
    */
  public int doEndTag()throws JspException{
      try{
          JspWriter out = pageContext.getOut();
          out.println("</table></div>");
      }catch(IOException e){
          throw new JspException(e.getMessage());
      }
      //返回值指示继续处理Jsp页面
      return EVAL_PAGE;
  }
    
    
    //以下方法用来设置各种属性值,是必不可少的,在jsp页面使用标签时,属性将转化为方法调用
    public String getAlign() {
        return align;
    }
    public void setAlign(String align) {
        this.align = align;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getTitleColor() {
        return titleColor;
    }
    public void setTitleColor(String titleColor) {
        this.titleColor = titleColor;
    }
    public String getTitleAlign() {
        return titleAlign;
    }
    public void setTitleAlign(String titleAlign) {
        this.titleAlign = titleAlign;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getBorderColor() {
        return borderColor;
    }
    public void setBorderColor(String borderColor) {
        this.borderColor = borderColor;
    }
    public String getMargin() {
        return margin;
    }
    public void setMargin(String margin) {
        this.margin = margin;
    }
    public String getBorderWidth() {
        return borderWidth;
    }
    public void setBorderWidth(String borderWidth) {
        this.borderWidth = borderWidth;
    }
    

    
    
}
原文地址:https://www.cnblogs.com/flytogalaxy/p/7686960.html