• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

questions about JSTL variables

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<c:set var="msg" value="Name List includes: "/>
<c:forEach items="${sessionScope.NameList}" var="name">
<c:set var="msg" value="${msg + name + ","}"/>
</c:forEach>

// now want to access this "msg" variable in JSp, how ?

Questions

1. Session Object "NameList" includes a List of Names Strings, so I want to iterate through it, and concatnate its name String with a comma separator, and eventually the msg should be saved in a variable called "msg". However, it just doesn't work, I don't know how I can correct the syntax..

2. In the JSp following this JSTL snippet, I want to access this "msg", but I don't know how. For example, how can I assign "msg" defined in JSTL to a new variable in JSP ? Can I do

<% String newMsg = msg; %>

I don't think this will work. But how can I access (not just print) a variable created in JSTL ?

Thanks
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per forum instructions, the versions of JSP and JSTL that you are using is needed to answer your question.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for your questions:

1. There is no string concatentation operator in the EL. What you want to do is easily possible using another mechanism if you are using JSP 2.0.

2. Why would you want to mix the JSTL and EL wth scriptlets? Use one or the other. Mushing both types of technology together on the same page just creates a mess.
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
As for your questions:

1. There is no string concatentation operator in the EL. What you want to do is easily possible using another mechanism if you are using JSP 2.0.

2. Why would you want to mix the JSTL and EL wth scriptlets? Use one or the other. Mushing both types of technology together on the same page just creates a mess.



It is not the latest version of JSTL/JSP, it should be the version two year ago. I want to use JSTL (if I can avoid scriplet), but don't know how to concat it and create a new var, I need to pass this "var" to a javascript section and pop up the msg. It is like

<c:set var="msg" value="Name List includes: "/>
<c:forEach items="${sessionScope.NameList}" var="name">
<c:set var="msg" value="${msg + name + ","}"/>
<script language="javascript">
alert('<%= msg %>);
</script>
</c:forEach>

I know in a javascript it can access a JSP variable by using <%= var %> but I don't know if we can access a EL variable in javascript. that's why I want to "assign" a EL variable to a JSP variable first and then access the JSp var in javascript.

Any help ? Thanks in advance
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry what I meant was --

<c:set var="msg" value="Name List includes: "/>
<c:forEach items="${sessionScope.NameList}" var="name">
<c:set var="msg" value="${msg + name + ","}"/>
</c:forEach>

<script language="javascript">
alert('<%= msg %>);
</script>
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj Ohadi:
I want to use JSTL (if I can avoid scriplet)



This is a worthy goal. I'm never short of amazed by how much easier JSP pages are to deal with once all scriplets have been banished from them. But, it is a difficult goal to achieve with versions of JSP prior to JSP 2.0.

Prior to JSP 2.0, an introductory version of the EL is available that is only able to be employed within the attributes of certain JSTL tags. This severely hampers what can be done with it.

If you are sincere in your desire to step into the best practice world of scriptless JSP pages, you really will have to buck up and migrate to JSP 2.0 (or convince the powers that be that it is necessary).


I know in a javascript it can access a JSP variable by using <%= var %> but I don't know if we can access a EL variable in javascript. that's why I want to "assign" a EL variable to a JSP variable first and then access the JSp var in javascript.



Technically, Javascript has access to neither of scripting variables or scoped variables (what you called an "EL variable").

What happens is the variable (of either type) is interpreted on the server to "write" the Javascript statement, which is then sent to the browser. From the Javascript point-of-view, it has no way of knowing whether you hard-coded the value into the page, or whether JSP "wrote" it.

As such, either a scripting variable or a scoped variable can be used for this purpose.

You might want to read this article for further insights into this process.
[ August 05, 2006: Message edited by: Bear Bibeault ]
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. let's abandon JSP scriptlet, talking about JSTL, since there is no String concatnance syntax, how can I make the String variable concatenate another name String while it loops through the iteration ? Could you write out one or two lines using my example ? I guess I just don't know the syntax well enough..

If I can get this EL var changed while it loops through, then I can print it out in javascript "alert"..

thanks.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no concatenation operator in JSTL.
But you can quite easily concatenate things:

<c:set var="msg" value="${msg}${name},"/>

or if you want to be pedantic and have one c:out per variable:
<c:set var="msg"><c:out value="${msg}"/><c:out value="${name}"/>,</c:set>
Anything you put between the <c:set> tags becomes part of the concatenated string value.

Is this code only for debugging purposes? You do realise that the standard toString() of a List returns it in a [comma, separated, format] ?
Would that be usable for you?

The code I think you are wanting:




And it might be as simple as


Cheers,
evnafets

Edit: removed errant '}' in expressions (thanks Bear)
Confimed that it DOES work: Tested Tomcat 4.1.30, JSTL1.0.6

Test page (if anyone is interested)

[ August 06, 2006: Message edited by: Stefan Evans ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stefan Evans:
There is no concatenation operator in JSTL.
But you can quite easily concatenate things:

<c:set var="msg" value="${msg}${name},}"/>



I don't have a JSP 1.2 installation to test upon, but I'm not sure that that constrtuct would pass muster. If someone with a JSP 1.2 and JSTL 1.0 setup could try this out, it would be informative.

P.S. There's also an extra brace at the end that would need to be removed.
[ August 06, 2006: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stefan Evans:
<c:set var="msg"><c:out value="${msg}"/><c:out value="${name}"/>,</c:set>
Anything you put between the <c:set> tags becomes part of the concatenated string value.



For some reason, I don't use this construct much (setting the value of a <c:set> from its body), and I'm not exactly sure why -- it's very powerful.
 
Pay attention! Tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic