自定义标签

Step1:建立web工程

Step2:将\MyEclipse\Common\plugins\com.genuitec.eclipse.j2eedt.core_10.0.0.me201110301321\data\libraryset\JSTL1.1\lib文件加下的两个 两个jar包(jstl.jar standard.jar)放到相应web工程的WebRoot/WEB-INF-lib文件夹下

 

Step3:项目右键,导入刚才的jar

Step4:右键项目refrash,找到Referenced Libraries/standard.jsr/META-INF/c.tld文件双击打开

Step5:将刚才打开的c.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>JSTL 1.1 core library</description>
  <display-name>JSTL core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>c</short-name>
  <uri>http://java.sun.com/jsp/jstl/core</uri>

  <validator>
    <description>
        Provides core validation features for JSTL tags.
    </description>
    <validator-class>
        org.apache.taglibs.standard.tlv.JstlCoreTLV
    </validator-class>
  </validator>

  <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>
</taglib>

 Step6:在项目WebRoot/WEB-INF下右键创建文件mydatetag.tld。编辑mydate.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">
    
  <display-name>JSTL core</display-name>
  <!-- 版本信息 -->
  <tlib-version>1.0</tlib-version>
  <!-- 自定义的标签命名前缀 -->
  <short-name>d</short-name>
  <!-- 自定义标签的命名空间 -->
  <uri>www.pursuit.com.cn/mydatetag</uri>

  <tag>
  	<!-- 标签名 -->
<name>date</name>
<!—自定义标签对应的标签类,处理业务逻辑(包名.类名)
    <tag-class>tag.MyDateTag</tag-class>
    <!-- 标签体可包含内容
    		empty(没有标签体)  scriptless(有标签体但是标签体内容不能出现java代码)
			Jsp:有标签体并且标签体的内容可以使java代码(只有复杂标签技术才支持,简单标签技术不支持) -->
    <body-content>empty</body-content>
    <attribute>
    	<!-- 标签参数(可以有多个) -->
        <name>pattern</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
</taglib>

 Step8:根据定义的标签中设置的标签类(包名、类名都一致)创建标签类,标签类需要继承SimpleTagSupport接口,并重写doTag()方法

 

 

 

package tag;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class MyDateTag extends SimpleTagSupport{
	//定义标签的属性,需要与mydate.tld中定义的属性名称及类型一致
	private String pattern;
	//设置属性set方法
	public void setPattern(String pattern){
		this.pattern = pattern;
	}
	//重写doTag()方法,实现业务逻辑
	public void doTag() throws JspException,IOException{
		//获取容器上下文,通过上下文或得jsp隐藏属性out
		PageContext ptx = (PageContext)getJspContext();
		JspWriter out = ptx.getOut();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		//将时间格式化并输出
		out.write(sdf.format(date));
	}
}

 Step9:写一个test.jsp页面验证自定义标签

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <!-- 引入标签 -->
<%@ taglib uri="www.pursuit.com.cn/mydatetag" prefix="d" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 使用自定以标签  d 标签前缀  date标签名 -->
	<d:date pattern="yyyy-MM--dd" />
</body>
</html>

 

 

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