JSP模板应用指南(上)

Window 工具包提供了一种典型的布局机制,比如说在一个容器中确定部件元素的位置。在AWT 和 Swing都有布局管理器,而在VisualWorks Smalltalk中有wrapper。本文将介绍一种JSP模板机制,它允许布局被封装和重新利用。JSP模板最小化了布局改变所造成的影响,这里我们将鼓励大家采用封装模块化设计。

尽管 Web开发工具的改进非常迅速,但是它们仍然落后于图形用户界面(GUI)工具包(Swing 和 VisualWorks Smalltalk)。例如,在传统的GUI工具包中提供了布局管理器,在一个窗体或另一个窗体中,允许布局运算被封装和重新利用。本文介绍的这种JSP模板机制,就象布局管理器一样,可以封装布局,所以它能够被重新利用而不只是复制使用。

由于在布局的发展过程中出现了许多的变化,而对功能的封装是非常重要的一步,它能够被自如修改而做到对其他应用的影响最小。

JSP没有提供对封装布局的直接支持,所以具有统一格式的网页通常可以复制布局代码;例如,在图1中,显示了一个网页,它包含了标题、页脚、工具条以及页面的主要内容。

图1.网页布局 点击放大(22 KB)

在图1中显示的网页布局将以HTML表格标签来执行:

例1.包含内容:

 1<html><head><title>JSPtemplates</title></head>
 2<body background="graphics/background.jpg">
 3<table>
 4<tr valign="top"><td>```
 5@include file='sidebar.html'
 6```</td>
 7<td><table>
 8<tr><td>```
 9@include file='header.html'
10```</td></tr>
11<tr><td>```
12@include file='introduction.html'
13```</td></tr>
14<tr><td>```
15@include file='footer.html'
16```</td></tr>
17</table>
18</td>
19</tr>
20</table>
21</body></html>

在上面的例子中,包括了JSP include 命令,它允许页面内容改变——通过改变包含的文件——无须修改网页自身。不过,由于布局是很难被编码的,布局改变需要对网页进行修改。如果一个网站有多个相同格式的页面,那么一般情况下甚至简单布局的改变也涉及到整个页面的修改。

为了减少布局改变所造成的影响,我们需要一种仅仅只包含布局的机制;采用这种机制,布局和内容都可以在不修改文件的情况下分开进行修改。这种机制就是JSP模板。

使用模板
模板是一种JSP文件,它包含了参数化了的内容。这里所讨论的模板使用的是一套定制化标签来执行的:template:get,template:put和template:insert。template:get 标签访问参数化的内容,就象在例 2.a中的一样,它将和图 1一样的格式来生成网页。

例 2.a.一个模板

1@taglib uri='/WEB-INF/tlds/template.tld' prefix='template' 
 1<html><head><title><template:get name="title"></template:get></title></head>
 2<body background="graphics/background.jpg">
 3<table>
 4<tr valign="top"><td><template:get name="sidebar"></template:get></td>
 5<td><table>
 6<tr><td><template:get name="header"></template:get></td></tr>
 7<tr><td><template:get name="content"></template:get></td></tr>
 8<tr><td><template:get name="footer"></template:get></td></tr>
 9</table>
10</td>
11</tr>
12</table>
13</body></html>

例 2.a几乎与例1完全一样,不过在例2.a中我们使用了template:get 取代了例1中的include 命令.让我们来分析一下template:get 如何运行。

template:get 使用了一个专门的名字(在请求的范围内)来对一个Java Bean进行修改。Bean包含了URI (统一资源标志符,网页的一个组件,它包含在template:get中)。例如,在例 2.a的模板列表中,template:get 获得了一个URI——header.html——从一个名为header 的Bean中(在请求的范围内)。接着在template:get 中包含了header.html。

template:put 把Bean放到请求的范围内(此范围将在后面被template:get修改)。 模板包含在template:insert中。 例 2.b中举例说明了put 和 insert 标签的用法:

例 2.b. 从例2.a中使用模板

1@taglib uri='/WEB-INF/tlds/template.tld' prefix='template' 
 1<template:inserttemplate=' articletemplate.jsp'="">
 2<template:put content="Templates" direct="true" name="title"></template:put>
 3<template:put content="/header.html" name="header"></template:put>
 4<template:put content="/sidebar.jsp" name="sidebar"></template:put>
 5<template:put content="/introduction.html" name="content"></template:put>
 6<template:put content="/footer.html" name="footer"></template:put>
 7   
 8  
 9在insert 开头标签指定了被包含的模板,在这个例子里,模板在例2.a中。每一个put 标签在请求范围内存储了一个Bean,而在insert 结尾标签包含了模板。模板接着象上面所描述的那样访问Bean。   
10  
11direct 的属性能够为template:put指定;如果direct 设置为true, 和标签相关联的内容将不包含在template: get中。   
12  
13一个网站包含了多页相同格式的页面,这样就可以使用一个模板,比如在例 2.a中列出了一个模板,在许多的JSP网页(例2.b)中,都用到了这个模板。   
14  
15使用模板的另一个好处是可以进行模块化设计。例如,例2.b中列出的JSP 文件中包含了header.html,让我们再来看下面的例2.c。   
16  
17例2.c. header.html   
18  
19<table>
20<tr>
21<td><img src="graphics/java.jpg"/></td>
22<td><img src="graphics/templates.jpg"/></td>
23</tr>
24</table><hr/>   
25  
26由于header.html 是被包含的内容,所以它不必在需要显示标头的页面中复制其代码。而且,尽管header.html 是一个HTML文件,但是在文件中并没有使用一般的起始HTML标签(比如<html>或<body>),因为这些标签都将被模板定义。由于在模板中包含了header.html,这些标签在header.html就可以不必再使用了。   
27  
28注意:JSP提供了两种方式来包含内容:静态方式,使用include命令;动态方式,使用include action。include命令包含了目标页面的引用源,这和C语言中的#include和Java中的import相似。include action 包含了在运行时间内目标所产生的响应。   
29  
30与JSP include action一样,模板包含有动态内容。所以,尽管在例1和例2.b中的JSP网页在功能上是一致的,但是前面包含的静态内容被后面动态的包含了。   
31  
32可选内容   
33所有的模板内容都是可选的,模板的内容可以很容易的在更多的网页中使用。例如,在图 2.a和图 2.B中显示了两个页面——登录和清单——它们使用的是同一个模板。两个页面中都包含一个标头、页脚和主要内容。清单页面中有一个编辑Panel (这是登陆页面所缺乏的)用来改变清单。   
34  
35  
36图 2.a.一个登陆窗口 点击放大(24 KB)   
37  
38  
39图 2.B.一个清单页 点击放大(42 KB)   
40  
41下面,你会发现模板将被登录和清单页面共用:   
42  

@taglib uri='template.tld' prefix='template'

 1  
 2……   
 3  
 4<table width="670">
 5<tr><td width="60"></td>
 6<td><template:get name="header"></template:get></td></tr>
 7<tr><td width="60"></td>
 8<td><template:get name="main-content"></template:get></td></tr>
 9<tr><td width="60"></td>
10<td><template:get name="editPanel"></template:get></td></tr>
11<tr><td width="60"></td>
12<td><template:get name="footer"></template:get></td></tr>
13</table>   
14  
15……   
16  
17清单页面使用了上面的模板以及专门用于编辑Panel的内容:   
18  

@taglib uri='template.tld' prefix='template'

1  

@taglib uri='security.tld' prefix='security'

 1  
 2<template:inserttemplate=' template.jsp'="">   
 3  
 4……   
 5  
 6<template:put content="/editPanelContent.jsp" name="editPanel"></template:put>   
 7  
 8……   
 9  
10   
11  
12与上面相对照,登录页面没有专门用于编辑Panel的内容:   
13  

@taglib uri='template.tld' prefix='template'

 1  
 2<template:inserttemplate=' template.jsp'="">
 3<template:put content="Login" direct="true" name="title"></template:put>
 4<template:put content="/header.jsp" name="header"></template:put>
 5<template:put content="/login.jsp" name="main-content"></template:put>
 6<template:put content="/footer.jsp" name="footer"></template:put>
 7   
 8  
 9由于登录页面中没有专门用于编辑Panel的内容,所以它没有包括。   
10  
11基于Role的内容   
12Web应用程序常常会基于不同的用户生成不同的内容。 例如,相同的 JSP模板,只有当用户为管理员的时候才出现编辑Panel,下面是得出的两个不同的页面(如图3.a和3.b.)   
13  
14  
15图 3.a. 管理员的清单页面 点击放大(27 KB)   
16  
17  
18图 3.b.其他用户的清单页面 点击放大(21 KB)   
19  
20在图3.a和3.b中的模板使用了template:get的 role 属性:   
21  

@taglib uri='template.tld' prefix='template'

 1  
 2......   
 3  
 4<table>   
 5  
 6......   
 7  
 8<td><template:get name="editPanel" role="curator"></template:get></td>   
 9  
10......   
11  
12</table>   
13  
14......   
15  
16get 标签仅仅在用户的Role 与Role属性相匹配的时候才包含内容。让我们来看看标签handler是如何使用Role属性的:   
17  
18public class GettagextendstagSupport {   
19  
20private String name = null, role = null;   
21  
22......   
23  
24public void setRole(String role) { this.role = role; }   
25  
26......   
27  
28public int doStartTag() throws JspException {   
29  
30......   
31  
32if(param != null) {   
33  
34if(roleIsValid()) {   
35  
36// include or print content ......   
37  
38}   
39  
40}   
41  
42......   
43  
44}   
45  
46private boolean roleIsValid() {   
47  
48return role == null || // valid if role isn't set   
49  
50((javax.Servlet.http.HttpServletRequest)   
51  
52pageContext.getRequest()).isUserInRole(role);   
53  
54}   
55  
56}</template:inserttemplate='></template:inserttemplate='></body></html></template:inserttemplate='>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus