Java Tiles - insertAttribute 与 putAttribute - 区别/用法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20246549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:33:51  来源:igfitidea点击:

Tiles - insertAttribute vs putAttribute - Difference/usage?

javajspweb-applicationstiles

提问by aces.

I am working with Tilesand jsp servletsin a web application. Recently, I came across tiles insertAttributeand putAttributetags. Going over the documentation listed out for both the tags(hereand here), the only difference I can make out is:

我正在Web 应用程序中使用Tilesjsp servlet。最近,我遇到了瓷砖insertAttributeputAttribute标签。仔细阅读为这两个标签(此处此处)列出的文档,我能看出的唯一区别是:

putAttribute has to be enclosed in a parent container tag. Thus, from functionality viewpoint, the tags are similar.

putAttribute has to be enclosed in a parent container tag. 因此,从功能的角度来看,标签是相似的。

putAttributehas the enclosing constraint whereas insertAttributedoes not have any such constraint and seems to have more number of attributes which can be used. I suspect insertAttributetag was added later to the tiles framework.

putAttribute具有封闭约束,而insertAttribute没有任何此类约束,并且似乎有更多可以使用的属性。我怀疑insertAttribute标签是后来添加到磁贴框架中的。

In what scenarios, should one tag(esp. putAttribute) be used for another? Any explanation/illustration regarding the difference/usage scenario will be welcomed.

在什么情况下,应该将一个标签(特别是putAttribute)用于另一个?欢迎任何有关差异/使用场景的解释/说明。

回答by aces.

Form this link

形成这个链接

http://tiles.apache.org/framework/tiles-jsp/tlddoc/tiles/tld-summary.html

http://tiles.apache.org/framework/tiles-jsp/tlddoc/tiles/tld-summary.html

Inserts the value of an attribute into the page.

将属性值插入页面。

This tag can be flexibly used to insert the value of an attribute into a page. As in other usages in Tiles, every attribute can be determined to have a "type", either set explicitly when it was defined, or "computed". If the type is not explicit, then if the attribute value is a valid definition, it will be inserted as such. Otherwise, if it begins with a "/" character, it will be treated as a "template". Finally, if it has not otherwise been assigned a type, it will be treated as a String and included without any special handling.

该标签可灵活用于将属性值插入到页面中。与 Tiles 中的其他用法一样,每个属性都可以确定为具有“类型”,在定义时明确设置或“计算”。如果类型不是显式的,那么如果属性值是一个有效的定义,它将被插入。否则,如果它以“/”字符开头,则会被视为“模板”。最后,如果它没有以其他方式分配类型,它将被视为字符串并包含在内,而无需任何特殊处理。

Example :

例子 :

  <code>
            <tiles:insertAttribute name="body" />
          </code>

Declare a list that will be pass as attribute to tile.

声明一个将作为属性传递给 tile 的列表。

Declare a list that will be pass as attribute to tile. List elements are added using the tags 'addAttribute' or 'addListAttribute'. This tag can only be used inside 'insertTemplate', 'insertDefinition', 'definition' tags.

声明一个将作为属性传递给 tile 的列表。使用标签“addAttribute”或“addListAttribute”添加列表元素。此标签只能在“insertTemplate”、“insertDefinition”、“definition”标签内使用。

回答by Mircea Stanciu

Put Atttribute -> you declare what content will be associated with specified attribute name:

Put Atttribute -> 您声明哪些内容将与指定的属性名称相关联:

<definition name="admin-template-metronic" extends="metronic-template" template="/WEB-INF/templates/metronic/tiles/admin.jsp">
    <put-attribute name="html-title" value="Administrative Console"/>
    <put-attribute name="banner-content" value="/WEB-INF/templates/metronic/pages/common/banner.jsp"/>
    <put-attribute name="footer-content" value="/WEB-INF/templates/metronic/pages/common/footer.jsp"/>
    <put-attribute name="menu-content" value="/WEB-INF/templates/metronic/pages/admin/main-menu.jsp"/>
</definition>

Insert Attribute: it will use the attribute defined up using put-attribute. In your page, banner-content will be replaced with the content of file "banner.jsp"

插入属性:它将使用使用 put-attribute 定义的属性。在您的页面中,banner-content 将被替换为文件“banner.jsp”的内容

<tiles:insertAttribute name="banner-content"/>