问题
The question is how can I override an attribute in tiles child template.
I've got two templates: base and child. This is a part of base layout - HTML head:
<title>
<tiles:insertAttribute name="title" />
lyricsBase: <c:out value="${jukebox.name}" />
</title>
This is my tiles.xml:
<definition name="t.base" template="/WEB-INF/tiles/base.jsp">
<put-attribute name="title" value="SomeTitle"/>
</definition>
[...]
<definition name="t.song" extends="t.base">
<put-attribute name="body" value="/WEB-INF/jsp/song.jsp"/>
<put-attribute name="title" value="song.title"/>
</definition>
When I run my page, I get following HTML title: song.title lyricsBase: xxx
. What code should I put into the child view to override the title
attribute? I'm trying to make it ${song.title}, for example
<tiles:putAttribute name="title" value="${song.title}" />
...but it doesn't work. Thanks for any help!
回答1:
You should not be using the value attribute but the expression attribute. In tiles3 I was having an issue with string values for put attributes without cascade being true... This assumes tiles3 (although most is applicable to most of tiles 2).
Following is untested:
<definition name="t.base" template="/WEB-INF/tiles/base.jsp">
<put-attribute name="title" cascade="true" value="SomeTitle"/>
</definition>
[...]
<definition name="t.song" extends="t.base">
<put-attribute name="body" value="/WEB-INF/jsp/song.jsp"/>
<put-attribute name="title" cascade="true" expression="EL:song.title"/>
</definition>
Then <tiles:insertAttribute name="title" />
should work as expected in your template.
来源:https://stackoverflow.com/questions/14884304/apache-tiles-how-to-override-an-attribute-from-child-template