• jsp:tld标签


    z注意每个uri地址要保持统一

    1.创建MytagPrinta.java文件

    package cn.tag;
    
    import java.io.IOException;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspTagException;
    import javax.servlet.jsp.tagext.TagSupport;
    
    public class MytagPrinta extends TagSupport {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	
    	protected String value = null;
    	protected String type = "0";
    	
    	
    
    	
    	public String getValue() {
    		return value;
    	}
    
    
    	public void setValue(String value) {
    		this.value = value;
    	}
    
    
    	public String getType() {
    		return type;
    	}
    
    
    	public void setType(String type) {
    		this.type = type;
    	}
    
    
    
    	@Override
    	public int doStartTag() throws JspException {
    		// TODO Auto-generated method stub
    
    		try {
    			if(type.equals("0"))
    			{
    				pageContext.getOut().println("hello:" + value);
    			}else{
    				pageContext.getOut().print("hell:"+value+"<br>");
    			}
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			throw new JspTagException( e.getMessage() );
    		}	
    		
    		return SKIP_PAGE;
    	}
    	
    	
    	@Override
    	public int doEndTag() throws JspException {
    		// TODO Auto-generated method stub
    		return EVAL_PAGE;
    	}
    	
    
    	
    	
    	
    }
    

      

    2.配置web-info下的web.xml文件

    <jsp-config>
        <taglib>  
    		<taglib-uri>http://www.tag.com/mytag</taglib-uri>  
    		<taglib-location>/WEB-INF/mytlds/mytag.tld</taglib-location>  
    	 </taglib> 
    	 
     </jsp-config>
    

      3.web-info下的mytlds文件夹下创建 mytag.tld

    <?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">
    <description>mytag 带type和value属性的打印</description>
    <display-name>mytag 标签库</display-name>
    <tlib-version>1.0</tlib-version>
    <short-name>mytag</short-name>
    <uri>http://www.tag.com/mytag</uri>
    
    <tag>
    	<description>打印value和type</description>
    	<name>printa</name>
    	<tag-class>cn.tag.MytagPrinta</tag-class>
    	<body-content>empty</body-content>
    	<attribute>
    		<name>type</name>
    		<required>false</required>
    		<rtexprvalue>true</rtexprvalue>
    	</attribute>
    	<attribute>
    		<name>value</name>
    		<required>true</required>
    		<rtexprvalue>true</rtexprvalue>
    	</attribute>
    	
    </tag>
    
    </taglib>
    

      

    4.新建tag.jsp文件。调用

    <%@ page language="java" contentType="text/html; charset=utf-8"%>
    <%@ taglib prefix="mytag" uri="http://www.tag.com/mytag" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>带属性标签实例</title>
    </head>
    <body>
    
    <h3>调用带属性的print标签</h3>
    打印后换行: <mytag:printa value="print" type="1"/>
    打印后不换行:<mytag:printa value="printa" type="0"/>
    
    </body>
    </html>
    

      

  • 相关阅读:
    我的友情链接
    我的友情链接
    BuChain 介绍:视屏讲解
    2019年5月数据库流行度排行:老骥伏枥与青壮图强
    五一4天就背这些Python面试题了,Python面试题No12
    钱包:BUMO 小布口袋 APP 用户手册
    工具箱:BUMO 工具应用场景示例
    工具箱:BUMO 密钥对生成器用户手册
    开发指南:BUMO 智能合约 Java 开发指南
    钱包:BOMO 轻钱包用户手册
  • 原文地址:https://www.cnblogs.com/achengmu/p/8277847.html
Copyright © 2020-2023  润新知