首页 > 开发 > JSP > 正文

使用自定义标签实现JSP页面和代码的分离

2020-02-05 13:59:34
字体:
来源:转载
供稿:网友

test.jsp
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
  <head>
    <title>my jsp 'test.jsp' starting page</title>
  </head>
 
  <body>
    this is my jsp page. <br>
    date : <%= new java.util.date().tostring() %> <br>
    file : <input value="<%= request.getservletpath() %>" />
  </body>
</html>

为了将这个这个test.jsp改成自定义标签方法,我们分别使用简单标签和内容标签两种不同的方式实现。

1. 简单标签

由于我们需要输出两个内容(日期和文件名),因此我们为标签创建一个参数。具体代码:

demotag.java
package com.mycompany;

import java.util.date;

import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class demotag extends tagsupport {
 
  public int dostarttag() throws jspexception {   
    try {
      httpservletrequest request = (httpservletrequest)pagecontext.getrequest();
      jspwriter out = pagecontext.getout();     
     
      if (parameter.comparetoignorecase("filename") == 0)
        out.print(request.getservletpath());
      else
        out.print(new date());
     
    } catch (java.io.ioexception e) {
      throw new jsptagexception(e.getmessage());
    }
   
    return skip_body;
  }
 
  private string parameter = "date";
 
  public void setparameter(string parameter) {
    this.parameter = parameter;
  }
 
  public string getparameter() {
    return parameter;
  }
}

接下来,我们创建标签文件 mytaglib.tld。标签文件其实只是一个xml格式的说明文件,内容也很简单。

mytaglib.tld
<?xml version="1.0" encoding="iso-8859-1"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>

<tag>
  <name>demo</name>
  <tag-class>com.mycompany.demotag</tag-class>
  <body-content>empty</body-content>
  <attribute>
    <name>parameter</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
</tag>

</taglib>

在这个标签文件中,我们将我们创建的标签取名 demo,并声明了类型和参数(parameter)。将该文件保存在 /web-inf 下面。
当然,我们还需要将我们自定义的标签添加到 web.xml 中,否则还是无法使用。

web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" version="2.4" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <jsp-config>
    <taglib>
      <taglib-uri>/web-inf/mytaglib</taglib-uri>
      <taglib-location>/web-inf/mytaglib.tld</taglib-location>
    </taglib>
  </jsp-config>

</web-app>

你可能在别处看到过类似的声明,只是没有外面的 jsp-config,但是我们使用的是dtd 2.4,如果不加,eclipse 会提示出错。

到此为止,我们的自定义标签算是创建完毕。接下来,我们可以开始改写那个jsp文件来分离代码了。

test.jsp
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<%@taglib uri="/web-inf/mytaglib" prefix="mytag"%>
<html>
  <head>
    <title>my jsp 'test.jsp' starting page</title>
  </head>
 
  <body>
    this is my jsp page. <br>
    date : <mytag:demo parameter="date" /><br>
    file : <mytag:demo parameter="filename" />
  </body>
</html>

上面这些想必你已经很熟悉,我就不做多说了。

2. 内容标签

创建过程和上面大抵相同,只是程序文件和配置内容有些差异。

demotag2.java
package com.mycompany;

import java.io.ioexception;
import java.util.date;

import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class demotag2 extends bodytagsupport {
 
  public int dostarttag() throws jsptagexception {   
    return eval_body_buffered;
  }
 
  public int doendtag() throws jsptagexception {
    string body = this.getbodycontent().getstring();
    httpservletrequest request = (httpservletrequest)pagecontext.getrequest();
   
    body = body.replace("$date", new date().tostring());
    body = body.replace("$filename", request.getservletpath());
   
    try {
      pagecontext.getout().print(body);
    }
    catch (ioexception e) {
      throw new jsptagexception(e.getmessage());
    }
   
    return skip_body;
  }
}

我们将新的标签 demotag2 加入到上面的标签文件中。

mytaglib.tld
<?xml version="1.0" encoding="iso-8859-1"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>

<tag>
  <name>demo</name>
  <tag-class>com.mycompany.demotag</tag-class>
  <body-content>empty</body-content>
  <attribute>
    <name>parameter</name>
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
</tag>

<tag>
  <name>demo2</name>
  <tag-class>com.mycompany.demotag2</tag-class>
  <body-content>jsp</body-content>
</tag>

</taglib>

web.xml 文件无需修改。

看看同时使用两种标签的test.jsp效果。

test.jsp
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<%@taglib uri="/web-inf/mytaglib" prefix="mytag"%>
<html>
  <head>
    <title>my jsp 'test.jsp' starting page</title>
  </head>
 
  <body>
    this is my jsp page. <br>
    date : <mytag:demo parameter="date" /><br>
    file : <mytag:demo parameter="filename" />

    <hr>

    <mytag:demo2>
    date: $date<br>
    file: $filename
    </mytag:demo2>
  </body>
</html>

至此,两种标签方式都完成。
本文并没有就相关技术细节做出说明,建议您看看sun有关jsp自定义标签的官方文档。

无论是用自定义标签,还是使用javabean,都没有太大的区别,各人或者团队可以根据自己的习惯使用。如果需要在独立类库中封装一些供页面使用的单元,自定义标签应该更适合些。不过现在的ide环境(myeclipse)在编写自定义标签的时候可能有些不太舒服的情况,界面开发人员使用javabean方式可能更方便些,免得莫名其妙的提示干扰您的工作。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表