apache tiles - how to override an attribute from child template

旧城冷巷雨未停 提交于 2019-12-11 11:42:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!