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

    <?xml version="1.0" encoding="UTF-8" ?>
     
        version="2.0">
        <!-- description用来添加对taglib(标签库)的描述 -->
        <description>孤傲苍狼开发的自定义标签库</description>
        <!--taglib(标签库)的版本号 -->
        <tlib-version>1.0</tlib-version>
        <short-name>GaclTagLibrary</short-name>
        <!-- 
            为自定义标签库设置一个uri,uri以/开头,/后面的内容随便写,如这里的/gacl ,
            在Jsp页面中引用标签库时,需要通过uri找到标签库
            在Jsp页面中就要这样引入标签库:<%@taglib uri="/gacl" prefix="gacl"%>
        -->
        <uri>/gacl</uri>
        <!--一个taglib(标签库)中包含多个自定义标签,每一个自定义标签使用一个tag标记来描述  -->
        <!-- 一个tag标记对应一个自定义标签 -->
         <tag>
            <description>这个标签的作用是用来输出客户端的IP地址</description>
            <!-- 
                为标签处理器类配一个标签名,在Jsp页面中使用标签时是通过标签名来找到要调用的标签处理器类的
                通过viewIP就能找到对应的me.gacl.web.tag.ViewIPTag类
             -->
            <name>viewIP</name>
            <!-- 标签对应的处理器类-->
            <tag-class>cn.springmvc.controller.ViewIPTag</tag-class>
            <body-content>jsp</body-content><!-- empty 或者 -->
             <attribute> 
        <name>userName</name> 
        <required>true</required> 
        <rtexprvalue>true</rtexprvalue> 
      </attribute> 
        </tag>
     
    </taglib>
    <xdp:viewIP userName="John1231" >添加</xdp:viewIP>
     
    package cn.springmvc.controller;
    import java.io.IOException;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.PageContext;
    import javax.servlet.jsp.tagext.Tag;
    public class ViewIPTag implements Tag  {
         //接收传递进来的PageContext对象
        private PageContext pageContext;
        private String userName;
        @Override
        public int doEndTag() throws JspException {
            System.out.println("调用doEndTag()方法");
            return 0;
        }
        @Override
        public int doStartTag() throws JspException {
            System.out.println("调用doStartTag()方法");
            System.out.println(userName);
            HttpServletRequest request =(HttpServletRequest) pageContext.getRequest();
            JspWriter out = pageContext.getOut();
            String ip = request.getRemoteAddr();
            try {
                //这里输出的时候会抛出IOException异常
                out.write(ip);
                out.write("<h1>呵呵</h1>");
            } catch (IOException e) {
                //捕获IOException异常后继续抛出
                throw new RuntimeException(e);
            }
            return 0 ; //继续执行的意思
        }
        @Override
        public Tag getParent() {
            return null;
        }
        @Override
        public void release() {
            System.out.println("调用release()方法");
        }
        @Override
        public void setPageContext(PageContext pageContext) {
            System.out.println("setPageContext(PageContext pageContext)");
            this.pageContext = pageContext;
        }
        @Override
        public void setParent(Tag arg0) {
     
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
     
     
     
    }
  • 相关阅读:
    php多态
    ssl certificate problem: self signed certificate in certificate chain
    test plugin
    open specific port on ubuntu
    junit vs testng
    jersey rest service
    toast master
    use curl to test java webservice
    update folder access
    elk
  • 原文地址:https://www.cnblogs.com/zfxJava/p/5660365.html
Copyright © 2011-2022 走看看