aspose file tools*
The moose likes JSP and the fly likes questions about JSTL variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "questions about JSTL variables" Watch "questions about JSTL variables" New topic
Author

questions about JSTL variables

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
<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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

As per forum instructions, the versions of JSP and JSTL that you are using is needed to answer your question.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

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

Joined: Jun 30, 2006
Posts: 316
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

Joined: Jun 30, 2006
Posts: 316
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

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

Joined: Jun 30, 2006
Posts: 316
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.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
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 cut per variable:
<c:set var="msg"><cut value="${msg}"/><cut 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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Originally posted by Stefan Evans:
<c:set var="msg"><cut value="${msg}"/><cut 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.
 
wood burning stoves
 
subject: questions about JSTL variables
 
Similar Threads
forEach nested loop
Mixing JSTL and JSP?
SQL Poll Question in JSP
Setting an object with c:set from sessionScope
Nested C:forEach varStatus ??