• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using JSTL variables in a Javascript?

 
Jon Baxter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64962
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64962
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic