• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing scriptlet variable using JSTL?  RSS feed

 
Sverre Moe
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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 Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!