struts 模板方法
转载:转载请保留本信息,本文来自http://www.matrix.org.cn/resource/article/0/701.html
This is the <b>TOP</b> Content for the <b>First</b>
example. This is a static html file.

类似的,middle1.jsp包括如下内容:

This is the <b>MIDDLE</b> Content for the <b>First</b> 
example. This is a dynamic JSP file and the
current date and time is <%= new Date() %>


定义了内容之后,让我们来定义两个模版。
在template-example下建立名为templates的目录,并在其下创建两个文件template1.jsp和template2.jsp。
这些模版定义了两种布局方式,如下图所示。
struts 模板方法
struts 模板方法

我们如下来定义这两个模版:


<%@ taglib uri='/WEB-INF/struts-
template.tld' prefix='template' %>
<html>
<head>
<title><template:get name='title'/>
</title>
</head>
<body>
<table border=1>
<tr>
<td><template:get name='top'/></td>
</tr>
<tr>
<td bgcolor="#FF55FF">
<template:get name='middle'/></td>
</tr>
<tr>
<td><template:get name='bottom'/></td>
</tr>
</table>
</body>
</html>
定义 Template 1




<%@ taglib uri='/WEB-INF/struts-
template.tld' prefix='template' %>
<html>
<head>
<title><template:get name='title'/>
</title>
</head>
<body>
<table border=1>
<tr>
<td><template:get name='top'/></td>
<td bgcolor="#FF55FF">
<template:get name='middle'/></td>
<td><template:get name='bottom'/></td>
</tr>
</table>
</body>
</html>
定义 Template 2

注意到这两个模版定义了两种布局方式,换句话说,它们定义了两种不同的布局法则。
我们已经有了我们自己的内容和模版。现在我们需要的是容器文件。在tomcat-example目录下创建两个容器文件分别名为container1.jsp和container2.jsp。容器文件需要包含组件(内容)。同样,容器文件利用template insert和put tags标签来逐个定义使用和加入的组件(内容)。

让我们带着这些概念看一下其中的一个容器文件(container1.jsp)。

<%@ taglib uri='/WEB-INF/struts-template.tld' prefix='template' %>
<template:insert template='/templates/template1.jsp'>
<template:put name='title' content='Templates' direct='true'/>
<template:put name='top' content='/content/top1.htm' />
<template:put name='middle' content='/content/middle1.jsp' />
<template:put name='bottom' content='/content/bottom1.htm'/>
</template:insert>


我们可以打开浏览器,输入http://localhost:8080/template-example/container1.jsp看看这个文件的实际输出。你会看到如下的页面:
struts 模板方法
在这个页面的后面,container1.jsp使用模版template1.jsp来定义输出页面的布局并且从不同的组件中包含内容。注意这里属性direct的用法<template:put name='title' content='Templates' direct='true'/>。当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,页面输出如下:
struts 模板方法

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

从Struts 1.1开始,引入了另一种模版机制——Tiles。现在它已经成为了模版机制的一个主要分支。Tiles机制定义与Tiles网络站点中,并且与Struts定义的模版标签兼容。事实上,Tiles是模版标签的一个扩展集。那为什么我们要要Tiles来替代模版呢?

Tiles允许我们传递参数,从而对容器有更多的控制权。当我想使模版和容器具有动态特性时变的非常有用。此外,Tiles与Struts动作组件(Struts action components)结合的更为紧密。

但是,Tiles会使代码变的更为复杂。而且更为重要的是我们还要花更多的时间来掌握它。

结语
使用模版可以方便你管理网络应用程序。这篇文章是理解Struts模版的开始。这里只介绍了关于模版的很少的一部分知识,还有很多比如基于角色(role-based)和可选内容(optional content)的知识要靠大家自己去学习。希望我的这篇文章能激励大家去探索更多的关于模版的特点。


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=725102


相关文章:

猜你喜欢
  • 2022-12-23
  • 2021-07-02
  • 2022-12-23
  • 2021-09-21
相关资源
相似解决方案