JSP - JSTL 如何自定义标签库
jstl标签库的配置
?*?将jstl.jar和standard.jar拷贝到WEB-INF/lib下(如果使用el表达式,不用拷贝这两个jar)
?
?注意:jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行,这个环境
??????是目前较为常用的环境
?????
标签库的使用
?*?采用taglib指令引入
1
|
<%@ taglib prefix=
"c"
? uri=
"http://java.sun.com/jsp/jstl/core
"
%>?
|
2
|
<%@ taglib prefix=
"fmt"
uri=
"http://java.sun.com/jsp/jstl/fmt
"
%>
|
??
??
自定义函数库:
?1、定义类和方法(方法必须是public?static)?
?2、编写自定义tld文件,并且将此文件放到WEB-INF或WEB-INF任意子目录下
?3、在jsp中采用taglib指令引入自定义函数库
?4、采用?前缀+冒号(:)+函数名?调用即可?
MyFunctions.java??
02
|
public
class
MyFunctions {???
|
04
|
???
public
static
String sayHello(String name) {???
|
05
|
??????
return
"Hello "
+ name;???
|
09
|
public
class
MyFunctions {
|
11
|
???
public
static
String sayHello(String name) {
|
12
|
??????
return
"Hello "
+ name;
|
???
myfunctions.tld
自定义标签
Xml代码
01
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>??
|
03
|
<
taglib
xmlns
=
"http://java.sun.com/xml/ns/j2ee
"
?? |
04
|
??
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance
"
?? |
05
|
??
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd
"
?? |
06
|
??
version
=
"2.0"
>??
|
08
|
??
<
description
>my functions library</
description
>??
|
09
|
??
<
display-name
>my functions</
display-name
>??
|
10
|
??
<
tlib-version
>1.0</
tlib-version
>??
|
11
|
??
<
short-name
>my</
short-name
>??
|
12
|
??
<
uri
>http://www.bjsxt.com/functions</
uri
>??
|
15
|
????
<
name
>sayHello</
name
>??
|
16
|
????
<
function-class
>com.bjsxt.struts.MyFunctions</
function-class
>??
|
17
|
????
<
function-signature
>java.lang.String sayHello(java.lang.String)</
function-signature
>??
|
21
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
|
23
|
<
taglib
xmlns
=
"http://java.sun.com/xml/ns/j2ee
"
|
24
|
??
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance
"
|
25
|
??
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd
"
|
28
|
??
<
description
>my functions library</
description
>
|
29
|
??
<
display-name
>my functions</
display-name
>
|
30
|
??
<
tlib-version
>1.0</
tlib-version
>
|
31
|
??
<
short-name
>my</
short-name
>
|
32
|
??
<
uri
>http://www.bjsxt.com/functions</
uri
>
|
35
|
????
<
name
>sayHello</
name
>
|
36
|
????
<
function-class
>com.bjsxt.struts.MyFunctions</
function-class
>
|
37
|
????
<
function-signature
>java.lang.String sayHello(java.lang.String)</
function-signature
>
|
?
jstl_fn.jsp
注意与前面的配置文件myfunctions.tld相对应,
prefix对应<short-name>my</short-name>
uri对应?<uri>http://www.bjsxt.com/functions</uri>
可使用以下面两种方式给name赋值:
1、${my:sayHello("David")?}
2、request.setAttribute("name",?"David");
Java代码
01
|
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>???
|
02
|
<%@ taglib uri="http://www.bjsxt.com/functions
" prefix="my" %>???
|
05
|
request.setAttribute("name", "David");???
|
08
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">???
|
11
|
????
<
title
>testTemplate</
title
>???
|
14
|
????
${my:sayHello(name) }???
|
17
|
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
|
18
|
<%@ taglib uri="http://www.bjsxt.com/functions
" prefix="my" %>
|
21
|
request.setAttribute("name", "David");
|
24
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
27
|
????
<
title
>testTemplate</
title
>
|
30
|
????
${my:sayHello(name) }
|
?
补充:web-app?version="2.4"
有时也需要在web.xml中添加对标签的定义:
Xml代码
03
|
????????
<
taglib-uri
>www.bjsxt.com/functions</
taglib-uri
>??
|
04
|
????????
<
taglib-location
>/WEB-INF/my.tld</
taglib-location
>??
|
09
|
????????
<
taglib-uri
>www.bjsxt.com/functions</
taglib-uri
>
|
10
|
????????
<
taglib-location
>/WEB-INF/my.tld</
taglib-location
>
|
注意:
???
可能出现的异常
1、The?function?xxx?must?be?used?with?a?prefix?when?a?default?namespace?is?not?specified
---?在jsp页面中调用方式不正确,可能将?":"?写成了?"."
2、The?function?xxx?cannot?be?located?with?the?specified?prefix
---?a)?类中定义的方法不是?public?static?的方法
??????b)?类中的方法名称和jsp自带的标签元素冲突,重名等
?
?
?