JSP: EL expression is not evaluated [duplicate]
Asked Answered
F

9

30

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.

Floorman answered 27/4, 2009 at 15:14 Comment(0)
G
66

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.

Gussiegussman answered 27/4, 2009 at 15:21 Comment(5)
On Tomcat 5.5, there are (only?) two possible reasons: invalid schema in web.xml, or el-ignored config option.Selfdenial
I (and others) have had a single page working fine and, after changing something on the page (ie, adding some html), it just stops evaluating the EL expressions. The only explanation I can come up with is a bug in Tomcat somewhere, but it's not worth digging too deep into since enabling it manually (via the code above) resolves the problem.Gussiegussman
If you created your webapp with maven archetype the correct answer is this: https://mcmap.net/q/465945/-jsp-el-expression-is-not-evaluated-duplicateExhaustless
@Exhaustless That's it. Many thanks!Tendon
It's amazing. I got headache whole day until reading this solution. Thank you very much!Baronetcy
M
39

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.

Milestone answered 12/8, 2011 at 3:13 Comment(3)
Thanks so much, this is exactly what I have been through - created from archetype, and EL and JSTL all not working, even though JSP file was using exactly same code as some previous app that was working before. Fixed the web.xml namespaces, restarted, still not working; finally modified the JSP, and sorted!Propound
It was Tomcat not recompiling the JSPs. It tracks the last modified of the JSP file - newer versions also track the last modified times of the dependencies. These are checked at a configurable frequency (usually 5 seconds if memory serves) and recompilation is triggered if a change is detected. The .java file can be located in Tomcat's work directory and will persist across restarts.Approximal
@Milestone Thanks so much for your detailed experiment and answer.Tendon
Z
18

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">
Zaremski answered 18/8, 2014 at 22:1 Comment(4)
that's save my day! thanks!Remembrance
This solution is working.Islander
maven generated archetype's header in web.xml is the rootcause for the issue.changing the header got worked for me .ThanksTramroad
If you want to continue to use a <!DOCTYPE element because, for instance, you want to define some includes, you can do that. Just make sure to remove the DTD definition out of the DOCTYPE and move it to the web-app as shown above.Cincinnatus
L
9

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.

Larva answered 11/1, 2014 at 10:9 Comment(0)
P
2

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

<jsp:directive.page isELIgnored="false"/>
Pepsinogen answered 18/11, 2015 at 15:0 Comment(0)
K
1

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

Karolekarolina answered 3/1, 2013 at 13:4 Comment(0)
A
0

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.

Agrarian answered 27/4, 2009 at 15:21 Comment(0)
N
0

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

Nimiety answered 23/4, 2010 at 8:50 Comment(1)
Tomcat 5.5 also doesn't require any configuration for EL in web.xml. Tomcat 5.0 also doesn't. If you have had a problem with it, it lies somewhere else.Cashandcarry
B
0

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
Burkitt answered 11/4, 2014 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.