File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Using JSTL variables in a Javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Using JSTL variables in a Javascript?" Watch "Using JSTL variables in a Javascript?" New topic

Using JSTL variables in a Javascript?

Jon Baxter

Joined: Dec 03, 2004
Posts: 9
I know JSTL has been pushed around to reduce scriptlet code in JSPs.
But can JSTL variables be used in Javascript method calls. For example

Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
You can refer to Expression Language variables outside of a JSTL tag if your server supports JSP 2.0. If your server only supports JSP 1.2, then you will need to use a JSTL tag to do what you are asking.

[ December 10, 2004: Message edited by: Anthony Watson ]

Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

Anthony makes a good point: don't confuse the JSTL (tags) and EL (expression language). Although they go hand in hand, they are different animals.

[Asking smart questions] [About Bear] [Books by Bear]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
And NO, you CANNOT use JSTL and/or EL in clientside code and expect it to be executed on the client.
You CAN use it to create clientside code on the fly on the server which is then sent to the client.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

If that point was not already understood, Jeroen has made it quite clear: JSP is merely a templating technology for composing "normal" HTML pages to be sent to the client.
[ December 11, 2004: Message edited by: Bear Bibeault ]
Joseph Hatton

Joined: Dec 12, 2002
Posts: 20
Hi, all!

Correct. JavaScript is executed on the client-side. But that doesn't mean that JSTL can't be evaluated and mixed with JavaScript. Not only can you pass JSTL evaluated code to JavaScript function, you can add JSTL code within the <script></script> tags.

I was caught by surprise when I could do this and set the proper window size. Here is a good example:

var totalDocSize = <c ut value="${retrieveDocumentForm.numOfSvgPages}"/>;
<!-- Use a different set of values when there are versions involved -->
<c:when test="${retrieveDocumentForm.versionSize > 1}">
var toolbarHeight = 100;
var maxHeight = 200;
var maxWidth = 230;
<c therwise>
var toolbarHeight = 130;
var maxHeight = 190;
var maxWidth = 170;
</c therwise>
var rowHeight = 20;
var printPageHight = 40;
var thumbnailHeight = rowHeight * Math.round(((totalDocSize / 4) + 1));
var totalHeight = toolbarHeight + thumbnailHeight + printPageHight;
if (totalHeight > maxHeight) {
totalHeight = maxHeight

However, I don't recommend mixing and matching JSTL with JavaScript. Just in case you can to port this to a JS file, you might have to rip out all of the JSTL expression. Personally, I do try to minimize JSTl and JavaScript mix.
I agree. Here's the link:
subject: Using JSTL variables in a Javascript?
It's not a secret anymore!