Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Accessing scriptlet variable using JSTL?

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should not the following be possible?

<%
String[] dataSources = {"DS1","DS2","DS3","DS4"};
%>
..

And then access the scriptlet variable using ${dataSources} ?
Somehow the ${dataSources} produces notting

I need to use this variable in a <c:forEach items="${dataSources}" var="dataSource"></c:forEach>, but it seems there is notting in this variable

I think I may be using JSTL 1.2 as I am getting the taglib using uri="http://java.sun.com/jsp/jstl/core"

The web container is a WebSphere 6.1 and Servlet API is 2.4
[ July 17, 2007: Message edited by: Sverre Moe ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSTL and EL work with scoped variables.
They are meant to replace scriptlets and are not designed to work together.

Why are you trying to use both in the same page?
 
Sverre Moe
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
JSTL and EL work with scoped variables.
They are meant to replace scriptlets and are not designed to work together.

Why are you trying to use both in the same page?



Coz I am trying to replace this code


With this code


So I have created an array of String containing the list. The ${dataSources} should be the variable
String[] dataSources = {"DS1","DS2","DS3","DS4"}; that I have created in a scriptlet

Edit: I had to solve this problem by moving the String[] dataSources to my DataSourceUtil and make a getter for it. Then I created a bean like this
<jsp:useBean id="dataSourceUtil" class="DataSourceUtil"/>

Then I access the array using ${dataSourceUtil.dataSources}

[ July 17, 2007: Message edited by: Sverre Moe ]
[ July 17, 2007: Message edited by: Bear Bibeault ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than hard code your list of data source names in the JSPs, why not bind them to application scope?
Then you would be able to access the array using JSTL and EL (which is the proper way to write a scriptless JSP) and, if you want to make the list configurable later, you won't have to change anything except the contextListener that creates and binds the list to scope.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sverre Moe:


Edit: I had to solve this problem by moving the String[] dataSources to my DataSourceUtil and make a getter for it. Then I created a bean like this
<jsp:useBean id="dataSourceUtil" class="DataSourceUtil"/>

Then I access the array using ${dataSourceUtil.dataSources}

[ July 17, 2007: Message edited by: Sverre Moe ]



Saw this after my last post.
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic