。 在模版文件中使用。它告诉模版由puttag标签定义的不同组件的名称。它类似于java gui中的layout.addlayoutcomponent(componentname, component)方法。
一个简单的例子
你可以下载这个例子。我们将要创建两套内容文件、容器(集合单元)文件以及模版文件。当我们有了这些文件后,就可以就可以互换他们的模版,看到他们在布局上的改变。
复制struts-blank.war到tomcat的webapps目录并且将他改名为template-example.war。重起tomcat。template-example会由tomcat自动部署。删除pages目录和index.jsp文件。他们与我们的讨论暂时无关。
让我们定义又定义我们自己的内容文件做为一个开始。在template-example下建立一个名为content的目录,并且在其中创建六个文件:top1.htm, top2.htm, bottom1.htm, bottom2.htm, middle1.jsp, and middle2.jsp。
放入什么文件是取决于你的,但是对于初学者,静态的.htm文件更容易理解。举个例子来说,top.htm包括以下的内容:
this is the top content for the first
example. this is a static html file.
类似的,middle1.jsp包括如下内容:
this is the middle content for the first
example. this is a dynamic jsp file and the
current date and time is <%= new date() %>
定义了内容之后,让我们来定义两个模版。
在template-example下建立名为templates的目录,并在其下创建两个文件template1.jsp和template2.jsp。
这些模版定义了两种布局方式,如下图所示。


我们如下来定义这两个模版:
<%@ taglib uri='/web-inf/struts-
template.tld' prefix='template' %>
定义 template 1
<%@ taglib uri='/web-inf/struts-
template.tld' prefix='template' %>
定义 template 2
注意到这两个模版定义了两种布局方式,换句话说,它们定义了两种不同的布局法则。
我们已经有了我们自己的内容和模版。现在我们需要的是容器文件。在tomcat-example目录下创建两个容器文件分别名为container1.jsp和container2.jsp。容器文件需要包含组件(内容)。同样,容器文件利用template insert和put tags标签来逐个定义使用和加入的组件(内容)。
让我们带着这些概念看一下其中的一个容器文件(container1.jsp)。
<%@ taglib uri='/web-inf/struts-template.tld' prefix='template' %>
我们可以打开浏览器,输入http://localhost:8080/template-example/container1.jsp看看这个文件的实际输出。你会看到如下的页面:

在这个页面的后面,container1.jsp使用模版template1.jsp来定义输出页面的布局并且从不同的组件中包含内容。注意这里属性direct的用法。当direct设置为true时,content标签定义的内容将会被直接调用。它不寻找一个外部的文件。(when direct is set to true, the content specified by the content attribute is used directly. it is not looked up in an external file.有点疑问,翻的不好)
container2类似,所不同的只是他使用template2,页面输出如下:

我们到目前位置所做的工作的优点是显而易见的。举例来说,我们可以用template2替换template1(通过简单的复制和粘贴用template2.jsp替换template2.js1)。之后container1.jsp会马上变成如下的模样。我们改变了容器的布局却没有碰container1!这就是模版的作用。

struts template vs. tiles
从struts 1.1开始,引入了另一种模