问题就出在建Web Project的时候web.xml声明上。
web.xml声明部分一般分为如下版本的xsd,
web-app_2_2.xsd
web-app_2_3.xsd
web-app_2_4.xsd
web-app_2_5.xsd
更详细的列出各版本web.xml声明部分吧,如下:
web-app_2_2.xsd
Java代码
web.xml声明部分一般分为如下版本的xsd,
web-app_2_2.xsd
web-app_2_3.xsd
web-app_2_4.xsd
web-app_2_5.xsd
更详细的列出各版本web.xml声明部分吧,如下:
web-app_2_2.xsd
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/dtd/web-app_2_2.dtd">
web-app_2_3.xsd
Java代码
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
web-app_2_4.xsd
Java代码
<?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">
web-app_2_5.xsd
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
如果想使用EL表达式,确定web.xml里的xsd版本之后一定要在JSP的声明(<%@page %>)部分加一行,如下:
<%@ page isELIgnored="false" %>
这样设为false才能解析EL表达式。 经过各版本的test之后....
注意!! 其中servlets 2.4(我没记错的话JSP 2.0出来之后的第一个版本),这个版本的isELIgnored默认设置为false。所以使用web.xml里用web-app_2_4.xsd声明的时候在JSP页面不用特意声明。
官方Documention中写道
The isELIgnored Attribute
• Format
– <%@ page isELIgnored="false" %>
– <%@ page isELIgnored="true" %>
Purpose
– To control whether the JSP 2.0 Expression Language
(EL) is ignored (true) or evaluated normally (false).
• Notes
– If your web.xml specifies servlets 2.3 (corresponding to
JSP 1.2) or earlier, the default is true
• But it is still legal to change the default—you are permitted
to use this attribute in a JSP-2.0-compliant server
regardless of the web.xml version.
– If your web.xml specifies servlets 2.4 (corresponding to
JSP 2.0) or earlier, the default is false
• Format
– <%@ page isELIgnored="false" %>
– <%@ page isELIgnored="true" %>
Purpose
– To control whether the JSP 2.0 Expression Language
(EL) is ignored (true) or evaluated normally (false).
• Notes
– If your web.xml specifies servlets 2.3 (corresponding to
JSP 1.2) or earlier, the default is true
• But it is still legal to change the default—you are permitted
to use this attribute in a JSP-2.0-compliant server
regardless of the web.xml version.
– If your web.xml specifies servlets 2.4 (corresponding to
JSP 2.0) or earlier, the default is false
不同版本引入新的标签库的
< web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
< taglib>
< taglib-uri>http://jsptags.com/tags/navigation/pager< /taglib-uri>
< taglib-location>/WEB-INF/pager-taglib.tld< /taglib-location>
< /taglib>
< /web-app>
按上面进行配置时MyEclipse一直在报错,应该把< taglib> 放到 < jsp-config>中:
< ?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
< jsp-config>
< taglib>
< taglib-uri>http://jsptags.com/tags/navigation/pager< /taglib-uri>
< taglib-location>/WEB-INF/pager-taglib.tld< /taglib-location>
< /taglib>
< /jsp-config>
< /web-app>
sun从来不把servlet各个版本的web.xml的规格公布一下,即使上网查找,也很难发现。倒是跟随支援的servlet容器会发布一些范例程序,好运的话,里面将会有最新版本的web.xml写法,比如说,tomcat6正式版里面有了servlet 2.5的写法,如下:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
version="2.5">
别以为看到这里就结束了,很可惜地告诉你,这段代码是错误的。不信你尝试打开一下这个链接http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd,你会发现是no page found。
那为什么tomcat6的范例程序能够工作呢,那是因为在tomcat6的lib里面,已经存在这个文件,所以也不需要从网络上面抓取。其实你按照这个web.xml写了servlet 2.5的程序,在tomcat6里面也是可以运行的。
可是当我使用eclipse+xmlbuddy的时候,问题就出来了,因为web-app_2_5.xsd一直不能下载,xmlbuddy一直报错,并且没有语法提示功能。通过搜索,我发现了web-app_2_5.xsd的真实地址其实是http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd,而它的namespace是http://java.sun.com/xml/ns/javaee,于是代码应该改成:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
留意一下不同的部分,其实是因为sun把j2ee改名为javaee。