使用jstl1.1,注意与1.0的区别

来源:互联网 发布:mac 链接服务器 编辑:程序博客网 时间:2024/05/17 12:49

近日做一个小系统,使用了Struts,还用到了JSTL。这套配置在以前开发项目的时候就用过的,不过当时没有深入了解。所以,导致本次使用时,遇到了一个小问题,浪费了一点时间。

所以,做了一个小小的调查,搞清楚了JSTL1.0和JSTL1.1的区别及使用方法。

 

现象

在<c:out>标签中使用了EL,结果运行时报错。说根据tld文件的说明,该标签不支持表达式(EL)。

原文为:

According to TLD or attribute directive in tag file, attribute value does not accept any expressions

 

依赖的环境及jar

1. 使用的标签库是从apache上down下来的。

    http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html

    一共两个文件,一个jstl.jar,一个standard.jar。

 

2. tomcat是5.0系列的。

 

原因

在JSP中声明uri的时候,写成了jstl 1.0的uri。

而1.0确实是不支持EL的,但是它的tld文件又存在于1.1的jar包中。

 

经过调查确认,总结如下三点:

1. jstl 1.0和1.1支持的servlet,jsp规范都不相同,支持相应规范的tomcat的版本也有要求的。具体如下。

NOTE: Standard-1.1 (JSTL 1.1) requires a JSP container that supports the Java Servlet 2.4 and JavaServer Pages 2.0 specifications. Jakarta Tomcat 5 supports the new specifications. The Standard-1.1 taglib has been tested with Tomcat 5.0.3.Standard-1.0 (implementation of the JSTL 1.0 specification) requires a JSP container that supports the Java Servlet 2.3 and JavaServer Pages 1.2 specifications. Jakarta Tomcat 4 supports these specifications. The Standard 1.0 taglib has been tested with Tomcat 4.1.24.

 

2. web.xml中要申明相应的servlet版本。

JSTL1.1和JSP2.0需要servlet2.4

<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">

 

JSTL1.0和JSP1.2需要servlet2.3

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.3" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_3.xsd">

 

3. JSTL1.0和JSTL1.1的uri是不一样的,但是他们的tld文件同时存在于同一个standard.jar中。

解压缩下载的standard.jar,你会在/META-INF/下发现有一个c-1_0.tld,还有一个c.tld。

其中c-1_0.tld是JSTL1.0的core标签库的tld文件,c.tld是JSTL1.1的core标签库的tld文件。

用文本软件打开两个文件就会找到具体的uri。

 

JSTL1.0的使用方法为:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

JSTL1.1的使用方法为:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>