国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JSP > 正文

使用自定義標(biāo)簽實(shí)現(xiàn)JSP頁(yè)面和代碼的分離

2024-09-05 00:19:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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>

為了將這個(gè)這個(gè)test.jsp改成自定義標(biāo)簽方法,我們分別使用簡(jiǎn)單標(biāo)簽和內(nèi)容標(biāo)簽兩種不同的方式實(shí)現(xiàn)。

1. 簡(jiǎn)單標(biāo)簽

由于我們需要輸出兩個(gè)內(nèi)容(日期和文件名),因此我們?yōu)闃?biāo)簽創(chuàng)建一個(gè)參數(shù)。具體代碼:

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;
  }
}

接下來(lái),我們創(chuàng)建標(biāo)簽文件 mytaglib.tld。標(biāo)簽文件其實(shí)只是一個(gè)xml格式的說(shuō)明文件,內(nèi)容也很簡(jiǎn)單。

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>

在這個(gè)標(biāo)簽文件中,我們將我們創(chuàng)建的標(biāo)簽取名 demo,并聲明了類(lèi)型和參數(shù)(parameter)。將該文件保存在 /web-inf 下面。
當(dāng)然,我們還需要將我們自定義的標(biāo)簽添加到 web.xml 中,否則還是無(wú)法使用。

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>

你可能在別處看到過(guò)類(lèi)似的聲明,只是沒(méi)有外面的 jsp-config,但是我們使用的是dtd 2.4,如果不加,eclipse 會(huì)提示出錯(cuò)。

到此為止,我們的自定義標(biāo)簽算是創(chuàng)建完畢。接下來(lái),我們可以開(kāi)始改寫(xiě)那個(gè)jsp文件來(lái)分離代碼了。

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>

上面這些想必你已經(jīng)很熟悉,我就不做多說(shuō)了。

2. 內(nèi)容標(biāo)簽

創(chuàng)建過(guò)程和上面大抵相同,只是程序文件和配置內(nèi)容有些差異。

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;
  }
}

我們將新的標(biāo)簽 demotag2 加入到上面的標(biāo)簽文件中。

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 文件無(wú)需修改。

看看同時(shí)使用兩種標(biāo)簽的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>

至此,兩種標(biāo)簽方式都完成。
本文并沒(méi)有就相關(guān)技術(shù)細(xì)節(jié)做出說(shuō)明,建議您看看sun有關(guān)jsp自定義標(biāo)簽的官方文檔。

無(wú)論是用自定義標(biāo)簽,還是使用javabean,都沒(méi)有太大的區(qū)別,各人或者團(tuán)隊(duì)可以根據(jù)自己的習(xí)慣使用。如果需要在獨(dú)立類(lèi)庫(kù)中封裝一些供頁(yè)面使用的單元,自定義標(biāo)簽應(yīng)該更適合些。不過(guò)現(xiàn)在的ide環(huán)境(myeclipse)在編寫(xiě)自定義標(biāo)簽的時(shí)候可能有些不太舒服的情況,界面開(kāi)發(fā)人員使用javabean方式可能更方便些,免得莫名其妙的提示干擾您的工作。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 承德县| 巴东县| 马龙县| 光山县| 嫩江县| 铜鼓县| 莱阳市| 江永县| 曲阳县| 卫辉市| 林周县| 宁波市| 渝中区| 左权县| 清水河县| 广水市| 察隅县| 江永县| 宁化县| 临清市| 柏乡县| 河池市| 大田县| 贵州省| 九寨沟县| 平湖市| 冷水江市| 祁门县| 蓝田县| 深泽县| 闽侯县| 永新县| 靖边县| 佛坪县| 仁化县| 崇礼县| 三门县| 三门县| 苍南县| 海门市| 遂宁市|