You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tiles.apache.org by Dan Woodroffe <da...@gmail.com> on 2012/01/23 23:19:59 UTC
Help propagating attribute from view to templates
Hi, I hoped somebody could help me, I think what I'm asking is pretty
basic but I haven't been able to get this configured correctly. I'd
like to prefix this question by saying I do not know Java, I'm a front
end dev working with an overstretched Java team.
We've encountered problems with Sitemesh and I'm attempting to replace
our dependancy on it with Tiles, as it looks far more useful. I've
followed the tutorials but cannot work out how to override attributes
on a per page basis.
I'm not sure of the correct terminology, but /location on our website
relates to /WEB-INF/jsp/location.jsp
This is what I have, I'm using Tiles 2.2, we've enabled MutableTileContainers:
tiles-defs.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<definition name="base.definition" template="/WEB-INF/jsp/layout.jsp">
<put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
<put-attribute name="body" value="" />
</definition>
<definition name="location" extends="base.definition">
<put-attribute name="body" value="/WEB-INF/jsp/location.jsp" />
</definition>
layout.jsp
...
<tiles:putAttribute name="title" cascade="true" value="Layout Title" />
<tiles:insertAttribute name="header" />
<tiles:insertAttribute name="body" />
...
header.jsp
<tiles:getAsString name="title" />
location.jsp
...
<tiles:putAttribute name="title" cascade="true" value="Location Title" />
...
This displays 'Layout Title' as expected, however the presence of
<putAttribute name="title" ... /> in 'layout.jsp is purely to
illustrate my problem.. I wish to populate the 'title' attribute
within location.jsp. When I remove the putAttribute from layout,
expecting to see the title specified in 'location.jsp' to appear, the
application throws a 500 along the lines of 'cannot find attribute
title'.
I've verified that location.jsp is loading, but it seems the syntax I
am using is incorrect.
I'm fully aware that I could place this title in the xml configuration
for each page type, but there are multiple types of data I need to
perpetuate through from location.jsp to the layout.jsp (and
header.jsp), for example we have an in house jawr wrapper to achieve
deferred javascript load and execution that references different files
on a per page basis.
So, my question is, given the setup above, what is the syntax for
creating the title attribute in 'location.jsp' so that it is available
in 'header.jsp'
Many thanks for reading, hope you can help!
--
Email - dan.woodroffe@gmail.com
Telephone [UK] - +44 (0)208 1230888
Skype - danwoodroffe
MSN - dan.woodroffe@gmail.com
Re: Help propagating attribute from view to templates
Posted by Nicolas LE BAS <ma...@nlebas.net>.
On 12-02-07 07:27 PM, Dan Woodroffe wrote:
>>
>> A pleasure :)
>>
>> Please tell us when you've solved your problem if tiles helped!
>>
>> Nick
>>
>
> I'm a convert :-D once I started barking up the correct tree and got
> little help configuring the system to enable proper usage of the
> expression attribute rather than trying to override attributes from the
> view jsp we flew through migrating from Sitemesh to Tiles. Now we are able
> to share page fragments across multiple applications using Tiles in
> combination with overlays, the front end codebase is so much more
> maintainable and extendable. Big thumbs up!
>
Good idea! I've been advising people to use the web-fragment facility
from JavaEE 6 to share JSPs across webapps, but Maven overlays work just
as well.
Thanks for sharing it with us
Nick
Re: Help propagating attribute from view to templates
Posted by Dan Woodroffe <da...@gmail.com>.
>
> A pleasure :)
>
> Please tell us when you've solved your problem if tiles helped!
>
> Nick
>
I'm a convert :-D once I started barking up the correct tree and got
little help configuring the system to enable proper usage of the
expression attribute rather than trying to override attributes from the
view jsp we flew through migrating from Sitemesh to Tiles. Now we are able
to share page fragments across multiple applications using Tiles in
combination with overlays, the front end codebase is so much more
maintainable and extendable. Big thumbs up!
Re: Help propagating attribute from view to templates
Posted by Nicolas LE BAS <ma...@nlebas.net>.
On 12-01-24 02:23 PM, Dan Woodroffe wrote:
> Many thanks for the detailed response, it's stopped me from throwing
> more hours away barking up entirely the wrong tree!
A pleasure :)
Please tell us when you've solved your problem if tiles helped!
Nick
Re: Help propagating attribute from view to templates
Posted by Dan Woodroffe <da...@gmail.com>.
That makes a lot of sense, I think I was viewing Tiles more as an
extended Sitemesh than as an alternative approach. The example you
provided of regarding a method for preparing a list of scripts in the
xml file for jawr makes it obvious to me now that i don't need to be
preparing them inside location.jsp at all.
Many thanks for the detailed response, it's stopped me from throwing
more hours away barking up entirely the wrong tree!
Dan
Re: Help propagating attribute from view to templates
Posted by Nicolas LE BAS <ma...@nlebas.net>.
Hi!
On 12-01-23 05:19 PM, Dan Woodroffe wrote:
> I've verified that location.jsp is loading, but it seems the syntax I
> am using is incorrect.
The syntax is correct, it's just that location.jsp happens too late to
change anything in header.jsp.
To keep it simple, this is what happens when you render the definition
"location":
- Tiles looks for all declared attributes:
* header = /WEB-INF/jsp/header.jsp (inherited from base.definition)
* body = /WEB-INF/jsp/location.jsp (overriden)
- Tiles looks for the template of the definition (inherited from
base.definition)
- Tiles renders the template /WEB-INF/jsp/layout.jsp
* layout.jsp adds a new attribute "title" = Layout title
* layout.jsp calls the attribute header (/WEB-INF/jsp/header.jsp).
# header.jsp calls the attribute "title" at this time
* layout.jsp calls the attribute body (/WEB-INF/jsp/location.jsp)
# location.jsp adds a new attribute "title", but this is too late
to be used in the header.
> I'm fully aware that I could place this title in the xml configuration
> for each page type, but there are multiple types of data I need to
> perpetuate through from location.jsp to the layout.jsp (and
> header.jsp), for example we have an in house jawr wrapper to achieve
> deferred javascript load and execution that references different files
> on a per page basis.
Well, anything you do in location.jsp will never be seen in header.jsp,
sorry.
I'm not sure exactly why placing the title in the xml files is not an
option for you.
My guess is you've designed location.jsp to work with sitemesh:
location.jsp that produces all page-specific content, and then sitemesh
is transforming parts of it based on its configured rules. The jawr
wrapper would be to support jawr from sitemesh (googling for "sitemesh
site:jawr.java.net" gives nothing, so that would explain why it's an
in-house module).
Tiles works very differently from sitemesh. Instead of transforming an
existing page, it builds the page step by step. That means you assemble
several JSPs into a single page, and attributes should be known
beforehand. For instance, you may need to write the attribute "title" in
/html/head/title (from head.jsp) and again in /html/body/div[id='title']
(from layout.jsp). That's why we usually place the attributes in the xml
configuration.
I don't know if I've guessed right, or if my explanation is clear enough
or even that helps you at all. I'm sure tiles provides an elegant
solution for what you're trying to achieve (and probably so does
sitemesh in a different way), but I'm guessing a lot here.
Borrowing from what Antonio said earlier today, I'll close this message
with an example of how jawr could work with Tiles without the need of in
house dev:
tiles-def.xml
<definition name="base" template="...">
<put-attribute name="title" value="Layout title" />
<put-list-attribute name="scripts">
<!-- the scripts that everybody needs -->
<add-attribute value="script1.js" />
<add-attribute value="script2.js" />
</put-list-attribute>
</definition>
<definition name="location" extends="base">
<put-attribute name="title" value="Location title" />
<put-list-attribute name="scripts" inherit="true">
<!-- add a new script for this page -->
<add-attribute value="script_location.js" />
</put-list-attribute>
</definition>
header.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://jawr.net/tags" prefix="jwr" %>
<tiles:importAttribute name="scripts" />
<head>
<c:forEach var="scr" items="${scripts}">
<jwr:script src="${scr.value}" />
</c:forEach>
</head>
I hope I've helped you see a bit further through the fog...
Nick