You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tiles.apache.org by MiB <di...@gmail.com> on 2012/01/22 23:20:21 UTC
change content in one Tile for one page only?
I need to change some content in my header tile for one page only,
linking in some special javascript. Do I have to make a new main
template just for this page, or is there some other way?
In several individual pages I need to change the id value of body,
which is in my main template. How do I go about this in Tiles?
I've been trough the documentation, but this has eluded me. Please
offer some starter points or thoughts.
/MiB
Re: change content in one Tile for one page only?
Posted by Antonio Petrelli <an...@gmail.com>.
2012/1/23 MiB <di...@gmail.com>
> So I'd like to be able to change 1 above to other values per the current
> view for the script attribute "src" or omit it totally and also in 2 above
> change the attribute value "id" in the body element. I suppose I could make
> an alternative tile and swap the this for the views where I need this, but
> then I'd need one for every page, which seems over the top and clumsy.
>
You can use Tiles attributes even as simple value holders. In your case,
think about having a list of scripts to load. In your template page do:
<snip>
<tiles:importAttribute name="scripts" />
<c:forEach var="scriptAttribute" items="${scripts}" />
<script src="${scriptAttribute.value}" type="text/javascript"></**script>
</c:forEach>
</snip>
And in your definition:
<definition name="...">
<put-list-attribute>
<add-attribute value="script1.js" />
<add-attribute value="script2.js" />
</put-list-attribute>
</definition>
See more here:
http://tiles.apache.org/2.2/framework/tutorial/advanced/list-attributes.html
About the id of the body, it is even simpler, simply use an attribute of
type "string" and import it in your layout page with
<tiles:importAttribute> and use it as a normal bean in page scope.
On 3 above I'd like to change the class attribute on list items based on
> the current view. I suppose it's a similar solution like for the previous I
> need here, and also in this case what I need to affect is inside a tile and
> as with the previous 1 I'd like to avoid making one big tile per view.
>
You can apply the same suggestions I gave you above. Moreover, you might be
interested in the View Preparer:
http://tiles.apache.org/2.2/framework/tutorial/advanced/preparer.html
HTH
Antonio
Re: change content in one Tile for one page only?
Posted by Nicolas LE BAS <ma...@nlebas.net>.
On 12-01-23 01:00 PM, MiB wrote:
>
> Yes, I'm using UrlBasedViewResolver this way:
> ***webmvc-config.xml***
> <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
> id="tilesViewResolver">
> <property name="viewClass"
> value="org.springframework.web.servlet.view.tiles2.TilesView"/>
> </bean>
>>
>> Once you know that, there are easy ways of putting the data into a
>> tiles attribute, and then you can use that attribute in your JSP with
>> <tiles:insertAttribute> or <tiles:importAttribute> as you like.
>
> While I can use attributes in my jspx files, my view resolver is only
> changing the main content tiles primary and secondary in my earlier
> sample and the needed changes are in other tiles, ie "header" and also
> within the head element which is only within my main template
> "default.jspx".
The TilesView just uses the spring-provided view name as the name of the
definition to be processed. So the "current view" as seen from Tiles is
the current definition.
With Tiles 2.1 and above, you can use wildcards to put a part of a
definition's name into an attibute:
http://tiles.apache.org/2.2/framework/tutorial/advanced/wildcard.html
By default, Spring uses the request path as the view name, so usually
people use it as the definition name, too. For instance you could use:
<definition name="default" template="/WEB-INF/layouts/default.jspx">
<put-attribute name="header" value="/WEB-INF/views/header.jspx" />
<put-attribute name="footer" value="/WEB-INF/views/footer.jspx" />
<!-- we need the "section" attribute, too, but just define it in
subclasses -->
</definition>
<definition name="/*/index" extends="default">
<put-attribute name="primary" value="/WEB-INF/views/primary.jspx" />
<put-attribute name="secondary"
value="/WEB-INF/views/secondary.jspx" />
<!-- whatever matches the * in the definition name -->
<put-attribute name="section" value="{1}"/>
</definition>
and then in your header.jspx (I'm assuming it's a navigation menu):
<!-- import the definition attribute into page scope for EL />
<tiles:importAttribute name="section" />
<ul id="navigation">
<li id="start" class="${section=='start'?'current':'menuentry'}"><a
href="/">Start</a></li>
<!-- More list items -->
</ul>
The <li> element receives the class 'current' in /start/index, but the
class 'menuentry' in /other/index.
Of course you can use another naming scheme if you prefer, like
"index-*" instead of "/*/index". It just has to fit whatever view names
you provide from the controller.
If you don't want to use EL expressions like
${section=='start'?'current':'menuentry'}, or if you need a more
complicated logic, you may use a ViewPreparer like Antonio said. Within
a preparer you can manipulate definition attributes and request
attributes as you want.
Hope this helps.
Nick
Re: change content in one Tile for one page only?
Posted by MiB <di...@gmail.com>.
23 jan 2012 kl. 15.57 Nicolas LE BAS asked:
> The critical piece of information I'm missing is: what is the
> "current view"? i.e. how are you calling Tiles? Do you get the
> "current view" concept from your own request attribute, or are you
> using something like spring's UrlBasedViewResolver?
Yes, I'm using UrlBasedViewResolver this way:
***webmvc-config.xml***
<bean
class="org.springframework.web.servlet.view.UrlBasedViewResolver"
id="tilesViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView
"/>
</bean>
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer
" id="tilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/layouts/layouts.xml</value>
<value>/WEB-INF/views/**/views.xml</value>
</list>
</property>
</bean>
***
>
> Once you know that, there are easy ways of putting the data into a
> tiles attribute, and then you can use that attribute in your JSP
> with <tiles:insertAttribute> or <tiles:importAttribute> as you like.
While I can use attributes in my jspx files, my view resolver is only
changing the main content tiles primary and secondary in my earlier
sample and the needed changes are in other tiles, ie "header" and also
within the head element which is only within my main template
"default.jspx".
/MiB
Re: change content in one Tile for one page only?
Posted by Nicolas LE BAS <ma...@nlebas.net>.
Hi!
On 12-01-23 07:26 AM, MiB wrote:
> An additional question related to the previous:
>
> I have my
> ***header.jspx:***
> <div id="header" xmlns:jsp="http://java.sun.com/JSP/Page"
> xmlns:fn="http://java.sun.com/jsp/jstl/functions"
> xmlns:c="http://java.sun.com/jsp/jstl/core"
> xmlns:spring="http://www.springframework.org/tags" version="2.0">
>
> <!--jsp output and directive -->
> <h1>Main Headline</h1>
> <ul id="navigation">
> <!--3. I'D LIKE TO CHANGE THE CLASS FOR SOME LIST ITEMS BELOW BASED ON
> THE CURRENT VIEW -->
> <li id="start" class"current"><a href="/">Start</a></li>
> <!-- More list items without class attribute -->
> </ul>
> </div>
> ***
>
> On 3 above I'd like to change the class attribute on list items based on
> the current view. I suppose it's a similar solution like for the
> previous I need here, and also in this case what I need to affect is
> inside a tile and as with the previous 1 I'd like to avoid making one
> big tile per view.
The critical piece of information I'm missing is: what is the "current
view"? i.e. how are you calling Tiles? Do you get the "current view"
concept from your own request attribute, or are you using something like
spring's UrlBasedViewResolver?
Once you know that, there are easy ways of putting the data into a tiles
attribute, and then you can use that attribute in your JSP with
<tiles:insertAttribute> or <tiles:importAttribute> as you like.
Nick
Re: change content in one Tile for one page only?
Posted by MiB <di...@gmail.com>.
23 jan 2012 kl. 09.19 skrev Antonio Petrelli:
>>
>> I need to change some content in my header tile for one page only,
>> linking
>> in some special javascript. Do I have to make a new main template
>> just for
>> this page, or is there some other way?
>>
>> In several individual pages I need to change the id value of body,
>> which
>> is in my main template. How do I go about this in Tiles?
>>
>> I've been trough the documentation, but this has eluded me. Please
>> offer
>> some starter points or thoughts.
>
>
> Can you explain with an example what are you trying to accomplish?
> You are
> too vague.
Thanks for your response Antonio.
My Tiles setup is
***layout.xml:***
<definition name="default" template="/WEB-INF/layouts/default.jspx">
<put-attribute name="header" value="/WEB-INF/views/header.jspx" />
<put-attribute name="footer" value="/WEB-INF/views/footer.jspx" />
</definition>
****
and typically I put specific content in:
*** view.xml:***
<definition name="index" extends="default">
<put-attribute name="primary" value="/WEB-INF/views/
primary.jspx" />
<put-attribute name="secondary" value="/WEB-INF/views/
secondary.jspx" />
</definition>
***
with a main template
***default.jspx:***
"<html xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core
"
xmlns:tiles="http://tiles.apache.org/tags-tiles" xmlns:spring ="http://www.springframework.org/tags
"
xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" lang="se">
<!-- jsp output and directive here-->
<head>
<!--meta here-->
<link rel="stylesheet" href="styles/layout.css"></link>
<!--1. THESE ARE WHAT I WANT TO CHANGE FOR SOME VIEWS: -->
<script src="build/mediaelement-and-player.js" type="text/
javascript"></script>
<script src="build/jquery.js" type="text/javascript"></script>
<!-- message code for title here-->
</head>
<!--2. HERE I WOULD LIKE TO CHANGE PER VIEW THE VALUE OF THE "id"
ATTRIBUTE OF BODY -->
<body id="a_value">
<div id="page">
<tiles:insertAttribute name="header"/>
<div id="container" class="top">
<div id="main_content" class="page_content">
<tiles:insertAttribute name="primary"/>
<tiles:insertAttribute name="secondary" ignore="true"/>
</div>
</div>
<tiles:insertAttribute name="footer"/>
</div>
</body></html>
***
So I'd like to be able to change 1 above to other values per the
current view for the script attribute "src" or omit it totally and
also in 2 above change the attribute value "id" in the body element. I
suppose I could make an alternative tile and swap the this for the
views where I need this, but then I'd need one for every page, which
seems over the top and clumsy.
I suspect I need inheritance and/or nested definitions, but I don't
understand exactly how to implement this for the above. Even better
would be if it's was possible to go even closer to the details I need
to affect and change the attribute values at some places based on the
current view. I don't know of this is possible, but this is what I'm
looking for. Any suggestions are welcome.
An additional question related to the previous:
I have my
***header.jspx:***
<div id="header" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:fn="http://java.sun.com/jsp/jstl/functions
"
xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:spring="http://www.springframework.org/tags
" version="2.0">
<!--jsp output and directive -->
<h1>Main Headline</h1>
<ul id="navigation">
<!--3. I'D LIKE TO CHANGE THE CLASS FOR SOME LIST ITEMS BELOW BASED ON
THE CURRENT VIEW -->
<li id="start" class"current"><a href="/">Start</a></li>
<!-- More list items without class attribute -->
</ul>
</div>
***
On 3 above I'd like to change the class attribute on list items based
on the current view. I suppose it's a similar solution like for the
previous I need here, and also in this case what I need to affect is
inside a tile and as with the previous 1 I'd like to avoid making one
big tile per view.
I have a feeling Tiles is more versatile than I have understood so far
and this is why I ask for some guidance or suggestions.
Thank you!
/MiB
Re: change content in one Tile for one page only?
Posted by Antonio Petrelli <an...@gmail.com>.
2012/1/22 MiB <di...@gmail.com>
> I need to change some content in my header tile for one page only, linking
> in some special javascript. Do I have to make a new main template just for
> this page, or is there some other way?
>
> In several individual pages I need to change the id value of body, which
> is in my main template. How do I go about this in Tiles?
>
> I've been trough the documentation, but this has eluded me. Please offer
> some starter points or thoughts.
Can you explain with an example what are you trying to accomplish? You are
too vague.
Antonio