Velocity的layout功能
一、从VelocityViewServlet到VelocityLayoutServlet
使用Velocity开发web应用时,需要在web.xml中配置一个Velocity提供的VelocityViewServlet接受处理对velocity模板(即vm文件)的forward访问。VelocityViewServlet负责将设置在request中的attribute“读出”和模板文件进行merge形成最终的页面,向response输出显示在用户电脑上。
VelocityViewServlet是一个简单的易使用的。但是只要把web.xml中VelocityViewServlet换成VelocityLayoutServlet,并配置上2,3句话,将具有页面简单Layout的功能。而这个功能其实非常强大。
二、VelocityLayoutServlet可以。。。
VelocityLayoutServlet可以用来简化velocity下的页面布局开发。
使用VelocityLayoutServlet,可以使当forward到一个页面时,能把自动把该页面作为一个页面布局的一部分整体显示出来。比如访问用户资料页面,能够自动把网站的头,尾等自动也输出显示处理。
三、VelocityLayoutServlet使用按步就班
系统中有若干页面布局是这样设定的:头部(header),左侧菜单区域(sub),中右侧页面内容部(main),底部(footer)。
1、
创建文件[webapp home]/vm/layout/layout.vm如下:
- <!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>
$screen_content相当于一个占位符,被forward的目标页面内容将替代该处内容。
#parse($sub):表示sub位置是可以动态通过$sub变量设置的。
同时也创建'vm/layout/footer.vm''vm/layout/header.vm'这两个文件。
2、
创建WEB-INF/vm/user/profile.vm如下:(假设该页面用于显示用户的资料信息)
- #set($layout = "layout.vm")
- #set($sub= "vm/user/sub.vm" )
- A: What's your name?<br>
- B: My name is $user.loginName!
注意,这个文件和普通我vm不一样的地方在于前两句话。
第一句话设置这个页面使用哪个布局器。
第二句话设置sub的值,用于布局把vm/user/sub.vm文件包含进来。
同时也创建"vm/user/sub.vm"文件
3、配置velocity.properites文件
使用velocity一般都是需要配置velocity.properites的,至少应该设置input.encoding和output.encoding等。在这个文件中增加设置如下代码:
- # 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
4、确定web.xml配置和下面的差不多如下(主要是配置VelocityLayoutServlet,而非VelocityViewServlet):
- <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>
5、运行服务器吧,
a) 访问http://www.xxx.com/vm/user/profile.vm 看看效果。
b) 回到vm/user/profile.vm文件,把#set($layout = "layout.vm")去掉 看看效果。
效果比较:
加上$layout设置的profile.vm文件,浏览该页面时,将自动把该页面变成布局的一部分,把header.vm,sub.vm,footer.vm也输出出来;去掉$layout设置后浏览时,只是输出该页面,不会将header.vm,sub.vm,footer.vm输出出来。
这种功能在调试和编写时非常方便。
以后每增加一个新的页面时,只要在第一行设置了$layout指向一个布局模板便可(注意布局模板的路径,第3步已经做了提示),便可以轻松具有简单的布局功能了
6、后语:
一直想着自己实现一个类似的功能,最后再仔细看看Velocity官方网站时才发现velocity已经有了。我觉得挺好的,很符合我的需求。
所以,如果你使用或即将使用Velocity开发系统,强烈推荐使用layout功能。
Velocity的layout功能相关推荐
- cocos2dx封装一个具有Layout功能的Point类 (提供源码)
(原创文章,转载请注明原文出处:http://blog.csdn.net/while0/article/details/79032004) 基于cocos2dx开发游戏,免不了设置节点或精灵的位置,这 ...
- 在Spring MVC中使用Velocity
在Spring MVC中使用Velocity – Part 1工程中配置velocity 目的 Spring MVC中结合velocity的配置和操作. 简介 我们要显示一个课程列表,需要如下的 Ja ...
- velocity制作pdf
velocity制作PDF Velocity 是一个基于 java 的模板引擎(templateengine). 它可以让视图的设计者在 web 页面中引用 java 代码中定义的数据对象和命令. 1 ...
- iOS6 自动布局 入门–Auto Layout(转)
iOS6 自动布局 入门–Auto Layout(转) 标签: 杂谈 目前为止,即使你的界面设计是在合理的复杂度内,你也必须要为之写许多代码来适应变化的布局.现在我相信你会很高兴听到这种情况将不会 ...
- adalm pluto_Apache Pluto和Velocity集成教程示例
adalm pluto Velocity is a Project of Apache Software Foundation, it's released under Apache Velocity ...
- Velocity User Guide 用户手册
转自:http://blog.csdn.net/gaojinshan/article/details/23945879 官方网址:http://velocity.apache.org/engine/d ...
- 改造industrial_robot_simulator给joint_states的velocity赋值,便于测试轨迹规划的速度平滑性
原生的不存在velocity赋值的功能,现在补充上,补充之后就可以看到joint_states的velocity话题赋值了 #!/usr/bin/env python # # Software Lic ...
- Auto Layout iOS
转自http://iosshare.cn/?p=1009 你是否曾经因为尝试想让你的应用同时在景观方向(横版)以及肖像方向(竖版)看上去不错而感到受挫?是否为了让应用同时支持iPhone以及iPad的 ...
- SpringBoot_web开发-引入thymeleaf
前端交给我们的是html页面,如果是我们以前开发,我们需要把它转成JPS文件,转JSP的好处,当我们查出一些数据,转化到JSP页面以后,我们可以用c:each进行遍历,c:if进行判断,我们JSP支持 ...
最新文章
- iOS开发面试题整理
- 引用 vsftpd配置手册(实用)
- php xingnengfenxi_PHP 性能分析与实验:性能的微观分析
- c 窗体程序 mysql_C\C++开发MySQL程序简介(下)
- 操作篇 bgp协议了解与学习
- Java连接Mysql数据库增删改查实现
- 编译选项—微软官方+各地总结
- oracle 存储过程和包的权限
- 2022年2月国产数据库排行榜: OceanBase“三连增”重夺榜眼,GaussDB实现本月最大涨幅引期待...
- 使用vue开发一个双向展开的卷轴组件
- 我是如何做Web项目的
- VC双缓冲画图技术介绍
- QTP连接数据库,并将数据保存至Excel中
- Optisystem7中 matlab元件 使用
- ISO/IEC 20000 信息技术(IT)服务管理体系及全套最新标准资料
- 一个基于angularJS的工资计算器
- 出行助手Vue项目中高德API的使用
- 【我的C语言学习进阶之旅】介绍一下NDK开发中关于JNI函数的两种注册方式:静态注册和动态注册
- 组播PIM-SM-ASM模型
- java基础入门txt下载地址_java基础入门-ZipOutputStream打包下载