Tiles, how to use a dynamic collection variable in request scope in a generic page?

C

conor.macmahon

Hi there,
After reading the tiles documentation, I'm a little confused
how I can elegantly solve my problem. I have a generic page, that
iterates through a collection, for example:

<c:forEach var="item" items="${requestScope.items1/items2/items3}"
begin="0">
</c:forEach>

The issue I have is how to configure tiles so that the generic page can
switch between items1/items2/items3 depending on a given tiles
definition? How would I do this? Note it all works if I iterate through
a given hard-coded variable, just need now to make that variable
dynamic?

Any help with the above would be appreciated, I'm sure I'm not the only
one to have to solve this problem using tiles?
Best regards,
Conor
 
T

Tim B

Hi there,
After reading the tiles documentation, I'm a little confused
how I can elegantly solve my problem. I have a generic page, that
iterates through a collection, for example:

<c:forEach var="item" items="${requestScope.items1/items2/items3}"
begin="0">
</c:forEach>

The issue I have is how to configure tiles so that the generic page
can switch between items1/items2/items3 depending on a given tiles
definition? How would I do this? Note it all works if I iterate
through a given hard-coded variable, just need now to make that
variable dynamic?

Any help with the above would be appreciated, I'm sure I'm not the
only one to have to solve this problem using tiles?
Best regards,
Conor

here's a way to do it:
in your tiles-defs.xml file (or equivalent)
within the definitions:
<put name="myCollection" value="item1"/>
<put name="myCollection" value="item2"/>
etc.

in your jsp (each one or a base jsp for the tile):

<tiles:useAttribute name="myCollection" scope="request"/>

then in each jsp:

<% String myCollection = request.getAttribute("myCollection"); %>

Then either do some logic based on the value of myCollection or,
depending on how you've set it up, just use an expression to
write out myCollection where it will be used

example in struts, which I'm more familiar with than JSTL:

<logic:iterate id="eachOne" name="<%=myCollection%>">
</logic:iterate>

this might work for JSTL:

<c:forEach var="item" items="${requestScope.<%=myCollection%>}"
begin="0">
</c:forEach>
 
C

conor.macmahon

Hi Tim,
Thanks for that suggestion. However, I don't think I can use
this way of working as the items in the collection in tiles-defs, as
in:

<put name="myCollection" value="item1"/>
<put name="myCollection" value="item2"/>

are fixed. However, I need items in myCollection to be dynamic, not
static. Hence, I do not know in advance how many items to place in the
myCollection object, so again cannot define this in the tiles-defs xml
config file.

Suspect there's an equivalent way to do this programmatically, but I
still have to find out? Thanks though Tim, hope I've explained myself.
Is there a lateral solution I'm not seeing here, anyone else have any
suggestions, I'm surely not the only one to have this requirement?
Best,
Conor
 
T

Tim B

Hi Tim,
Thanks for that suggestion. However, I don't think I can use
this way of working as the items in the collection in tiles-defs, as
in:

<put name="myCollection" value="item1"/>
<put name="myCollection" value="item2"/>

are fixed. However, I need items in myCollection to be dynamic, not
static. Hence, I do not know in advance how many items to place in the
myCollection object, so again cannot define this in the tiles-defs xml
config file.

Suspect there's an equivalent way to do this programmatically, but I
still have to find out? Thanks though Tim, hope I've explained myself.
Is there a lateral solution I'm not seeing here, anyone else have any
suggestions, I'm surely not the only one to have this requirement?
Best,
Conor


Ok, just to make sure I've understood properly what you are looking
for. You have a page that is defined in a tiles definition. You want
to have further tiles definitions using the same layout jsps but you
want these other pages to each access a different collection by
retrieving it from the request or session, perhaps to display a list
of options or some such. Is this correct? If this is right, then you
would use the <put name="myCollection" value="itemnumber"/> in
each definition, as above. You would then use the unique 'itemnumber'
value to retrieve the corresponding collection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top