zoukankan      html  css  js  c++  java
  • JSTL自定义标签

    当JSTL标签库已经无法满足我们的需求时候,就需要自己开发自定义标签,来满足我们的需求,自定义标签实际上是一个普通的java类,继承SimpleTagSupport类。

    做类。派生自SimpleTagSupport,重写doTag()方法。getJspBody(),getJspContext(),invoke(null);

    package com.itnba.maya.zidingyi;
    
    import java.io.IOException;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.*;
    
    public class Zidingyijstl extends SimpleTagSupport {
        private String a="p";    
    
        public void setA(String a) {
            this.a = a;
        }
        private int b;
        public void setB(int b){
            this.b=b;
        }
    
        public void doTag() throws JspException, IOException {    //重写doTage()方法
            JspFragment frag=this.getJspBody();
            this.getJspContext().getOut().write("<"+a+">");
            for(int i=0;i<b;i++){
            frag.invoke(null);
            }
            this.getJspContext().getOut().write("</"+a+">");
        
        }
    
    }

    在web项目的WEB-INF目录下建立tld文件(myjstl.tld),这个tld文件为标签库的声明文件,并配置好相应的信息。(可以参考核心标签库的tld文件)

    <?xml version="1.0" encoding="UTF-8"?>
    
    <taglib 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"
        version="2.1">
        
      <description>自定义的一些标签</description>
      <display-name>自定义标签</display-name>
      <tlib-version>1.0</tlib-version>
      <short-name>m</short-name>
      <uri>http://www.itnba.com/maya/myjstl</uri>
      
      <tag>
        <description>
              描述
        </description>
        <name>show</name>   <!-- 标签名 -->
        <tag-class>com.itnba.maya.zidingyi.Zidingyijstl</tag-class>  <!-- 唯一标识 -->
        <body-content>scriptless</body-content>   <!--  标签里不可写脚本<%%>-->
        
          <!--  添加属性-->
         <attribute>
            <description>
            这是自定义标签的描述信息,可以在MyEclipse中有提示
            </description>
            <name>a</name>          <!--  属性的名字,要跟方法名要对应的-->
            <required>false</required>   <!--是否必填-->
            <rtexprvalue>false</rtexprvalue>  <!--标签里可不可以用EL表达式-->
        </attribute>
        
        <!--再写一个方法-->
        <attribute>
            <description>
            这是自定义标签的描述信息,可以在MyEclipse中有提示.
            </description>
            <name>b</name>                 <!--  属性的名字,要跟方法名要对应的-->
            <required>true</required>     <!--是否必填-->
            <rtexprvalue>false</rtexprvalue>   <!--标签里可不可以用EL表达式-->
        </attribute>
      </tag>
      
    </taglib>

     jsp页面

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@ taglib prefix="m" uri="http://www.itnba.com/maya/myjstl" %>    <%--配置 --%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <m:show b="2" a="h2">
    haha
    </m:show>
    </body>
    </html>

    显示

  • 相关阅读:
    角点检测
    25岁董事长给大学生的18条忠告
    10大忠告
    实验常用正交表
    面试必备15题
    专家系统
    深圳租房完全攻略
    你应选什么样的职业?
    托福报名详细过程解说
    Managed Direct3D开发经验浅析
  • 原文地址:https://www.cnblogs.com/hq233/p/6391912.html
Copyright © 2011-2022 走看看