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
01 | Myfunctions.java代码 |
02 | public class MyFunctions { |
03 | |
04 | public static String sayHello(String name) { |
05 | return "Hello " + name; |
06 | } |
07 | |
08 | } |
09 | public class MyFunctions { |
10 | |
11 | public static String sayHello(String name) { |
12 | return "Hello " + name; |
13 | } |
14 | |
15 | } |
myfunctions.tld
自定义标签
Xml代码
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | |
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" > |
07 | |
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 > |
13 | |
14 | < function > |
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 > |
18 | </ function > |
19 | |
20 | </ taglib > |
21 | <? xml version = "1.0" encoding = "UTF-8" ?> |
22 |
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 " |
26 | version = "2.0" > |
27 | |
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 > |
33 | |
34 | < function > |
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 > |
38 | </ function > |
39 | |
40 | </ taglib > |
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" %> |
03 | |
04 | <% |
05 | request.setAttribute("name", "David"); |
06 | %> |
07 | |
08 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
09 | < html > |
10 | < head > |
11 | < title >testTemplate</ title > |
12 | </ head > |
13 | < body > |
14 | ${my:sayHello(name) } |
15 | </ body > |
16 | </ html > |
17 | <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> |
18 | <%@ taglib uri="http://www.bjsxt.com/functions " prefix="my" %> |
19 |
20 | <% |
21 | request.setAttribute("name", "David"); |
22 | %> |
23 |
24 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
25 | < html > |
26 | < head > |
27 | < title >testTemplate</ title > |
28 | </ head > |
29 | < body > |
30 | ${my:sayHello(name) } |
31 | </ body > |
32 | </ html > |
补充:web-app version="2.4"
有时也需要在web.xml中添加对标签的定义:
Xml代码
01 | < jsp-config > |
02 | < taglib > |
03 | < taglib-uri >www.bjsxt.com/functions</ taglib-uri > |
04 | < taglib-location >/WEB-INF/my.tld</ taglib-location > |
05 | </ taglib > |
06 | </ jsp-config > |
07 | < jsp-config > |
08 | < taglib > |
09 | < taglib-uri >www.bjsxt.com/functions</ taglib-uri > |
10 | < taglib-location >/WEB-INF/my.tld</ taglib-location > |
11 | </ taglib > |
12 | </ jsp-config > |
注意:
可能出现的异常
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自带的标签元素冲突,重名等