博客
关于我
NVelocity标签使用详解
阅读量:801 次
发布时间:2023-02-17

本文共 3510 字,大约阅读时间需要 11 分钟。

本文使用的NVelocity版本为1.1.1,应该是目前为止最新的版本吧

前几天在Google上找到了一个自称是NVelocity 1.6.1 beta2的DLL,下载下来一看更新时间是2009年的,还没版本NVelocity 1.1.1(2010年出的)新呢!这让我有些困惑,究竟1.1.1是不是已经过时了?还是说这个1.6.1 beta2其实是有更新的?我需要进一步确认一下。

一、资源、文档下载

资源和文档下载方面,NVelocity官方网站应该是最可靠的来源。除了官方网站,你也可以在一些技术论坛和社区中找到更多资源下载链接。需要注意的是,下载资源时要确保来源的可靠性,避免下载到恶意软件或过时的版本。

二、使用步骤

使用NVelocity的步骤大致如下:

a) 创建Velocity引擎(VelocityEngine)并设置属性

using System;using System.Web;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");        velocityEngine.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,             context.Server.MapPath("~/Template/"));        velocityEngine.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");        velocityEngine.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");        velocityEngine.Init();        // 2.Velocity上下文对象设置        VelocityContext vc = new VelocityContext();        vc.Put("Name", "MT!");        List
list = new List
(); for (int i = 1; i < 11; i++) { list.Add("My Name Is :" + i); } vc.Put("list", list); // 3.创建模板(Template) Template template = velocityEngine.GetTemplate("default.html"); // 4.合并模板和上下文对象、输出 template.Merge(vc, HttpContext.Current.Response.Output); HttpContext.Current.Response.End(); } public bool IsReusable { get { return false; } }}

b) VelocityContext上下文对象创建于设置

c) 使用VelocityEngine创建模板(Template)

d) 合并模板和上下文对象、输出

三、代码演示

在代码演示部分,我们可以看到以下示例:

  • 一般处理类ShowHTML.ashx代码如下:
  • <%@ WebHandler Language="C#" Class="ShowHTML" %>using System;using System.Web;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");        velocityEngine.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,             context.Server.MapPath("~/Template/"));        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
    list = new System.Collections.Generic.List
    (); for (int i = 1; i < 11; i++) { list.Add("My Name Is :" + i); } vc.Put("list", list); // 3.创建模板(Template) Template template = velocityEngine.GetTemplate("default.html"); // 4.合并模板和上下文对象、输出 template.Merge(vc, HttpContext.Current.Response.Output); HttpContext.Current.Response.End(); } public bool IsReusable { get { return false; } }}
    1. 默认模板default.html代码如下:
    2. NVelocity 使用测试模板俺叫$Name
      #foreach($item in $list) $item #end
      1. 效果如下:
      2. (此处图片被移除)

        通过以上代码示例,可以看到NVelocity在生成HTML内容方面的强大功能。虽然1.1.1版本已经有段时间,但它仍然是一个稳定的版本,适合大多数开发需求。关于NVelocity 1.6.1 beta2的更新情况,我需要进一步确认其是否稳定,是否有兼容性问题,这将影响到升级的决策。

        【Stone 制作整理,引用请写明出处谢谢合作,联系QQ:1370569】

    转载地址:http://zznfk.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现Length conversion长度转换算法(附完整源码)
    查看>>
    Objective-C实现Levenshtein 距离算法(附完整源码)
    查看>>
    Objective-C实现levenshteinDistance字符串编辑距离算法(附完整源码)
    查看>>
    Objective-C实现lfu cache缓存算法(附完整源码)
    查看>>
    Objective-C实现LFU缓存算法(附完整源码)
    查看>>
    Objective-C实现linear algebra线性代数算法(附完整源码)
    查看>>
    Objective-C实现linear congruential generator线性同余发生器算法(附完整源码)
    查看>>
    Objective-C实现linear discriminant analysis线性判别分析算法(附完整源码)
    查看>>
    Objective-C实现linear regression线性回归算法(附完整源码)
    查看>>
    Objective-C实现linear search线性搜索算法(附完整源码)
    查看>>
    Objective-C实现Linear search线性搜索算法(附完整源码)
    查看>>
    Objective-C实现LinearSieve线性素数筛选算法 (附完整源码)
    查看>>
    Objective-C实现LinkedListNode链表节点类算法(附完整源码)
    查看>>
    Objective-C实现LinkedList链表算法(附完整源码)
    查看>>
    Objective-C实现local weighted learning局部加权学习算法(附完整源码)
    查看>>
    Objective-C实现logistic regression逻辑回归算法(附完整源码)
    查看>>
    Objective-C实现logistic sigmoid函数(附完整源码)
    查看>>
    Objective-C实现longest Common Substring最长公共子串算法(附完整源码)
    查看>>
    Objective-C实现longest increasing subsequence最长递增子序列算法(附完整源码)
    查看>>
    Objective-C实现longestCommonSubsequence最长公共子序列算法(附完整源码)
    查看>>