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

原文地址:https://www.cnblogs.com/ad-zhou/p/9032182.html

时间: 2024-08-29 07:38:52

JSP自定义标签的使用简化版的相关文章

JSP自定义标签渲染时报Illegal to flush错误

javax.servlet.ServletException:Illegal to flush within a custom tag 此错误是因为在JSP页面中的某一个自定义标签的doStartTag或doEndTag中用了out.flush,而其他的自定义标签没有导致.可以去掉out.flush. JSP自定义标签渲染时报Illegal to flush错误,布布扣,bubuko.com

JSP 自定义标签 生命周期

1. 2. JSP 自定义标签 生命周期,布布扣,bubuko.com

JSP自定义标签开发入门《转》

JSP自定义标签开发入门 一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; 首先我们需要大致了解开发自定义标签所涉及到的接口与类的层次结构(其中SimpleTag接口与SimpleTagSupport类是JSP2.0中新引入的). 目标1:自定义一个用表格显示用户信息的简单标签 效果图: 在jsp页面使用此自定义标签: 假设我们有一个UserInfo的javabean,那

整理一份jsp自定义标签以及权限控制标签

jsp自定义标签使用场景因地制宜,可以实现自定义的标签输出功能也可以实现权限的管理 1:先定义标签类 1-1:页面输出标签 package com.suyin.web.jspsectag; import java.io.IOException; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.Tag; public cla

一个简单的jsp自定义标签

学到了一个简单的jsp自定义标签,后面有更多的例子,会更新出来: 例子1: 步骤: 1.编写标签实现类: 继承javax.servlet.jsp.tagext.SimpleTagSupport; 重写doTag,实现在网页上输出: 2.在web-inf目录或其子目录下,建立helloword.tld文件,即自定义标签的说明文件 注意:标签处理类必须放在包中,不能是裸体类:不需要修改web.xml: //tld: tag lib description 标签库描述 java代码: package

JSP自定义标签配置

JSP自定义标签 <taglib> <taglib-uri>/WEB-INF/you.tld</taglib-uri> <taglib-location>/WEB-INF/you.tld</taglib-location> </taglib> 由这个配置可知,JSP自定义标签配置文件不是放在lib目录下,也不是放在classes文件夹中,更不是放在WebRoot目录下,而是放在WEB-INF目录下

JSP自定义标签开发入门

一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; 首先我们需要大致了解开发自定义标签所涉及到的接口与类的层次结构(其中SimpleTag接口与SimpleTagSupport类是JSP2.0中新引入的). 目标1:自定义一个用表格显示用户信息的简单标签 效果图: 在jsp页面使用此自定义标签: 假设我们有一个UserInfo的javabean,那么在JSP页面使用此标签只

基于JSP 自定义标签使用实例介绍

添加JSP自定义标签: 先添加一个tld文件到WEB-INF文件夹中<?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://

jsp自定义标签tag EL函数

原文:jsp自定义标签tagEL函数 源代码下载地址:http://www.zuidaima.com/share/1550463459052544.htm 简单易懂的自定义EL函数 tag.java </pre><p> </p><pre name="code" class="java">package com.zuidaima.tag; import java.io.IOException; import javax.