tiles的cascade属性

参考一:

http://www.voidcn.com/article/p-uwpbtzyd-btr.html

我正在使用Tiles,我想为一个页面定义的属性在一个包含的子页面中可读,如下所示:

 

瓷砖-definitions.xml中:

 

<definition name="page" template="/WEB-INF/tiles/layout/page.jsp">
  <put-attribute name="header" value="/WEB-INF/jsp/_include/header.jsp"/>
  <put-attribute name="footer" value="/WEB-INF/jsp/_include/footer.jsp"/>
</definition>

<definition name="tutorial" extends="page">
  <put-attribute name="title" value="Tutorial"/>
  <put-attribute name="body" value="/WEB-INF/jsp/tutorial.jsp"/>
</definition>

page.jsp表示一个简单的页面结构:

 

<html>
  <head><title><tiles:getAsString name="title"/></title></head>
  <body>
      <tiles:insertAttribute name="header"/>
      <tiles:insertAttribute name="body"/>
      <tiles:insertAttribute name="footer"/>
  </body>
</html>

我试图在“标题”子页面中读取标题,但是我收到以下错误:

 

org.apache.tiles.template.NoSuchAttributeException: Attribute ‘title’ not found.

这是我如何尝试访问header.jsp中的属性:

 

<tiles:getAsString name="title"/>

要将title属性传播到内部模板,您需要将cascade =“true”属性添加到标题的< put-attribute>标签,像这样:

 

 

<definition name="tutorial" extends="page">
  <put-attribute name="title" cascade="true" value="Tutorial"/>
  <put-attribute name="body" value="/WEB-INF/jsp/tutorial.jsp"/>
</definition>

参考二:

https://stackoverflow.com/questions/15833837/apache-tiles-2-2-2-propagating-values

Viewed 483 times

 

0

 

<tiles-definitions>
    <definition name="home" template="/WEB-INF/views/home.jsp">
        <put-attribute name="title"       value="My App" />
        <put-attribute name="header"      value="/WEB-INF/views/common/header.jsp" />
        <put-attribute name="menu"        value="/WEB-INF/views/common/nav.jsp" />
        <put-attribute name="footer"      value="/WEB-INF/views/common/footer.jsp" />
    </definition>
</tiles-definitions>

How do I propagate the title value (My App) to header.jsp? In header jsp, when I do:

<tiles:insertAttribute name="title" ignore="true" />

nothing is printed. When I do the same in home.jsp, My App is printed. cascaded=true has not helped.

spring spring-mvc apache-tiles

shareimprove this questionfollow

asked Apr 5 '13 at 12:20

user504674

add a comment

1 Answer

ActiveOldestVotes

0

 

It's not clear where you tried putting that cascade=true (assuming it's a typo in your question: it should be "cascade", not "cascaded") but the following works as expected:

tiles.xml:

<tiles-definitions>
    <definition name="home" template="/WEB-INF/views/home.jsp">
        <put-attribute name="title"   value="My App" cascade="true"/>
        <put-attribute name="header"  value="/WEB-INF/views/common/header.jsp" />
        (...)
    </definition>
</tiles-definitions>

home.jsp:

    Title: <tiles:insertAttribute name="title" /> <br/>
    Header: <tiles:insertAttribute name="header" />

header.jsp:

Title in header: <tiles:insertAttribute name="title" />

The output is:

Title: My App
Header: Title in header: My App

 

 

参考三:

https://tiles.apache.org/framework/tutorial/advanced/nesting-extending.html

Nesting Definitions

Sometimes it is useful to have a structured page with, say, a structured body. Typically, there is a main layout (for example, the "classic" layout) and the body is made of certain number of sections. In this case, nesting a definition (the one for the body) inside another definition (the main layout) can be useful.

Named subdefinitions

Tiles supports nesting definitions natively. One way of using nested definitions is creating a named "subdefinition" and using it as an attribute. For example:

<definition name="myapp.homepage.body" template="/layouts/three_rows.jsp">
  <put-attribute name="one" value="/tiles/headlines.jsp" />
  <put-attribute name="two" value="/tiles/topics.jsp" />
  <put-attribute name="one" value="/tiles/comments.jsp" />
</definition>

<definition name="myapp.homepage" template="/layouts/classic.jsp">
  <put-attribute name="title" value="Tiles tutorial homepage" />
  <put-attribute name="header" value="/tiles/banner.jsp" />
  <put-attribute name="menu" value="/tiles/common_menu.jsp" />
  <put-attribute name="body" value="myapp.homepage.body" />
  <put-attribute name="footer" value="/tiles/credits.jsp" />
</definition>

The myapp.homepage.body definition will be put inside the myapp.homepage, by putting it inside its body attribute. You will be seeing the definition one inside the other.

Anonymous nested definitions

What you can do with named subdefinitions can be done with nested anonymous definitions. The above example can be rewritten in:

<definition name="myapp.homepage.body" template="/layouts/three_rows.jsp">
  <put-attribute name="one" value="/tiles/headlines.jsp" />
  <put-attribute name="two" value="/tiles/topics.jsp" />
  <put-attribute name="one" value="/tiles/comments.jsp" />
</definition>

<definition name="myapp.homepage" template="/layouts/classic.jsp">
  <put-attribute name="title" value="Tiles tutorial homepage" />
  <put-attribute name="header" value="/tiles/banner.jsp" />
  <put-attribute name="menu" value="/tiles/common_menu.jsp" />
  <put-attribute name="body">
    <definition template="/layouts/three_rows.jsp">
      <put-attribute name="one" value="/tiles/headlines.jsp" />
      <put-attribute name="two" value="/tiles/topics.jsp" />
      <put-attribute name="one" value="/tiles/comments.jsp" />
    </definition>
  </put-attribute>
  <put-attribute name="footer" value="/tiles/credits.jsp" />
</definition>

The anonymous definition put under the "body" attribute can be used only by the surrounding definition. Moreover, you can nest a definition into a nested definition, with the desired level of depth.

Cascaded attributes

Attributes defined into a definition can be cascaded to be available to all nested definitions and templates. For example the sample definition detailed above can be rewritten this way:

<definition name="myapp.homepage" template="/layouts/classic.jsp">
  <put-attribute name="title" value="Tiles tutorial homepage" />
  <put-attribute name="header" value="/tiles/banner.jsp" />
  <put-attribute name="menu" value="/tiles/common_menu.jsp" />
  <put-attribute name="body" value="/layouts/three_rows.jsp" />
  <put-attribute name="footer" value="/tiles/credits.jsp" />

  <put-attribute name="one" value="/tiles/headlines.jsp" cascade="true" />
  <put-attribute name="two" value="/tiles/topics.jsp" cascade="true" />
  <put-attribute name="one" value="/tiles/comments.jsp" cascade="true" />
</definition>

The template of myapp.homepage.body definitionhas been used as the body attribute in the myapp.homepage definition. All of the attributes of myapp.homepage.body has been then moved as attributes of myapp.homepage definition, but with the addition of the "cascade" flag.

Extending Definitions

You can extend definitions like a Java class. The concepts of abstract definitionextension and override are available.

  • Abstract definition: it is a definition in which the template attributes are not completely filled. They are useful to create a base page and a number of extending definitions, reusing already created layout. For example:
    <definition name="myapp.page.common" template="/layouts/classic.jsp">
      <put-attribute name="header" value="/tiles/banner.jsp" />
      <put-attribute name="menu" value="/tiles/common_menu.jsp" />
      <put-attribute name="footer" value="/tiles/credits.jsp" />
    </definition>
  • Definition extension: a definition can inherit from another definition, to reuse an already made (abstract or not) definition:
    <definition name="myapp.homepage" extends="myapp.page.common">
      <put-attribute name="title" value="Tiles tutorial homepage" />
      <put-attribute name="body" value="myapp.homepage.body" />
    </definition>

    In this case, the header, menu and footer are inherited from the myapp.page.common definition, while the rest is defined inside the "concrete" definition.

  • Template and attribute override: when extending a definition, its template and attributes can be overridden.
    • Overriding a template:
      <definition name="myapp.homepage.alternate" extends="myapp.homepage"
          template="/layouts/alternate.jsp" />

      The definition has the same attributes, but its template changed. The result is that the content is the same, but the layout is different.

    • Overriding attributes:
      <definition name="myapp.homepage.customer" extends="myapp.homepage">
        <put-attribute name="menu" value="/tiles/common_menu_for_customers.jsp" />
      </definition>

      In this case, the page will have the same appearance as the myapp.homepage definition, but its menu subpage is different.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值