Struts - Tiles Question

W

WJ

I've worked with struts a bit and am relatively new to Tiles.

I've created a tile page that has a layout of a header and body, and need to
pass a parameter to the header
to change it's behavior.

I have a simple layout page, called layout.jsp, like this:

<!--included taglibs omitted -->

<html>
<head>
<title><tiles:getAsString name="title" /></title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td>
<tiles:insert attribute="header" />
</td>
</tr>
<tr>
<td>
<tiles:insert attribute="body" />
</td>
</tr>
</table>
</body>
</html>

I have a Tile page called tile.jsp, like this:

<%@ taglib uri="struts-tiles.tld" prefix="tiles" %>

<tiles:insert page="layout.jsp" flush="true">
<tiles:put name="header_type" content="1"/>
<tiles:put name="title" value="My Test Tile" />
<tiles:put name="header" value="header.jsp" />
<tiles:put name="body" value="body.jsp" />
</tiles:insert>

In my header.jsp, I have one relevant line:

<tr><td>==><tiles:getAsString name="header_type" /><==</td></tr>

body.jsp doesn't matter.

I expect to see at the top of my page ==>1<==. Everything works fine,
except, I get
==><== (with no '1'). In fact,
if I don't put an igore="true" I get an error (because the attribute was not
found).

How do I pass a value along the Tiles Context to the header.jsp?

I've also tried using tiles inside the body.jsp, like this:

<tr>
<td>
<tiles:insert page="header.jsp">
<tiles:put name="title" value="Test from body.jsp" />
<tiles:put name="header_type" value="1" />
</tiles:insert>
</td>
</tr>
 
J

Java Architect

[SNIP]
How do I pass a value along the Tiles Context to the header.jsp?

I think you already figured it out, but, just to make sure: the reason for
the error you reported is that, 'header_type' is in the component scope of
layout.jsp, but that scope does not extend into the scope of header.jsp. If
you want access to it in You have to pass along that info, you have to do it
using a put tag.

My solution would be to change the relevant portion of layout.jsp to be:

<tiles:insert attribute="header">
<tiles:put name="header_type" beanName="header_type"/>
</tiles:insert>

I'm don't want to waste your time, so if you don't need it spelled out,
please ignore this paragraph. The put looks redundant, but what it's saying
is put an attribute called 'header_type' into the context of 'header', and
set the value of that attribute to whatever is stored in the current
context's value of 'header_type'.
I've also tried using tiles inside the body.jsp, like this:

<tr>
<td>
<tiles:insert page="header.jsp">
<tiles:put name="title" value="Test from body.jsp" />
<tiles:put name="header_type" value="1" />
</tiles:insert>
</td>
</tr>

When I do this, I get a table that looks something like:

--------------
| ==> <== |
--------------
| ==> 1 <== |
--------------

This makes sense. In the first insert, the value of header_type is unknown,
hence the getAsString ignores it and nothing is printed. In the second
insert, the value is 1, so it gets printed. Remember that each 'insert' is
it's own tile, and the component context for each is independent.

Hope this helps.
 
W

WJ

I've also tried using tiles inside the body.jsp, like this:
When I do this, I get a table that looks something like:

--------------
| ==> <== |
--------------
| ==> 1 <== |
--------------

This makes sense. In the first insert, the value of header_type is unknown,
hence the getAsString ignores it and nothing is printed. In the second
insert, the value is 1, so it gets printed. Remember that each 'insert' is
it's own tile, and the component context for each is independent.

Hope this helps.


Thanks for the feedback. I'll try the change to the tile and layout. With
the example above,
I was not able to get this to work. I never got "1" in the header.jsp,
although my insert syntax
seems correct.
 
W

WJ

Found the issue. In the header.jsp, I wasn't importing the attribute with
useAttribute. Now it all works great.

Thanks for the explanation, too. That helped.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top