原因:問題在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文件也可以。 在jsp頁面的<%@ page isELIgnored="false" %>聲明中,將isELIgnored屬性設為false,EL表達 式才可以正常顯示,在2.4版本之前的版本,isELIgnored默認為true,所以只能在JSP頁面設置這個屬性為false才可以。2.4版本中isELIgnored屬性默認為false,因此EL表達式可以直接使用。
方法:1.JSP頁面中<%@ page isELIgnored="false" %>,每個頁面都如此,就會很麻煩。
2.將web.xml中的聲明改為2.4版本,如下:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd">
下面是官方Documention中isELIgnored Attribute的詳解:
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
PS: 我本來的版本是2.5,以前也沒出過此類問題,感覺可能是Tomcat的版本不一樣造成的問題,在Tomcat中的web.xml中貌似也有可以配置EL可用的定義。這種小問題雖然不起眼,但寫程序時感覺還是很容易遇到的,而且只有在親自寫代碼時才會碰到,書上是找不到的,所以積少成多,也許這就是經驗積累的過程吧!
PS: 2.5版本的也是默認true,要在每個使用EL表達式的JSP設定isELIgnored為false,
另,引入JSTL可以在本地放置uri="/WEB-INF/c.tld"文件,也可以引入網上的解析文件uri="http://java.sun.com/jsp/jstl/core",同時別忘了需要2個jar包(jstl.jar和standard.jar)