zoukankan      html  css  js  c++  java
  • NVelocity练习

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.IO;
    using Commons.Collections;
    using NVelocity;
    using NVelocity.App;
    using NVelocity.Context;
    using NVelocity.Runtime;


    namespace TestConsoleAppCastleNVelocity
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    //Step 1 - Creating a VelocityEngine
                VelocityEngine engine = new VelocityEngine();
                ExtendedProperties props 
    = new ExtendedProperties();
                props.AddProperty(RuntimeConstants.RESOURCE_LOADER, 
    "file");
                props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
                    
    "E:/_Projects/DotNet/TestConsoleAppCastleNVelocity/TestConsoleAppCastleNVelocity/templates/");
                props.AddProperty(RuntimeConstants.INPUT_ENCODING, 
    "utf-8");
                props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, 
    "utf-8");
                engine.Init(props);

                
    //Step 2 - Creating the Template            
                Template template = engine.GetTemplate("First.vm");

                
    //Step 3 - Merging the template
                VelocityContext context = new VelocityContext();
                context.Put(
    "from""somewhere");
                context.Put(
    "to""someone");
                context.Put(
    "subject""Welcome to NVelocity");
                context.Put(
    "customer"new Customer("顾客姓名吴XX"));

                List
    <Customer> list = new List<Customer>();
                list.Add(
    new Customer("张三"));
                list.Add(
    new Customer("李四"));
                list.Add(
    new Customer("五五"));
                list.Add(
    new Customer("赵六"));
                list.Add(
    new Customer("李七"));
                context.Put(
    "customerList", list);

                StringWriter writer 
    = new StringWriter();
                template.Merge(context, writer);
                Console.WriteLine( writer.GetStringBuilder().ToString() );

                
    //Console.WriteLine( System.AppDomain.CurrentDomain.BaseDirectory );

                Console.ReadKey();
            }
        }
    }

    模板代码:

    代码
    From来自: $from
    To去: $to
    Subject主题: $subject

    Hello $customer.Name

    We
    're please to yada yada yada.
    #set($myInt=1024)
    ${myInt}s

    ****************************
    #set ( $bFlag=3 )
    #
    if($bFlag==1) 
    It's one
    #elseif($bFlag==2)
    It's two
    #else
    It's three
    #end

    ##这是注释

    #*
    这是多行
    注释
    *#
    *******foreach test********
    <table>
    #foreach( $customer in $customerList )
        <tr><td>$velocityCount 
        
    #if($velocityCount%2==0) 偶数行 #else 奇数行 #end
        </td><td>$customer.Name</td></tr>
    #end
    </table>

    /Files/wucg/TestConsoleAppCastleNVelocity.rar

  • 相关阅读:
    Oracle对表解锁
    写txt文件在AIX5.3系统中乱码
    Spring Data JPA初使用
    简单流水号生成
    Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COER
    install Tcl8.5.10
    centOS下开机自启动apache、mysql、samba、svn等服务的最简单方法
    UNIX_SOCKET 进程间通信
    Linux(CentOS 5.5) Redis 安装及RedisPHP拓展安装应用
    python 返回 字典型结果集
  • 原文地址:https://www.cnblogs.com/wucg/p/1785654.html
Copyright © 2011-2022 走看看