1

I'm trying to create a percentage bar with HTML+CSS. I have the following code:

<div id="fundingBar">
    <div id="fundingPercentage" style="width:45%"><p class="percentage">45%</p></div>
</div>

Now I want the percentage to be a dynamic value, so I have a session attribute called "percentage". I'd like to use this value in the JSP page and the only way I know is using EL, so it should be something like this:

<div id="fundingBar">
    <div id="fundingPercentage" style="width:${percentage}%"><p class="percentage">${percentage}%</p></div>
</div>

but this doesn't work, so is there any way to do it this way or I should use javascript or something like that?

Thanks!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
MikO
  • 18,243
  • 12
  • 77
  • 109

2 Answers2

4

EL in template text (read: EL in plain HTML outside any JSP tags) works only if the web.xml is declared conform Servlet 2.4 or newer and if your webapp is deployed to a container which supports Servlet 2.4 or newer (which is already out since November 2003) and your webapp doesn't contain servletcontainer-specific libraries of an older version in its /WEB-INF/lib which would only collide container's own libraries.

Otherwise, if it's indeed a legacy system and not just a misconfigured web.xml version or a version clash in the classpath, then your only and ugly resort is using JSTL <c:out>.

<div id="fundingPercentage" style="width:<c:out value="${percentage}"/>%">

See also:


Of course I assume that you have really set the session attribute beforehand like so:

request.getSession().setAttribute("percentage", 45);
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks @BalusC! perfect answer. In fact my web.xml has version 3.0, so it does accept EL as plain text, the problem is that NetBeans is marking it as an error, but anyway it works fine! I didn't realize before that it was working because moreover I calculated badly the percentage so instead of setting 12% I was setting 0.12% so I couldn't see any changes :)... your links helped a lot! thanks! – MikO Dec 13 '12 at 17:23
0

How wide is the #fundingBar element? When using percentages, the elements are calculated against the parent element. So if #fundingBar is 0px wide, then 45% of 0px is also 0px.

cimmanon
  • 67,211
  • 17
  • 165
  • 171
  • no, funding bar element is 300px. I have a separated CSS file to define that. But for the #fundingPercentage div I put the style tag directly in the JSP page because I'd need to set it dynamically... – MikO Dec 13 '12 at 16:43