Java JSP 中的表达式语言不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2168832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expression Language in JSP not working
提问by user262577
I am new to JSP and using Expression language. I am using Eclipse Galileo with version 2.5 and Tomcat 6 server . I just want to ask that my simple Expression Language is not printing the vale like if i write ${1>2}
which suppose to give false but it is displaying ${1>2}
only when it renders the page. But when i am using <c:out value="${1>2}"/>
it is printing false correctly. I think there is an issue with tag library. Please kindly suggest me the reason for this i am giving a sample code for this so that you can understand where I am going wrong:-
我是 JSP 的新手并使用表达式语言。我正在使用 Eclipse Galileo 和 2.5 版和 Tomcat 6 服务器。我只是想问一下,我的简单表达式语言不会像我写的那样打印 vale${1>2}
假设给 false 但它${1>2}
仅在呈现页面时才显示 。但是当我使用<c:out value="${1>2}"/>
它时正确打印错误。我认为标签库存在问题。请告诉我这样做的原因,我为此提供了一个示例代码,以便您了解我哪里出错了:-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Expression Language Example</title>
</head>
<body>
Is 1 greater than 2 using cout :<c:out value="${1>2}"/>
Is 1 greater than 2 without using cout: ${1>2}
</body>
</html>
Updateas per the answers, here is more information:
根据答案更新,这里有更多信息:
I am showing my web.xml
how it looks like:
我正在展示我的web.xml
样子:
<?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:jsp="http://java.sun.com/xml/ns/javaee/jsp" 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">
<display-name>ScriptLessJsp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ElServlet</display-name>
<servlet-name>ElServlet</servlet-name>
<servlet-class>com.servlet.El.ElServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ElServlet</servlet-name>
<url-pattern>/ElServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Collections</display-name>
<servlet-name>Collections</servlet-name>
<servlet-class>com.servlet.El.Collections</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Collections</servlet-name>
<url-pattern>/go</url-pattern>
</servlet-mapping>
</web-app>
And in my lib folder I have added only jstl.jar
so that i can make use of <c:out>
tag to display but my EL for template text is not working.
在我的 lib 文件夹中,我只添加了jstl.jar
这样的<c:out>
标签,以便我可以使用标签来显示,但我的模板文本 EL 不起作用。
回答by BalusC
I'm quoting from an answerI provided before to the problem of EL not working:
我引用了我之前提供的关于 EL 不工作问题的答案:
With other words, the EL expression doesn't get evaluated? That can have one or more of the following causes:
- Application server in question doesn't support JSP 2.0.
- The
web.xml
is not declared as Servlet 2.4 or higher.- The
@page
is configured withisELIgnored=true
.- The web.xml is configured with
<el-ignored>true</el-ignored>
in<jsp-config>
.
换句话说,EL 表达式没有得到评估?这可能有以下一种或多种原因:
- 有问题的应用程序服务器不支持 JSP 2.0。
- 在
web.xml
未声明为的Servlet 2.4或更高版本。- 该
@page
配置有isELIgnored=true
。- web.xml 配置为
<el-ignored>true</el-ignored>
in<jsp-config>
。
In your particular case, EL works in taglibs, but not in template text, so I suspect it's caused by point 2. Ensure that your web.xml
is declared as at leastServlet 2.4. As Tomcat 6.0 supports Servlet 2.5, I would recommend to declare your web.xml
as Servlet 2.5:
在您的特定情况下,EL 在 taglibs 中工作,但不在模板文本中工作,所以我怀疑它是由第 2 点引起的。确保您至少web.xml
声明为Servlet 2.4。由于 Tomcat 6.0 支持 Servlet 2.5,我建议将您声明为 Servlet 2.5:web.xml
<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="Your_WebApp_ID"
version="2.5">
<!-- Here you go. -->
</web-app>
Another rare cause I've seen on this is that there's a collision with EL JAR's in the classpath. Ensure that you have notcopied any appserver-specific JAR files into your webapp's WEB-INF/lib
or, more worse, the JRE/lib
.
我在这方面看到的另一个罕见原因是与类路径中的 EL JAR 发生冲突。确保您没有将任何特定于应用程序服务器的 JAR 文件复制到您的 web 应用程序中,WEB-INF/lib
或者更糟的是,没有将任何特定于应用程序服务器的 JAR 文件复制到JRE/lib
.
As you're already using Eclipse and Tomcat, I would review the development steps you used for this all. Ensure that you're using "Eclipse for Java EEdevelopers" and that you've integrated the Tomcat instance in Eclipse's Serversview and that you've created a Dynamic Web Projectset to "Servlet 2.5" which makes use of the Tomcat instance. This way everything should go automagically (Eclipse will take appserver's libraries in the build path itself and autogenerate a Servlet 2.5 compliant web.xml
).
由于您已经在使用 Eclipse 和 Tomcat,我将回顾您用于这一切的开发步骤。确保您使用的是“Java EE开发人员的Eclipse ”,并且您已经在 Eclipse 的服务器视图中集成了 Tomcat 实例,并且您已经创建了一个使用 Tomcat 实例的设置为“Servlet 2.5”的动态 Web 项目。这样一切都应该自动进行(Eclipse 将在构建路径本身中采用 appserver 的库并自动生成符合 Servlet 2.5 的web.xml
)。
Update:as per your update: those com.servlet.El
servlets look suspicious. What exactly do they do? Parsing EL? Remove them and retry.
更新:根据您的更新:这些com.servlet.El
servlet 看起来很可疑。他们具体做什么?解析EL?删除它们并重试。
回答by McDowell
BalusC covers it, but I'll add these comments:
BalusC 涵盖了它,但我会添加以下评论:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
The JSTL TLDnamespace should include a "jsp" (as above). Given this namespace error may be due to following old instructions, check the most recent documentation to ensure you haven't included any obsolete libraries in WEB-INF/lib
. A number of technologies that used to be separate are now included in the container (the EL language being one).
所述JSTL TLD命名空间应包括一个“JSP”(如上述)。鉴于此命名空间错误可能是由于遵循旧说明造成的,请检查最新的文档以确保您没有在WEB-INF/lib
. 许多过去独立的技术现在包含在容器中(EL 语言就是其中之一)。
As an aside, I would generally use keywords like gt
instead of >
and lt
instead of <
- this is friendlier to XML and its ilk.
顺便说一句,我通常会使用关键字gt
代替>
和lt
代替<
- 这对 XML 及其同类更友好。
回答by Jimmy
In my case, I generated a webapp by maven archetype generator, I use maven-archetype-webapp. I need to change two things:
就我而言,我通过 maven 原型生成器生成了一个 webapp,我使用了 maven-archetype-webapp。我需要改变两件事:
in web.xml, change the head to:
<?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" version="2.5">
by default, eclipse generated jsp file with head:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
在 web.xml 中,将头部更改为:
<?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" version="2.5">
默认情况下,eclipse 生成带有头部的 jsp 文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Just remove it.
只需将其删除。
回答by Niklas P
I've had the problem that with the line <!DOCTYPE html>
in my JSP, the tomcat started with eclipse didn't interpret the EL correctly. Using the war and copying it to the webapps
folder and then starting tomcat with the commandline did the job.
我遇到的问题是,<!DOCTYPE html>
在我的 JSP 中,以 eclipse 启动的 tomcat 没有正确解释 EL。使用 war 并将其复制到webapps
文件夹,然后使用命令行启动 tomcat 即可完成这项工作。
So obviously eclipse tomcat plugin has a problem with <!DOCTYPE html>
?!
所以很明显eclipse tomcat插件有问题<!DOCTYPE html>
?!
回答by Ilya Yevlampiev
Setting <%@ page isELIgnored="false" %>
at the top of the page helped to me. Don't know why it was the root of the problem in my case. not clear why yet
<%@ page isELIgnored="false" %>
页面顶部的设置对我有帮助。不知道为什么这是我的问题的根源。尚不清楚为什么