NVelocity标签使用详解
来源:互联网 发布:自动化软件公司简介 编辑:程序博客网 时间:2024/05/23 10:31
使用步骤。
a) 创建Velocity 引擎(VelocityEngine)并设置属性.
b) VelocityContext 上下文对象创建于设置.
c) 使用VelocityEngine(Velocity 引擎)创建模板(Template).
d) 合并模板和上下文对象、输出.
代码演示。
先引入NVelocity.dll,然后添加代码。
1.一般处理类ShowHTML.ashx代码如下:
<%@ WebHandler Language="C#" Class="ShowHTML" %>using System;using System.Web;// NVelocity 引用using NVelocity;using NVelocity.App;using NVelocity.Runtime;public class ShowHTML : IHttpHandler{ public void ProcessRequest(HttpContext context) { // 1.创建Velocity 引擎(VelocityEngine)并设置属性 VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // Velocity加载类型velocityEngine.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,context.Server.MapPath("~/Template/")); // Velocity加载模板文件夹路径velocityEngine.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312"); // 输入编码格式设置velocityEngine.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312"); // 输出编码格式设置velocityEngine.Init(); // 2.Velocity 上下文对象设置 VelocityContext vc = new VelocityContext(); // 页面参数设值 vc.Put("Name", "MT!"); System.Collections.Generic.List<String> list = new System.Collections.Generic.List<string>(); for (int i = 1; i < 11; i++) { list.Add("My Name Is :" + i); } vc.Put("list", list); // 3.创建模板(Template),default.html为模板文件的名称。在模板文件夹目录Template template = velocityEngine.GetTemplate("default.html"); // 4.合并模板和上下文对象、输出 template.Merge(vc, HttpContext.Current.Response.Output); HttpContext.Current.Response.End(); } public bool IsReusable { get { return false; } }}
2.default.html模板代码如下:
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title>NVelocity 使用测试模板</title></head><body> 俺叫$Name <br /> #foreach($item in $list) $item<br /> #end</body></html>
3.效果如下:
原文:http://www.cnblogs.com/stone_w/archive/2011/09/09/2172341.html
0 0
- NVelocity标签使用详解
- NVelocity标签使用详解
- NVelocity标签使用详解
- NVelocity系列:NVelocity配置详解
- asp.net模板引擎:二、NVelocity标签语详解
- NVelocity的使用
- NVelocity其它使用注意
- NVelocity基本使用
- NVelocity
- NVelocity
- NVelocity
- Nvelocity
- NVelocity标签设置缓存的解决方案
- NVelocity标签设置缓存的解决方案
- 登录使用NVelocity模版实现
- 标签使用详解
- Mailto 标签使用详解
- label标签使用详解
- iOS界面-仿网易新闻左侧抽屉式交互
- Can not issue executeUpdate() for SELECTs
- Windows 下Zeus 控制端的安装和使用
- SimpleDateFormat使用详解
- java jar 打包命令
- NVelocity标签使用详解
- iOS界面-仿网易新闻左侧抽屉式交互 续(添加新闻内容页和评论页手势)
- 1024. Palindromic Number (25)
- WordPress设置语法高亮
- 给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数
- ZOJ 3717 2-sat 进位精度
- java之LineNumberReader
- Core Animation之基础介绍
- hdu 1160 FatMouse's Speed 简单dp 题解