Java JSP:不计算 EL 表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/793983/
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
JSP: EL expression is not evaluated
提问by
I have a JSP page running on Tomcat 5.5. I have the following code:
我有一个在 Tomcat 5.5 上运行的 JSP 页面。我有以下代码:
<c:forEach var="i" begin="1" end="10" step="1">
<c:out value="${i}" />
<br />
</c:forEach>
The output I am getting is:
我得到的输出是:
${i}
${i}
${i}
${i}
${i}
${i}
${i}
${i}
${i}
${i}
I cant work out why the forEach loop is working but the output is not working. Any help any one could give would be great.
我无法弄清楚为什么 forEach 循环有效但输出无效。任何人可以提供的任何帮助都会很棒。
采纳答案by RHSeeger
I know it's supposed to be on by default, but I run across pages now and again (or even the same page that changes behavior) where the EL processing doesn't happen. Adding the following to the top of any such pages should resolve the issue:
我知道默认情况下它应该是打开的,但是我时不时地跨页面运行(甚至是改变行为的同一页面),EL 处理不会发生。将以下内容添加到任何此类页面的顶部应该可以解决问题:
<%@ page isELIgnored="false" %>
I add it to every page because it doesn't hurt, and I still don't know the root cause that occasionally causes a page to stop interpreting the EL expressions.
我将它添加到每个页面,因为它不会受到伤害,但我仍然不知道偶尔导致页面停止解释 EL 表达式的根本原因。
回答by Peter ?tibrany
See my answer at Javascript String.replace(/\$/,str) works weirdly in jsp filefor possible reasons.
由于可能的原因,请参阅我在Javascript String.replace(/\$/,str) 中的回答在 jsp 文件中奇怪地工作。
Longer answer: ${i} is expression in so called 'Expression Language'. Sometimes, Expression Language can be disabled. See above answer for potential reasons, and ways how to enable it.
更长的答案: ${i} 是所谓的“表达式语言”中的表达式。有时,可以禁用表达式语言。有关潜在原因以及如何启用它的方法,请参阅上面的答案。
回答by Maheshkumar.P
Use tomcat6. It doesn't requires any configuration for EL in web.xml.
使用tomcat6。它不需要在 web.xml 中对 EL 进行任何配置。
回答by kevinmrohr
I just had this same problem and spent forever trying to figure out what was wrong.
我只是遇到了同样的问题,并且一直在试图找出问题所在。
I've developed lots of web apps from scratch. Why suddenly was this one not cooperating?
我从头开始开发了许多网络应用程序。这个人怎么突然不合作了?
One difference was this time I used the maven webapp archetype to generate the project structure. It created a web.xml file that looked like this:
一个区别是这次我使用了 maven webapp 原型来生成项目结构。它创建了一个 web.xml 文件,如下所示:
<!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>
<display-name>Archetype Created Web Application</display-name>
</web-app>
Once I realized that as my problem, I was sure I had the answer. So I copied one of my 2.5 web.xml headers, rebuilt, and redeployed. No cigar. Couldn't believe that wasn't the problem. Cleaned the project, restarted tomcat. Nope.
一旦我意识到这是我的问题,我确信我有答案。所以我复制了我的 2.5 web.xml 头文件之一,重建并重新部署。没有雪茄。不敢相信这不是问题。清理项目,重启tomcat。不。
RHSeeger's answer led me to try putting in the <%@ page isELIgnored="false" %>. That resolved the problem. But I still wanted to know why el started getting ignored to begin with.
RHSeeger 的回答让我尝试放入 <%@ page isELIgnored="false" %>。那解决了问题。但我仍然想知道为什么 el 开始被忽视。
I figured the el was being ignored because of something wrong with my web.xml, so I closely inspected it in comparison with another webapp's web.xml that I knew worked fine. No noticeable differences.
我认为 el 被忽略是因为我的 web.xml 有问题,所以我仔细检查了它,并与我知道工作正常的另一个 webapp 的 web.xml 进行了比较。没有明显的差异。
Then I removed the <%@ page isELIgnored="false" %> from my JSP, and redeployed, assuming the el would not be evaluated again, but much to my surprise, the el was evaluated fine!
然后我从我的 JSP 中删除了 <%@ page isELIgnored="false" %> 并重新部署,假设不会再次评估 el,但令我惊讶的是,el 的评估结果很好!
Then, figuring it must be some sort of caching problem, I undid my changes to the web.xml to recreate the problem. I redeployed, but still the el was being evaluated correctly, even with the bad web.xml. Then I cleaned my entire project (I'm using an exploded deployment), blowing away the exploded directory and recreating it. I then restarted tomcat. Still, the el appeared to be getting evaluated correctly in spite of the bad web.xml.
然后,确定它一定是某种缓存问题,我取消了对 web.xml 的更改以重新创建问题。我重新部署了,但仍然正确评估了 el,即使使用了错误的 web.xml。然后我清理了我的整个项目(我使用的是爆炸部署),吹走爆炸目录并重新创建它。然后我重新启动了tomcat。尽管如此,尽管 web.xml 很糟糕,但 el 似乎得到了正确的评估。
Finally it dawned on me. I simply added a space to some place in the JSP, repackaged it, and refreshed the page. Bingo! Now the el was not getting evaluated.
我终于明白了。我只是在 JSP 中的某个地方添加了一个空格,重新打包并刷新了页面。答对了!现在 el 没有得到评估。
So the problem waswith the web.xml. It would further complicated by the fact that the JSPs weren't getting recompiled unless they had changed. Not sure if tomcat uses an MD5 sum to decide if the JSPs need to be recompiled or what. Another possibility is that I'm using tiles, which I know has a caching mechanism, but I wouldn't expect that to survive an tomcat restart.
所以,问题是与web.xml中。由于 JSP 除非已更改,否则不会重新编译,这会进一步复杂化。不确定 tomcat 是否使用 MD5 sum 来决定是否需要重新编译 JSP 或什么。另一种可能性是我正在使用 tile,我知道它有一个缓存机制,但我不希望它能够在 tomcat 重启后幸存下来。
Anyway, unless you modify your JSPs AFTER fixing the web.xml, all bets are off as to whether the EL will start working again. Hope this saves someone else a headache. I'm also interested if anyone can tell me whether it was tomcat not recompiling the JSPs or tiles caching the output of the JSP. I'm pretty sure it's the recompile, because at compile time is when the JSP should have to figure out what to do with the ${} el expressions, right? Tiles can't actually cache what gets substituted into the el expressions, otherwise all kinds of problems would arise.
无论如何,除非您在修复 web.xml 之后修改您的 JSP,否则关于 EL 是否会再次开始工作的所有赌注都没有了。希望这可以避免其他人头痛。如果有人能告诉我是 tomcat 没有重新编译 JSP 还是瓦片缓存了 JSP 的输出,我也很感兴趣。我很确定这是重新编译,因为在编译时 JSP 应该弄清楚如何处理 ${} el 表达式,对吗?Tiles 实际上不能缓存被替换到 el 表达式中的内容,否则会出现各种问题。
回答by greg one
had similar problem to Kevin, i used maven to get started with webapp but no joy with evaluating expressions in jsp - i had to remove DOCTYPE header - all is good now without playing with isELIgnored bit - maven generates web.xml which links to 2.3 which as Peter stated has EL disabled by default
与 Kevin 有类似的问题,我使用 maven 开始使用 webapp,但在 jsp 中评估表达式并不快乐 - 我不得不删除 DOCTYPE 标头 - 现在一切都很好,无需使用 isELIgnored 位 - maven 生成链接到 2.3 的 web.xml正如彼得所说,默认情况下已禁用 EL
回答by Bouke Woudstra
Make sure to include the relevant namespaces in the web.xml. Just try to replace
确保在 web.xml 中包含相关的命名空间。只能尝试更换
<web-app>
with something like
像
<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_3_0.xsd"
version="3.0"
metadata-complete="true">
It fixed it for me. You can find the correct namespaces for your Tomcat instance in the example apps that come with a Tomcat install.
它为我修好了。您可以在 Tomcat 安装附带的示例应用程序中找到 Tomcat 实例的正确命名空间。
回答by Md. Rahman
From Controller:
来自控制器:
@RequestMapping(value = "createcustomer",method = RequestMethod.GET)
public String customer(Model model)
{
Customer cus=new Customer();
cus.setCustomerNumber("Test");
model.addAttribute("customer",cus);
return "createcustomer";
}
In View:
在视图中:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<div class="cl">
<form:form commandName="customer" method="POST">
<p>Name: <c:out value="${customer.CustomerNumber}"></c:out></p>
</form:form>
<div>
Output:
输出:
Name: Test
回答by user3954300
It's the header in web.xml that causes the problem
这是导致问题的 web.xml 中的标头
Below maven generated header stops EL from being eval'ed.
在 maven 生成的标头下方阻止 EL 被评估。
<!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>
Using below header eval's the EL.
使用下面的标头 eval 是 EL。
<?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">
回答by Fabien R
For those interested, the equivalent XML syntax for JSP 2.0 is:
对于那些感兴趣的人,JSP 2.0 的等效 XML 语法是:
<jsp:directive.page isELIgnored="false"/>