>  기사  >  Java  >  java jsp自定义标签相关介绍

java jsp自定义标签相关介绍

巴扎黑
巴扎黑원래의
2017-08-10 15:35:001691검색


本文实例讲述了jsp自定义标签用法。分享给大家供大家参考。具体如下:

在JSP中有一种机制,可以让你在JSP页面中插入与HTML类似的标记。本文介绍JSP定制标记的基本概念和构成,以及如何开发和应用JSP定制标记。

什么是标记

使用HTML语言我们可以这样去编辑我们的网页:





1

2

3

4

5

6

7

8

9

10


4ccd502a0c3a39bb24a6a5fa4ae8fcaf

  8068fdf7283316ff5fa88f31dee90af9

d802f82940ba52ec23926064d97e980a

HELLO WORLD

3cb49b5d5252470b6e8d1ec6b9d156b1

d5562fc1905fb3f910bdc60b59d26aa8

ed06eca2ad1683e2c8a054ea8b6b2dda

HELLO WORLD

d615c100a4e2da10d5129c387c7aa8e5

d0337f35161d647b47dd0e6f32b02633

在这里我们把34934661d7147ca926b095899343bad0,08eb756f81b28a01d33212cce3fbe742,a64997a0904a094b4570728d7f327acd称为标记。HTML 标记( HTML Markup)是HTML文档的控制语言,用于指定浏览器显示和打印文档的方式.它是用小于号"73fba53e3e558034b52e782854a23525"括起来的短语和符号,如 d90fcdb535d6081491edebb07224a674、4f8ea7115e78f861c264d09f6f145eb7等。许多HTMl标记以成对的方式出现,如08eb756f81b28a01d33212cce3fbe742611f9639240b30f69873ac7e66a1829c、9c5594a5fc8d2e506f1a8147102c836b4f8ea7115e78f861c264d09f6f145eb7 等。在JSP中我们也可以定制自己的标记,以供JSP页面使用,如下例所示





1

2

3

4

5

6

7

8

9

10

11


08d01fa4beb841661b25073ff436975f

9a5828652840913ac0ff93ee05ba33d8

100db36a723c770d327fc0aef2ce13b1

 93f0f5c25f18dab9d176bd4f6de5d30e

b2386ffb911b14667cb8f0f91ea547a7login6e916e0f7d1e588d4f442bf645aedb2f

 9c3bca370b5104690d9ef395f2c5f8d1

 6c04bd5ca3fcae76e30b72ad730ca86d

  02111d55848cef2c593c26712d240c95

1297c44f078c851ada4c33aadd4f023f

 36cc49f0c466276486e50c850b7e4956

73a6ac4ed44ffec12cee46588e518a5e

在上例中1297c44f078c851ada4c33aadd4f023f就是一个JSP定制标记符。widtht、height是这个标记的属性。87ecc62b36bc7a2fa256d982f103a6a7是一个标记库定义指令,在稍后我们将会讨论。在JSP中定制标记符,实质上就是以标记的形式封装了一个俱有独立功能的Java类。标记的使用减少了直接嵌入JSP页面的Java代码,方便了页面的布局,并且有利于代码的复用,提高了开发的效率。

JSP服务器解析标记的过程

那么当一个标记被嵌入JSP页面后,JSP服务器是如何对这个标记进行解析的呢?各对象的含义如下所示:

Client: 表示客户端。
JSP-Server:JSP服务器。
JSP-Page:JSP页面。
TLD:  标记库描述文件,定义标记和标记的各种属性和处理文件等。
TagClass 标记处理程序

当一个用户访问一个JSP页面时,这个请求被发送到JSP服务器,JSP服务器会根据这个请求去调用相应的页面,如果这个页面中有自定义的标记, JSP服务就会根据页面指令4000e405c537c31a6e93758fead4c19d去访问TLD得到处理程序的相关信息,接着调用该处理程序的构造器方法,启动标记符处理程序,并读取标记符的属性和相应值。对每个没有设置属性的,调用相应的set方法。当标记符第一次使用时,它的任何属性都不会做过设置,因此对每个属性都调用set方法。属性设置完以后,JSP服务器调用处理程序的doStartTag(),然后再调用doEndTag()方法。最后JSP服务器会继续处理剩下的页面,在页面结尾调用release ()方法,清理占用的所有资源。

TLD文件

TLD(TLD:Tag Library Descriptor标记库描述符)文件,标准的XML格式的标记定义文件,被用来存放标记符的信息,下面就是一个典型的TLD文件。





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40


fd127017018238a3d6d80a9ea7e1d552

85b661b81cf20aeded894e730191a16c

3ad8860f6f0a8bbcc3b68830c3b9138a

226d215169f4554e91621aab2ce44651

7c4c04b2ef67488a672c7230cfd27f6d

9e6d4588659cb9b96ed990342d916614

b71ff2470058e38f42aeb68d7f8f59a71.0b135785468d79ccc462b66b7d503f5f9

 ebfa4989b4147167b649927a19028252

7c8b5cc564243ba3c2944f714d3c5bfa1.180633d133c25fed5de3be4109946aeeb

 54164e304a4fdb9822ef0db91d3a7fad

 f68533488e4c5ca803d1fbc5a509a94dtagclass261f1694a70437db5dfb2b0bf0c8c5a2

 9fe8bdad75314d0b2fd55edede146f3f

621992283f45bdf0f155f43ebcc5ac51

  f86954835b3272b49ffaa300fb8663a9login9389ccf0d2a4b61014d0eaaeab0d274b

  40190776e9a5aba846662c3591c6ff7c

7ce75750c35387329b4d753f8f1b8a90

tagclass.login.login

346853814db32cf3b20e101149d48ab2

7689b425a9871e0e72297bb5a503b22a

  036928f672e7412f3368c347c2eadfc8

   3c2645bb7262ddc715438f9cbce5ebe5

  51123d0f798f4e5ee4743ec4d383723c

8d1f8e4733e2c57cae22ece8a33dbe5b

e2846888034a9a08adf2790d6cd454aa

  f86954835b3272b49ffaa300fb8663a9height9389ccf0d2a4b61014d0eaaeab0d274b

    9f169fe09f1cfdaf9095c45a5c284307

  0f09e79e8f34068893bc7eca0df91cd5truea70ce4472e565885e0448f962909048d

    94225b5c6c62850748db6b8eb9f8585c

  6de405fd8c91079ac5f0c80d75c9e047truec9eb827845dc17b7ec56c517dbfa017a

7f668e72ff851cc3b1beb3ed18d4a24f

  46ffbaae78525dedbf1096f5bdd23f92

8d1f8e4733e2c57cae22ece8a33dbe5b

  f86954835b3272b49ffaa300fb8663a9width9389ccf0d2a4b61014d0eaaeab0d274b

  0f09e79e8f34068893bc7eca0df91cd5truea70ce4472e565885e0448f962909048d

  6de405fd8c91079ac5f0c80d75c9e047truec9eb827845dc17b7ec56c517dbfa017a

  46ffbaae78525dedbf1096f5bdd23f92

39455d112d87f777584ba089837ed8f1

996a3b5d1c976cabb1342b0c09a29ea7

在这个TLD文件中定义了只有一个标记符的标记符库,这个名为login的标记符会调用一个Applet以验证用户的合法性。处理这个标记的类就是 tagclass.login.login。width、height是这个标记的两个属性。属性是在使用标记符时作为参数发送的值。我们可以在上面的示例中增加几个标记,也可以为每个标记添加几个属性。我们开发标记符库时不一定非要从头开始,自己编写一个全新TLD。我们可以使用某个集成的开发的环境,也可以修改上面的例子。

TagLib指令

那么当JSP服务器在解析一个标记符时,它是如何定义一个标记库的呢?这就是TagLib指令的主要责任。

Taglib 指令

定义一个标记库以及其自定义标记的前缀.

JSP 语法


复制代码代码如下:

c5ed71a2155e0c907c9cc84ff0eb47e6


例子






1

2

3

4

5

6

7

8

9

10


9a5828652840913ac0ff93ee05ba33d8

100db36a723c770d327fc0aef2ce13b1

 93f0f5c25f18dab9d176bd4f6de5d30e

b2386ffb911b14667cb8f0f91ea547a7login6e916e0f7d1e588d4f442bf645aedb2f

 9c3bca370b5104690d9ef395f2c5f8d1

 6c04bd5ca3fcae76e30b72ad730ca86d

  02111d55848cef2c593c26712d240c95

1297c44f078c851ada4c33aadd4f023f

 36cc49f0c466276486e50c850b7e4956

73a6ac4ed44ffec12cee46588e518a5e

描述

3e0568b0b3822a2e41864553dc4f94b1指令声明此JSP文件使用了自定义的标记,同时引用标记库,
也指定了他们的标记的前缀。 你必须在使用自定义标记之前使用3e0568b0b3822a2e41864553dc4f94b1指令。

属性

uri="URIToTagLibrary" :Uniform Resource Identifier (URI)根据标记的前缀对自定义的标记进行唯一的命名,URI可以是一个相对或绝对的路径。 
  prefix="tagPrefix":在自定义标记之前的前缀。如上例中的1297c44f078c851ada4c33aadd4f023f

标记符的处理程序(Tag handle)

我们还是以一个例子来看下如何实现一个Tag handle。首先是看一下它的类图:
让我们再看一下它的代码:





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49


packagetagclass.login;

importjavax.servlet.jsp.tagext.TagSupport;

importjavax.servlet.jsp.*;

importjava.io.*;

publicclassloginextendsTagSupport

{

publiclogin()

{

super();

}

publicintdoStartTag()throwsJspTagException

{

JspWriter out = pageContext.getOut();

try

{

out.println("5f20c42eab9b434e8533424437597eec e56bf5afaa9eef10b2ceba52e1d96544");

}

catch(Exception e)

{

}

returnSKIP_BODY;

}

publiccintdoEndTag()throwsJsptagException

{

returnEVAL_PAGE;

}

publicvoidrelease()

{

super.release();

}

publicvoidsetWidth(String language)

{

this.width = width;

}

publicString getWidth()

{

returnthis.width;

}

publicvoidsetHeight(String height)

{

this.height=height;

}

publicString getHeight()

{

returnthis.height;

}

privateString width;

privateString height;

}

从以上我们可以看出,实现一个简单的标记符处理程序有几个要求:①增加一个类,使之继承 java.Servlet.jsp.tagext.TagSupport类。这个类提供了java.Servlet.jsp.tagext.Tag接口所要求的所有的方法。另外,还需要使用一些基本的API,使JSP容器能够调用我们自己提供的标记符处理程序。②必须为每个标记符属性分别创建一个 gete8aa4f7ce323535a9b01247c46fe6139和sete8aa4f7ce323535a9b01247c46fe6139方法,JSP容器需要使用这些方法处理程序传递参数。③要为标记符处理程序创建一个构造器和自毁器。JSP需要使用构造器启动处理程序。自毁器是在realease()方法中定义的。在处理程序的生命周期结束时,需要调用自毁器释放所占用的资源。④创建两个名为doStartTag()和doEndTag()的方法,执行具体的处理和输出动作。这两个方法是在处理自定义标记符的起始位置和结束位置调用的。它们的返回值是在Tag Interface里定义的静态int,这几个静态值分别是:

SKIP_BODY隐含0 :跳过了开始和结束标签之间的代码。
EVAL_BODY_INCLUDE隐含1:将body的内容输出到存在的输出流中
SKIP_PAGE隐含5 : 忽略剩下的页面。
EVAL_PAGE隐含6:继续执行下面的页

当然标记符也有它自己的缺点。很不方便的封装过程,有限的功能。对于一些不太复杂和功能单一的逻辑描述,需要传递的参数要求不高时,使用JSP标记,要方便的多。对于大多数的商业逻辑应用,还是使用bean要好的多,也宜于servlet控制。

附:文章中所用示例的完整代码

JSP代码:login.jsp





1

2

3

4

5

6

7

8

9

10


9a5828652840913ac0ff93ee05ba33d8

100db36a723c770d327fc0aef2ce13b1

93f0f5c25f18dab9d176bd4f6de5d30e

b2386ffb911b14667cb8f0f91ea547a76e916e0f7d1e588d4f442bf645aedb2f

9c3bca370b5104690d9ef395f2c5f8d1

6c04bd5ca3fcae76e30b72ad730ca86d

02111d55848cef2c593c26712d240c95

1297c44f078c851ada4c33aadd4f023f

36cc49f0c466276486e50c850b7e4956

73a6ac4ed44ffec12cee46588e518a5e

标记符描述库:taglib.tld





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27


fd127017018238a3d6d80a9ea7e1d552

1a7c346d3fb1fc33a60fb9451a5134ff

7c4c04b2ef67488a672c7230cfd27f6d

b71ff2470058e38f42aeb68d7f8f59a71.0b135785468d79ccc462b66b7d503f5f9

7c8b5cc564243ba3c2944f714d3c5bfa1.180633d133c25fed5de3be4109946aeeb

 f68533488e4c5ca803d1fbc5a509a94dtagclass261f1694a70437db5dfb2b0bf0c8c5a2

621992283f45bdf0f155f43ebcc5ac51

  f86954835b3272b49ffaa300fb8663a9login9389ccf0d2a4b61014d0eaaeab0d274b

7ce75750c35387329b4d753f8f1b8a90

tagclass.login.login

7689b425a9871e0e72297bb5a503b22a

  036928f672e7412f3368c347c2eadfc8

  51123d0f798f4e5ee4743ec4d383723c

8d1f8e4733e2c57cae22ece8a33dbe5b

  f86954835b3272b49ffaa300fb8663a9height9389ccf0d2a4b61014d0eaaeab0d274b

  0f09e79e8f34068893bc7eca0df91cd5truea70ce4472e565885e0448f962909048d

  6de405fd8c91079ac5f0c80d75c9e047truec9eb827845dc17b7ec56c517dbfa017a

  46ffbaae78525dedbf1096f5bdd23f92

8d1f8e4733e2c57cae22ece8a33dbe5b

  f86954835b3272b49ffaa300fb8663a9width9389ccf0d2a4b61014d0eaaeab0d274b

  0f09e79e8f34068893bc7eca0df91cd5truea70ce4472e565885e0448f962909048d

  6de405fd8c91079ac5f0c80d75c9e047truec9eb827845dc17b7ec56c517dbfa017a

  46ffbaae78525dedbf1096f5bdd23f92

39455d112d87f777584ba089837ed8f1

996a3b5d1c976cabb1342b0c09a29ea7

标记符处理程序:login.java





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49


packagetagclass.login;

importjavax.servlet.jsp.tagext.TagSupport;

importjavax.servlet.jsp.*;

importjava.io.*;

publicclassloginextendsTagSupport

{

publiclogin()

{

super();

}

publicintdoStartTag()throwsJspTagException

{

JspWriter out = pageContext.getOut();

try

{

out.println("5f20c42eab9b434e8533424437597eec e56bf5afaa9eef10b2ceba52e1d96544");

}

catch(Exception e)

{

}

returnSKIP_BODY;

}

publiccintdoEndTag()throwsJsptagException

{

returnEVAL_PAGE;

}

publicvoidrelease()

{

super.release();

}

publicvoidsetWidth(String language)

{

this.width = width;

}

publicString getWidth()

{

returnthis.width;

}

publicvoidsetHeight(String height)

{

this.height=height;

}

publicString getHeight()

{

returnthis.height;

}

privateString width;

privateString height;

}

标记符处理程序中所使用的Applet : login.java





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50


importjava.awt.*;

importjava.awt.event.*;

importjava.applet.*;

publicclassloginextendsAppletimplementsActionListener

{

privateString s_username;

privateString s_userpassword;

privateButton b_ok;

privateButton b_register;

privateLabel l_username;

privateLabel l_userpassword;

privateTextField t_username;

privateTextField t_userpassword;

privateGridLayout g_gridlayout;

publicvoidinit()

{

b_ok=newButton("ok");

b_register=newButton("register");

l_username=newLabel("name");

l_userpassword=newLabel("password");

t_username=newTextField();

t_userpassword=newTextField();

b_ok.addActionListener(this);

b_register.addActionListener(this);

g_gridlayout=newGridLayout(3,2,10,10);

this.setLayout(g_gridlayout);

//this.setBackground(Color.blue);

add(l_username);

add(t_username);

add(l_userpassword);

add(t_userpassword);

add(b_ok);

add(b_register);

}

publicvoidactionPerformed(ActionEvent ev)

{

String s_label=ev.getActionCommand();

if(s_label.equals("ok"))

{

t_username.setText("name");

}

if(s_label.equals("register"))

{

t_userpassword.setText("password");

}

}

publicvoidpaint(Graphics g)

{

}

}

例子:

es.tld文件


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd" >
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>es</short-name>

    <tag>
        <name>hasPermission</name>
        <tag-class>com.jfit.core.tag.HasPermissionTag</tag-class>
        <body-content>JSP</body-content>
        <description>
        </description>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
            <description>资源编码,多个之间以“;”分割.</description>
        </attribute>
    </tag>

    <tag>
        <name>lacksPermission</name>
        <tag-class>com.jfit.core.tag.LacksPermissionTag</tag-class>
        <body-content>JSP</body-content>
        <description>
        </description>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
            <description>资源编码,多个之间以“;”分割.</description>
        </attribute>
    </tag>

    <tag>
        <name>hasRole</name>
        <tag-class>com.jfit.core.tag.HasRoleTag</tag-class>
        <body-content>JSP</body-content>
        <description>
        </description>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
            <description>角色编码,多个之间以“;”分割.</description>
        </attribute>
    </tag>

    <tag>
        <name>lacksRole</name>
        <tag-class>com.jfit.core.tag.LacksRoleTag</tag-class>
        <body-content>JSP</body-content>
        <description>
        </description>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
            <description>角色编码,多个之间以“;”分割.</description>
        </attribute>
    </tag>

    <tag>
        <name>isAdmin</name>
        <tag-class>com.jfit.core.tag.IsAdminTag</tag-class>
        <body-content>JSP</body-content>
        <description>
        </description>
    </tag>
    <tag>
        <name>lacksAdmin</name>
        <tag-class>com.jfit.core.tag.LacksAdminTag</tag-class>
        <body-content>JSP</body-content>
        <description>
        </description>
    </tag>


    <tag>
        <name>operation</name>
        <tag-class>com.jfit.core.tag.OperatiuonTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>method</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>permission</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>type</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>iconCls</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>plain</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>imgSrc</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

    </tag>


    <tag>
        <name>dictionary</name>
        <tag-class>com.jfit.core.tag.DictionaryTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>id</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>name</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>width</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>height</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>code</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>type</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <description>可选“combobox”、“combotree”.</description>
        </attribute>
        <attribute>
            <name>selectType</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
            <description>可选“select”、“all”.</description>
        </attribute>
        <attribute>
            <name>required</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>missingMessage</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>validType</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>multiple</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>editable</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>


</taglib>


web.xml


    <!--标签tag-->
    <jsp-config>
        <taglib>
            <taglib-uri>/essh-tags</taglib-uri>
            <taglib-location>/WEB-INF/tlds/es.tld</taglib-location>
        </taglib>
    </jsp-config>


其中一个标签类


public class IsAdminTag extends TagSupport {

    @Override
    public int doStartTag() throws JspException {
        if (SecurityUtils.isCurrentUserAdmin()) {
            return TagSupport.EVAL_BODY_INCLUDE;
        }
        return SKIP_BODY;
    }

}


JSP页面使用


<%@ taglib prefix="e" uri="/essh-tags" %>
                    <e:isAdmin>
						<c:choose>
						<c:when test="${content.mode eq &#39;reply&#39; || content.mode eq &#39;reject&#39;}">
							<a href="${ctxAdmin}/labor/meetingProposal/form?id=${content.id}">查看</a>
						</c:when>
						<c:otherwise>
						<a href="${ctxAdmin}/labor/meetingProposal/audit?id=${content.id}">审核审批答复</a>
						</c:otherwise>
						</c:choose>
		 </e:isAdmin>


위 내용은 java jsp自定义标签相关介绍의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.