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

    <?xml version="1.0" encoding="UTF-8" ?>
        version="2.0">
        <!-- description用来添加对taglib(标签库)的描述 -->
        <description>zfx的自定义标签库</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就能找到对应的cn.springmvc.controller.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>
    -----------------------------------------------------------------------------------------------
    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 {
            if(userName.equals("John")){
                System.out.println("1231");
                return SKIP_BODY;  //不继续执行了。
            }
            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);
            }
            System.out.println(SKIP_BODY);
            System.out.println(EVAL_PAGE);
            return EVAL_PAGE; //继续执行的意思
        }
        @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;
        }
     
    }
     
    --------------------------------------------------------------------------------------------
    <%@ page language="java" pageEncoding="UTF-8"%>
    <!-- 使用taglib指令引用gacl标签库,标签库的前缀(prefix)可以随便设置,如这里设置成 prefix="xdp" -->
    <%@taglib uri="/gacl"  prefix="xdp"%>
    <!DOCTYPE HTML>
    <html>
      <head>
        <title>输出客户端的IP</title>
      </head>
      <body>
        你的IP地址是(使用java代码获取输出):
        <%
            //在jsp页面中使用java代码获取客户端IP地址
            String ip = request.getRemoteAddr();
            out.write(ip);
        %>
        <hr/>
         你的IP地址是(使用自定义标签获取输出):
         <%--使用自定义标签viewIP --%>
         <xdp:viewIP userName="John1231" >添加</xdp:viewIP>
      </body>
    </html>
  • 相关阅读:
    JavaWeb--HttpSession案例
    codeforces B. Balls Game 解题报告
    hdu 1711 Number Sequence 解题报告
    codeforces B. Online Meeting 解题报告
    ZOJ 3706 Break Standard Weight 解题报告
    codeforces C. Magic Formulas 解题报告
    codeforces B. Sereja and Mirroring 解题报告
    zoj 1109 Language of FatMouse 解题报告
    hdu 1361.Parencodings 解题报告
    hdu 1004 Let the Balloon Rise 解题报告
  • 原文地址:https://www.cnblogs.com/zfxJava/p/5660385.html
Copyright © 2011-2022 走看看