发布于 2015-08-17 14:53:40 | 218 次阅读 | 评论: 0 | 来源: 网络整理

自定义标签是用户自定义的JSP语言元素。当包含一个自定义标签的JSP页面转换成servlet,标签转化为操作称为标记处理程序的对象上。 Web容器然后执行JSP页面的servlet调用时的这些操作。

JSP标签扩展可以创建直接插入到JSP页面,只是新的标签,在前面章节了解了内置的标签。 JSP 2.0规范中引入的简单标记处理程序编写这些自定义标记。

要编写自定义标签可以继承SimpleTagSupport类并重写的doTag()方法,在那里你可以用代码来生成的标签内容。

创建"Hello" 标签:

考虑你要定义一个名为<ex:Hello>一个自定义标签,想用它在以下主体:


<ex:Hello />

要创建自定义的JSP标签,必须首先创建一个标记处理程序的Java类。所以,让我们创建HelloTag类,如下所示:


package com.yiibai;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends SimpleTagSupport {

  public void doTag() throws JspException, IOException {
    JspWriter out = getJspContext().getOut();
    out.println("Hello Custom Tag!");
  }
}

上面的代码中有简单的编码,其中当doTag()方法获取当前JspContext对象并使用getJspContext()方法,并用它来发送“Hello Custom Tag!”到当前的JspWriter对象。 

让我们来编译上面的类,并复制它在环境变量CLASSPATH中可用的目录。最后创建如下的标签库文件:<Tomcat-Installation-Directory>webappsROOTWEB-INFcustom.tld.


<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Example TLD</short-name>
  <tag>
    <name>Hello</name>
    <tag-class>com.yiibai.HelloTag</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

现在是时候使用上面定义的自定义标记Hello 在我们的JSP程序如下:


<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
  <head>
    <title>A sample custom tag</title>
  </head>
  <body>
    <ex:Hello/>
  </body>
</html>

试着调用上面的JSP,这应该产生以下结果:


Hello Custom Tag!

访问标签体:

您可以包含在标签体,已经看到了标准的标签信息。考虑要定义一个名为<ex:Hello>一个自定义标签,想用它在以下方式的主体:


<ex:Hello>
   This is message body
</ex:Hello>

让我们做如下更改在我们上面的标签代码来处理标签体:


package com.yiibai;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends SimpleTagSupport {

   StringWriter sw = new StringWriter();
   public void doTag()
      throws JspException, IOException
    {
       getJspBody().invoke(sw);
       getJspContext().getOut().println(sw.toString());
    }

}

在这种情况下,从调用而产生的输出首先被捕获到一个StringWriter中被写入到与标记相关联的之前的JspWriter。现在,因此我们需要改变TLD文件中,如下所示:


<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Example TLD with Body</short-name>
  <tag>
    <name>Hello</name>
    <tag-class>com.yiibai.HelloTag</tag-class>
    <body-content>scriptless</body-content>
  </tag>
</taglib>

现在,让我们把上面用适当的主体如下标签:


<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
  <head>
    <title>A sample custom tag</title>
  </head>
  <body>
    <ex:Hello>
        This is message body
    </ex:Hello>
  </body>
</html>

这将产生以下结果:


This is message body

自定义标签的属性:

可以使用各种属性以及自定义标签。要接受一个属性值,自定义标签类需要实现setter方法​​,相同的JavaBean的setter方法​​,如下所示:


package com.yiibai;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends SimpleTagSupport {

   private String message;

   public void setMessage(String msg) {
      this.message = msg;
   }

   StringWriter sw = new StringWriter();

   public void doTag()
      throws JspException, IOException
    {
       if (message != null) {
          /* Use message from attribute */
          JspWriter out = getJspContext().getOut();
          out.println( message );
       }
       else {
          /* use message from the body */
          getJspBody().invoke(sw);
          getJspContext().getOut().println(sw.toString());
       }
   }

}

属性的名称是“message”,所以setter方法​​是的setMessage()。现在使用<attribute>元素,如下所示在TLD文件中添加此属性:


<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Example TLD with Body</short-name>
  <tag>
    <name>Hello</name>
    <tag-class>com.yiibai.HelloTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
       <name>message</name>
    </attribute>
  </tag>
</taglib>

现在,让我们尝试下面的JSP和message 属性,如下所示:


<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
  <head>
    <title>A sample custom tag</title>
  </head>
  <body>
    <ex:Hello message="This is custom tag" />
  </body>
</html>

这将产生以下结果:


This is custom tag

上面的例子是值得大家注意的,可以包含一个属性以下属性:

属性 目的
name The name element defines the name of an attribute. Each attribute name must be unique for a particular tag.
required This specifies if this attribute is required or optional. It would be false for optional.
rtexprvalue Declares if a runtime expression value for a tag attribute is valid
type Defines the Java class-type of this attribute. By default it is assumed as String
description Informational description can be provided.
fragment Declares if this attribute value should be treated as a JspFragment.

以下是指定相关的属性示例:


.....
    <attribute>
      <name>attribute_name</name>
      <required>false</required>
      <type>java.util.Date</type>
      <fragment>false</fragment>
    </attribute>
.....

如果使用的是两个属性,那么可以修改TLD如下:


.....
    <attribute>
      <name>attribute_name1</name>
      <required>false</required>
      <type>java.util.Boolean</type>
      <fragment>false</fragment>
    </attribute>
    <attribute>
      <name>attribute_name2</name>
      <required>true</required>
      <type>java.util.Date</type>
    </attribute>
.....
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务