You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tiles.apache.org by "Motley, Jeffery" <Je...@Williams.com> on 2010/07/27 20:54:29 UTC

Apache Tiles 2 and javascript and css

What is the appropriate place/way to define javascipt/css for a jsp that
is using a Tiles definition.? I would like each custom page to use the
master layout but override the body/content section. However I need
custome javascript/css for each page. For example how do I call
javascript for body onLoad?

 

Thanks for any help.

Jeff


Re: Apache Tiles 2 and JavaScript and css

Posted by Antonio Petrelli <an...@gmail.com>.
2010/7/28 Motley, Jeffery <Je...@williams.com>:
> This solution requires me to place additional definitions in the xml
> file for every page I have, since they all would have some kind of
> unique JavaScript. I would then use the extended definition in my custom
> page. Is there any way to not define the new JavaScript in the xml
> config file and to include it in the custom jsp that uses the master
> layout either by including a custom JavaScript file or have custom
> JavaScript inline in the custom jsp?

To not define a plethora of definitions, you can use a ViewPreparer,
and override an attribute following a rule (if you can find one):
http://tiles.apache.org/framework/tutorial/advanced/preparer.html
For an inline Javascript, you can use <tiles:insert> as usual with an
attribute of "template" type.

> For instance, in all of the examples I see the body tag is in the
> master_layout.jsp. I want to define the body tag in my custom.jsp that
> uses the master layout because I might want to call some custom
> JavaScript at body onLoad. I hope this makes sense?

In this particular case I think that using unobtrusive Javascript,
that modifies onload of <body> element when loaded, is the best
approach. With things like jQuery or Prototype it's very easy:
http://www.prototypejs.org/api/event/observe

Antonio

RE: Apache Tiles 2 and JavaScript and css

Posted by "Motley, Jeffery" <Je...@Williams.com>.
Thank you Antonio. This is great stuff. I do have one follow up question
to this however.

This solution requires me to place additional definitions in the xml
file for every page I have, since they all would have some kind of
unique JavaScript. I would then use the extended definition in my custom
page. Is there any way to not define the new JavaScript in the xml
config file and to include it in the custom jsp that uses the master
layout either by including a custom JavaScript file or have custom
JavaScript inline in the custom jsp? I would prefer to not have a lot of
xml in the tiles xml file. I am only trying to use the reuse the
definitions for header menu footer in the custom jsps I create. I would
like to override the JavaScript and content in the custom jsp files I
create. 

For instance, in all of the examples I see the body tag is in the
master_layout.jsp. I want to define the body tag in my custom.jsp that
uses the master layout because I might want to call some custom
JavaScript at body onLoad. I hope this makes sense?

Thanks again for any help. I am just trying to put the pieces together
in my head. I am creating a new app.
Jeff

-----Original Message-----
From: Antonio Petrelli [mailto:antonio.petrelli@gmail.com] 
Sent: Wednesday, July 28, 2010 2:37 AM
To: users@tiles.apache.org
Subject: Re: Apache Tiles 2 and javascript and css

2010/7/27 Motley, Jeffery <Je...@williams.com>:
> What is the appropriate place/way to define javascipt/css for a jsp
that
> is using a Tiles definition.? I would like each custom page to use the
> master layout but override the body/content section. However I need
> custome javascript/css for each page.

Use string attributes. For example:

<definition name="master" template="/jsp/master_layout.jsp">
  <put-attribute name="css" value="/css/default.css" type="string" />
  <put-attribute name="script" value="/js/default.js" type="string" />
</definition>

<definition name="extended" extends="master">
  <put-attribute name="css" value="/css/overridden.css" type="string" />
  <put-attribute name="script" value="/js/overridden.js" type="string"
/>
</definition>

And, in master_layout.jsp:

...
<head>
  <tiles:importAttribute name="css" />
  <link href="${css}" rel="stylesheet" type="text/css" />
  <tiles:importAttribute name="script" />
  <script type="text/javascript" src="${script}"></script>
</head>

HTH
Antonio

Re: Apache Tiles 2 and javascript and css

Posted by Antonio Petrelli <an...@gmail.com>.
2010/7/27 Motley, Jeffery <Je...@williams.com>:
> What is the appropriate place/way to define javascipt/css for a jsp that
> is using a Tiles definition.? I would like each custom page to use the
> master layout but override the body/content section. However I need
> custome javascript/css for each page.

Use string attributes. For example:

<definition name="master" template="/jsp/master_layout.jsp">
  <put-attribute name="css" value="/css/default.css" type="string" />
  <put-attribute name="script" value="/js/default.js" type="string" />
</definition>

<definition name="extended" extends="master">
  <put-attribute name="css" value="/css/overridden.css" type="string" />
  <put-attribute name="script" value="/js/overridden.js" type="string" />
</definition>

And, in master_layout.jsp:

...
<head>
  <tiles:importAttribute name="css" />
  <link href="${css}" rel="stylesheet" type="text/css" />
  <tiles:importAttribute name="script" />
  <script type="text/javascript" src="${script}"></script>
</head>

HTH
Antonio