jsp自定义标签

            

  1. 自定义标签的作用:

     使用过EL表达式的人,都知道,其方便使用和容易理解的作用;其本质,就是封装了一些java代码,因为在jsp中,所以封装为一些标签,但是,在java后台,他就等同是一些封装类;

  2. 自定义标签的开发步骤:

    1.创建标签处理类

    2.创建库文件

    3.在jsp中引入标签

  3. demo如下:

    处理文件类

  4. import java.io.IOException;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.TagSupport;
    
    public class MassageTag extends TagSupport {
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -5275739086977767635L;
    	private String key="hello";
    
    	public String getKey() {
    		return key;
    	}
    
    	public void setKey(String key) {
    		this.key = key;
    	}
    	
    	@Override
    	public int doStartTag() throws JspException {
    		
    		 JspWriter jw = pageContext.getOut();
    		 try {
    			jw.print("<input type='text' value="+key+">");
    			
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		
    		return SKIP_BODY;
    	}
    	
    	@Override
    	public int doEndTag() throws JspException {
    		
    		return SKIP_BODY;
    	}
    	
    	
    	@Override
    	public void release() {
    		
    		super.release();
    	}
    
    }
  5. 标签库描述文件:
  6. <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE taglib
            PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
    
        "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
    
    <!-- a tag library descriptor -->
    
    <taglib>
    
      <tlibversion>1.0</tlibversion>
    
      <jspversion>1.1</jspversion>
    
      <shortname>mytaglib</shortname>
    
      <uri>/mytag</uri>
    
      <tag>
    
        <name>message</name>
    
        <tagclass>com.web.tag.MassageTag</tagclass>
    
        <bodycontent>empty</bodycontent>
    
        <info>Just Says Hello</info>
    
        <attribute>
    
        <name>key</name>
    
        <required>true</required>
    
        </attribute>
    
      </tag>
    
    </taglib>
  7. web.xml里配置引入标签
  8. <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 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-app_2_4.xsd">
    
      <display-name>DemoTag</display-name>
      
    <jsp-config>
      <taglib>
      <taglib-uri>/mytag</taglib-uri>
      <taglib-location>>/WEB-INF/mytaglib.tld</taglib-location>
      </taglib>
    </jsp-config>
      
    <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>
  9. <%@ page contentType="text/html; charset=UTF-8" %>
    <%@ taglib uri="/mytag" prefix="mytaglib" %>
    <html>
    <body>
    <h2>Hello World!</h2>
    <hr>
    <mytaglib:message key="你好"/>
    <hr>
    </body>
    </html>
  10. pom.xml文件
  11. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.web</groupId>
      <artifactId>DemoTag</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>DemoTag Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>servlet-api</artifactId>
    			<version>2.5</version>
    		</dependency>
      </dependencies>
     <build>
    		<plugins>
    			<plugin>
    				<groupId>org.eclipse.jetty</groupId>
    				<artifactId>jetty-maven-plugin</artifactId>
    				<version>9.2.2.v20140723</version>
    			</plugin>
    		</plugins>
    	</build>
    </project>
  12. 测试:
  13. <%@ page contentType="text/html; charset=UTF-8" %>
    <%@ taglib uri="/mytag" prefix="mytaglib" %>
    <html>
    <body>
    <h2>Hello World!</h2>
    <hr>
    <mytaglib:message key="你好"/>
    <hr>
    </body>
    </html>

                    大神的范例:http://www.cnblogs.com/xdp-gacl/p/3916734.html(很厉害的样子哦)        

        

       


你可能感兴趣的:(jsp自定义标签)