wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Using JSTL variables in a Javascript?" Watch "Using JSTL variables in a Javascript?" New topic
Author

Using JSTL variables in a Javascript?

Jon Baxter
Greenhorn

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
Marshal

Joined: Jan 10, 2002
Posts: 60764
    
  65

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] [Bear's FrontMan] [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.


42
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60764
    
  65

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
Greenhorn

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:

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


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.
 
wood burning stoves
 
subject: Using JSTL variables in a Javascript?
 
Similar Threads
Mixing JSTL and JSP?
JSTL and basic String methods
Can't get first JSTL to work
Form Submit to JavaScript in Pop-up
JSTL c:redirect doesn't work from included jsp file