日期:2014-05-16  浏览次数:20358 次

问题解决方案-解决JSP页面中的EL表达式不被解析的问题

解决JSP页面中的EL表达式不被解析的问题


今天遇到EL表达式不被解析的问题,现象如下:

在**.jsp中设定了<c:set scope="request" var="sysCtxp" value="${pageContext.request.contextPath}" />,但是使用${sysCtxp}却无法取得值,输出后是"${sysCtxp}"字符串,EL表达式没有被解析。


反复尝试过一些方案后,将此类问题的解决方案归纳如下:


1、升级Tomcat容器至Tomcat6或以上;但请注意,该方法不能保证一定可以解决该问题,尝试过发现,即使6.x的不同版本也存在同样的问题,似乎与Tomcat6下lib目录中的jsp-api.jar/servlet-api.jar有关系,但是使用7.x版本,目前还没有出现该问题。


2、修改web.xml中<web-app>的属性。

一般修改该属性时,可以使用eclipse创建动态web工程方式,在创建时选定servlet2.4以上的版本,创建后会自动生成web.xml文件,然后从中摘出即可,最好不手写,以免出错。

?

以下是把web.xml中<web-app>版本修改到2.4或以上的样例代码:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 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">

?

注意:如果web.xml中声明部分的schema版本为2.4或者以上,而tomcat使用的是5.5.x以下的版本的时候就会出现在页面直接显示而不解析jstl/el表达式。如果web.xml中声明部分的schema版本为2.5或者以上,tomcat使用使用的是6.0以上则不出现这种问题。


3、在使用了EL表达式的所有JSP页面中,加入<%@ page isELIgnored="false" %>,可以把该语句放置到共通jsp页面上,然后在其他页面上include即可。


下面是官方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?

?