JSP: EL expression is not evaluated

JSP: EL expression is not evaluated

归属感 发布于 2021-11-27 字数 473 浏览 930 回复 9 原文

I have a JSP page running on Tomcat 5.5. I have the following code:

 <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.

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

书间行客 2022-06-07 9 楼

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
乞讨 2022-06-07 8 楼

Use tomcat6. It doesn't requires any configuration for EL in web.xml.

栩栩如生 2022-06-07 7 楼

See my answer at Javascript String.replace(/$/,str) works weirdly in jsp file for possible reasons.

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.

愛放△進行李 2022-06-07 6 楼

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

没有伤那来痛 2022-06-07 5 楼

For those interested, the equivalent XML syntax for JSP 2.0 is:

<jsp:directive.page isELIgnored="false"/>
你与昨日 2022-06-07 4 楼

Make sure to include the relevant namespaces in the web.xml. Just try to replace

<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.

情深缘浅 2022-06-07 3 楼

It's the header in web.xml that causes the problem

Below maven generated header stops EL from being eval'ed.

<!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.

<?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">
一直在等你来 2022-06-07 2 楼

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:

<!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.

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.

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.

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!

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.

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.

So the problem was with 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.

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.

稍尽春風 2022-06-07 1 楼

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:

<%@ 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.