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

    两种实现方法

    (1)创建一个标签处理器类实现sampleTag接口
    public class HelloSimpleTag implements SimpleTag



    HelloSimpleTag.java
    
    package cn.stud.wlc.tag;
    
    import java.io.IOException;
    
    import javax.servlet.jsp.JspContext;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.PageContext;
    import javax.servlet.jsp.tagext.JspFragment;
    import javax.servlet.jsp.tagext.JspTag;
    import javax.servlet.jsp.tagext.SimpleTag;
    
    public class HelloSimpleTag implements SimpleTag {

    //设置属性
    private String value;
    private String count;
    private PageContext pagecontext;

    //set方法是必须的 这些属性在tld文件里描述

    public void setValue(String value) {
            this.value = value;
        }
    
        public void setCount(String count) {
            this.count = count;
        }
    
        public void doTag() throws JspException, IOException {
            // TODO Auto-generated method stub  
            //执行 标签体逻辑 实际应该编写到该方法中
            //可以后去PageContext的八个成员变量
            System.out.println("value:"+value+"count:"+count);
            pagecontext.getOut().print("hello word");
    
        }
    
        public JspTag getParent() {
            System.out.println("getParent");
            return null;
        }
    
        public void setJspBody(JspFragment arg0) {
            // TODO Auto-generated method stub
            System.out.println("setJspBody");
        }
    
        public void setJspContext(JspContext arg0) {
            // TODO Auto-generated method stub 
            //jsp页面的PageContext对象传进来
            //jsp引擎调用
            this.pagecontext = (PageContext) arg0;
            System.out.println("setJspContext");
        }
    
        public void setParent(JspTag arg0) {
            // TODO Auto-generated method stub
            System.out.println("setParent");
        }
    
    }


    (2)继承simpleTagSupport

    package cn.stud.wlc.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.SimpleTagSupport;

    
    

    public class MaxTag extends SimpleTagSupport {
    private String num1;
    private String num2;

    
    

    public void setNum1(String num1) {
    this.num1 = num1;
    }

    
    

    public void setNum2(String num2) {
    this.num2 = num2;
    }

    //dotag之后的方法就不用挨个写了

    public void doTag() throws JspException, IOException {
    int a=0;
    int b=0;

    PageContext pageContext = (PageContext) getJspContext();
    JspWriter out =pageContext.getOut();
    try {
    a=Integer.parseInt(num1);
    b=Integer.parseInt(num2);
    out.print(a>b?a:b);

    } catch (Exception e) {
    out.print("输入格式不正确");
    }
    }

    
    


    }

     
    
    
    




    在WEB-INF lib下建立myTag.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_0.xsd" version="2.0"> <!-- 描述TLD文件 --> <description>there are custom tags of tag</description> <tlib-version>1.0</tlib-version> <!--建议在jsp页面中使用的标签前缀 --> <short-name>wlc</short-name> <!-- tld文件的ID --> <uri>http://www.wlc.com/myTag/core</uri> <!-- 描述自定义的HelloSimpleTag标签 --> <tag> <description>view ip of client</description> <!-- 标签的名 --> <name>Hello</name> <!-- 标签所在的全类名 --> <tag-class>cn.stud.wlc.tag.HelloSimpleTag</tag-class> <!-- 标签体的类型 --> <body-content>empty</body-content> <!-- 描述当前标签的属性值 --> <attribute> <name>value</name> <required>true</required> <!-- runtime expresSion value运行时表达式的值当前属性是否可以接受运行时表达式的值 --> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>count</name> <required>false</required> <!-- runtime expresSion value运行时表达式的值当前属性是否可以接受运行时表达式的值 --> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>


    在jsp中调用

    <!-- 导入标签库文件(自定义) -->
    <%@taglib uri="http://www.wlc.com/myTag/core" prefix="wlc" %>

      具体的使用

    <wlc:Hello value="name" count ="10"/>

    ctrl+shift+o去除多余的import

  • 相关阅读:
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    AcWing 1049. 大盗阿福 状态机模型dp
    AcWing 11. 背包问题求方案数
    AcWing 10. 有依赖的背包问题
    AcWing 7. 混合背包问题
    AcWing 532. 货币系统
    2018-2019 ACM-ICPC, Asia East Continent Final L Eventual … Journey
    AcWing 487. 金明的预算方案
  • 原文地址:https://www.cnblogs.com/wlc297984368/p/5430587.html
Copyright © 2011-2022 走看看