• JSP自定义标签的使用简化版


    在jsp中 如果不想页面中出现java代码 这个时候就需要使用到jsp的自定义标签库技术了

    自定义标签库 能够有效的减少jsp中java代码的出现 使其更加自然的像html页面一样

    如果要使用jsp自定义标签库技术顺着下列步骤做个demo就能理解个大概

    首先在项目下WEB-INF下创建一个tld文件 用于说明这个自定义标签库

    然后打开自己曾经导入过的jar包,并且保证该包在现行环境下能够使用

    找到包中的WEB-INF文件可以看到一个c.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>JSTL 1.2 core library</description>
      <display-name>JSTL core</display-name>
      <tlib-version>1.2</tlib-version>
      <short-name>c</short-name>
      <uri>http://java.sun.com/jsp/jstl/core</uri>
    </taglib>

    这样的内容 从头选到uri的位置截取过来 然后在尾部加上</taglib>

    申明 由于各个机子上环境可能不一致 此文件不建议直接从我这里copy 

    然后可以看一个实例标签

    <tag>
        <description>
            Catches any Throwable that occurs in its body and optionally
            exposes it.
        </description>
        <name>catch</name>
        <tag-class>org.apache.taglibs.standard.tag.common.core.CatchTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <description>
    Name of the exported scoped variable for the
    exception thrown from a nested action. The type of the
    scoped variable is the type of the exception thrown.
            </description>
            <name>var</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
      </tag>

    这是一个完整的标签中可用的分析

    <tag>
         <!-- 标签描述-->
        <description></description>
         <!-- 标签名称-->
        <name>catch</name>
        <!-- -指向的类 也就是具体执行操作的类->
        <tag-class>org.apache.taglibs.standard.tag.common.core.CatchTag</tag-class>
        <!-- 是否具有标签的格式-->
        <body-content>emty</body-content>
      </tag>

    下面是我一个demo Tag.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>there are my tags of tag</description>  
        <tlib-version>1.2</tlib-version>  
      
          <!-- 标签的短名称 -->
        <short-name>Tag</short-name>  
        
        <!-- 绑定到这个uri上 在页面上要使用 -->
        <uri>http://www.cnblogs.com</uri> 
        
           
        <tag>
            <description>view ip of client</description>  
            <name>ViewIP</name>  
            <tag-class>web.tag.ViewIpTag</tag-class>  
            <body-content>empty</body-content>  
         </tag>  
      
    </taglib>  

    然后是类中的代码

    package web.tag;
    
    import java.io.IOException;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.TagSupport;
    
    public class ViewIpTag extends TagSupport {
    
        @Override
        public int doStartTag() throws JspException {
            
            HttpServletRequest request =(HttpServletRequest) this.pageContext.getRequest();
            
            JspWriter out=this.pageContext.getOut();
            
            String ip = request.getRemoteAddr();
            
            try {
                out.write(ip);
            } catch (IOException e){
                throw new RuntimeException(e);
            }
            
            
            return super.doStartTag();
        }
    
    }

    最后是在jsp页面的使用

    <%@ taglib uri="http://www.cnblogs.com" prefix="Tag" %>
     你的ip是:<Tag:ViewIP/>

    加入这两行代码 然后访问所在页面 就能看的本机Ip

    恐惧源于无知,代码改变世界
  • 相关阅读:
    hpuoj--校赛--2015年的第一场雪(暴力)
    hpuoj--校赛--考试来了(水题)
    hpuoj--校赛--特殊的比赛日期(素数判断+模拟)
    bean copy
    spring boot+jaspersoft实现复杂报表
    spring boot 启动时运行代码(2)ApplicationListener
    Alibaba Java Coding Guidelines 安装(eclipse)并简单使用
    spring boot 系统启动时运行代码(1)-@PostConstruct
    java泛型&bean copy list
    jgroups-raft
  • 原文地址:https://www.cnblogs.com/ad-zhou/p/9032182.html
Copyright © 2020-2023  润新知