使用Velocity吧2:Velocity的layout功能

[b]一、从VelocityViewServlet到VelocityLayoutServlet[/b]

使用Velocity开发web应用时,需要在web.xml中配置一个Velocity提供的VelocityViewServlet接受处理对velocity模板(即vm文件)的forward访问。VelocityViewServlet负责将设置在request中的attribute“读出”和模板文件进行merge形成最终的页面,向response输出显示在用户电脑上。

VelocityViewServlet是一个简单的易使用的。但是只要把web.xml中VelocityViewServlet换成VelocityLayoutServlet,并配置上2,3句话,将具有页面简单Layout的功能。而这个功能其实非常强大。

[b]二、VelocityLayoutServlet可以。。。[/b]

VelocityLayoutServlet可以用来简化velocity下的页面布局开发。
使用VelocityLayoutServlet,可以使当forward到一个页面时,能把自动把该页面作为一个页面布局的一部分整体显示出来。比如访问用户资料页面,能够自动把网站的头,尾等自动也输出显示处理。

[b]三、VelocityLayoutServlet使用按步就班[/b]

系统中有若干页面布局是这样设定的:头部(header),左侧菜单区域(sub),中右侧页面内容部(main),底部(footer)。

1、

创建文件[webapp home]/vm/layout/layout.vm如下:


[code]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
...省略...
</head>
<body>
<div id="header>#parse('vm/layout/header.vm')</div>
<div id="content">
<div id="sub">#parse($sub)</div>
<div id="main">$screen_content</div>
</div>
<div id="footer">#parse('vm/layout/footer.vm')</div>
</body>
</html>
[/code]
$screen_content相当于一个占位符,被forward的目标页面内容将替代该处内容。
#parse($sub):表示sub位置是可以动态通过$sub变量设置的。

同时也创建'vm/layout/footer.vm''vm/layout/header.vm'这两个文件。


2、
创建WEB-INF/vm/user/profile.vm如下:(假设该页面用于显示用户的资料信息)
[code]
#set($layout = "layout.vm")
#set($sub= "vm/user/sub.vm" )
A: What's your name?<br>
B: My name is $user.loginName!
[/code]

注意,这个文件和普通我vm不一样的地方在于前两句话。
第一句话设置这个页面使用哪个布局器。
第二句话设置sub的值,用于布局把vm/user/sub.vm文件包含进来。


同时也创建"vm/user/sub.vm"文件

3、配置velocity.properites文件

使用velocity一般都是需要配置velocity.properites的,至少应该设置input.encoding和output.encoding等。在这个文件中增加设置如下代码:

[code]# Directory for layout templates,
# relative to web application root directory
tools.view.servlet.layout.directory = vm/layout/

# Filepath of the default layout template
# relative to the layout directory
# !!!!!哥们要注意这行提示:NOT relative to the root directory of the webapp!!!!
tools.view.servlet.layout.default.template = layout.vm
[/code]

4、确定web.xml配置和下面的差不多如下(主要是配置VelocityLayoutServlet,而非VelocityViewServlet):

[code]

<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>
org.apache.velocity.tools.view.servlet.VelocityLayoutServlet
</servlet-class>

<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/toolbox.xml</param-value>
</init-param>
<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
[/code]

5、运行服务器吧,
a) 访问http://www.xxx.com/vm/user/profile.vm 看看效果。

b) 回到vm/user/profile.vm文件,把[b]#set($layout = "layout.vm")[/b]去掉 看看效果。

效果比较:
加上$layout设置的profile.vm文件,浏览该页面时,将自动把该页面变成布局的一部分,把header.vm,sub.vm,footer.vm也输出出来;去掉$layout设置后浏览时,只是输出该页面,不会将header.vm,sub.vm,footer.vm输出出来。

这种功能在调试和编写时非常方便。


以后每增加一个新的页面时,只要在第一行设置了$layout指向一个布局模板便可(注意布局模板的路径,第3步已经做了提示),便可以轻松具有简单的布局功能了


6、后语:

一直想着自己实现一个类似的功能,最后再仔细看看Velocity官方网站时才发现velocity已经有了。我觉得挺好的,很符合我的需求。

[b]所以,如果你使用或即将使用Velocity开发系统,强烈推荐使用layout功能。[/b]

参考资料
1、[url]http://velocity.apache.org/tools/devel/view/layoutservlet.html[/url]。
2、[url=http://www.iteye.com/topic/60119]使用velocity吧 :$velocityCount和Tool的使用 [/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值