日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Nvelocity 模板引擎 实例

發(fā)布時間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nvelocity 模板引擎 实例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

??看了小泥鰍博客源碼,覺得模板引擎是個好東西,參考了源代碼寫了基于Nvelocity模板引擎的實例。寫著玩的,歡迎拍磚。 項目結構如下:

??

?? 關鍵代碼:TemplateHelper.cs ? ?Nvelocity輔助類,參考NVelocity 操作類VelocityHelper

?

代碼 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

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

namespace NVelocityApp.Core
{
public class TemplateHelper
{
private VelocityEngine velocity = null;
private IContext context = null;
private string templatFileName;

public TemplateHelper(string templatePath)
{
velocity
= new VelocityEngine();

//使用設置初始化VelocityEngine
ExtendedProperties props = new ExtendedProperties();

props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatePath);
props.AddProperty(RuntimeConstants.INPUT_ENCODING,
"utf-8");

// props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");
// props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");

// props.SetProperty(RuntimeConstants.RESOURCE_MANAGER_CLASS, "NVelocity.Runtime.Resource.ResourceManagerImpl\\,NVelocity");

velocity.Init(props);
//RuntimeConstants.RESOURCE_MANAGER_CLASS
//為模板變量賦值
context = new VelocityContext();

}

/// <summary>
/// 給模板變量賦值
/// </summary>
/// <param name="key">模板變量</param>
/// <param name="value">模板變量值</param>
public void Put(string key, object value)
{
//if (context == null)
// context = new VelocityContext();
context.Put(key, value);
}




/// <summary>
/// 生成字符
/// </summary>
/// <param name="templatFileName">模板文件名</param>
public string BuildString(string templateFile)
{
//從文件中讀取模板
Template template = velocity.GetTemplate(templateFile);

//合并模板
StringWriter writer = new StringWriter();
template.Merge(context, writer);
return writer.ToString();
}


/// <summary>
/// 顯示模板
/// </summary>
/// <param name="templatFileName">模板文件名</param>
public void Display(string templatFileName)
{
//從文件中讀取模板
Template template = velocity.GetTemplate(templatFileName);
//合并模板
StringWriter writer = new StringWriter();
template.Merge(context, writer);
//輸出
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
}

?

?

?? ? ? ? ? ? ? ? TagFields.cs ? ? ? ? ? ?對應模板中限定名。

?

代碼 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NVelocityApp.Core
{
public class TagFields
{
/// <summary>
/// 網站名稱
/// </summary>
public const string SITE_NAME = "sitename";

/// <summary>
/// 網站描述
/// </summary>
public const string SITE_DESCRIPTION = "sitedescription";


/// <summary>
/// 文章列表
/// </summary>
public const string Article_LIST = "articlelist";
}
}

?

?

?? ? ? ? ? ? ? ? ?PageMethod.cs ? ? ? ?頁面處理類,主要向相應模板傳遞數(shù)據(jù)。有點像MVC中的C。

?

代碼 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace NVelocityApp.Core
{

public class PageMethod
{

public void index(TemplateHelper th)
{

string aa = HttpContext.Current.Request.QueryString["t"];
th.Put(TagFields.SITE_NAME,
"測試網站");

}

public void list(TemplateHelper th)
{
List
<Person> list = new List<Person>();

for (int i = 0; i <= 5; i++)
{
list.Add(
new Person { ID = i.ToString(), Name = "" + i + "個用戶" });
}
th.Put(TagFields.SITE_NAME,
"測試網站");
th.Put(TagFields.Article_LIST, list);
}
}
}

?

?

?? ? ? ? ? ? ? ?Entity 下的是實體。當做測試用。

?

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

namespace NVelocityApp.Core
{
public class Person
{
public string ID
{
get;
set;
}

public string Name
{
get;
set;
}

}

}

?

?

?? ? ? 沒有寫業(yè)務層與數(shù)據(jù)層,可以根據(jù)情況擴展。

?? ? ? ? ? ? ? Default.aspx.cs 中代碼,通過反射調用PageMethod.cs中方法。反射的用法可以參考。反射學習筆記(1)

?

代碼 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NVelocityApp.Core;
using System.Reflection;

namespace NVelocityApp
{
public partial class _Default : System.Web.UI.Page
{

/// <summary>
/// 模板封裝類
/// </summary>
private TemplateHelper th = null;// new TemplateHelper();

/// <summary>
/// 模板路徑
/// </summary>
private string templatePath = null;


protected void Page_Load(object sender, EventArgs e)
{
string cmd = Request.QueryString["t"];//頁面唯一限定名

//可以設置讀取
templatePath = Server.MapPath(string.Format("/themes/{0}/template/", "default"));
th
= new TemplateHelper(templatePath);
try
{
Assembly ass
= Assembly.Load("NVelocityApp.Core");
object obj = ass.CreateInstance("NVelocityApp.Core.PageMethod");
var method
= obj.GetType().GetMethod(cmd);
if (method != null)
{
method.Invoke(obj,
new object[] { th });
th.Display(cmd
+ ".html");
}

}
catch (Exception)
{


}
}
}
}

?

?

?? ? ? ? ? ?Intelligencia.UrlRewriter.dll ? URL重寫組件。也可以自己寫個httpModules或HttpHandel處理,我直接用開源組件了,方便嘛。如果不熟悉的朋友可以參考。Url 重寫

?? ? ? ? ? ? ? ? rewrite.xml ?重寫規(guī)則

代碼 <?xml version="1.0" encoding="utf-8" ?>
<rewriteRules>
<rewrite url="~/Default.aspx" to="~/Default.aspx?t=index" processing="stop" />
<rewrite url="~/Index" to="~/Default.aspx?t=index" processing="stop" />
<rewrite url="~/List" to="~/Default.aspx?t=list" processing="stop" />

</rewriteRules>

?

當然還有最重要的Nvelocity語法,可以看看這個nVelocity使用簡介。

源碼在這:NVelocityApp.rar



?

轉載于:https://www.cnblogs.com/dooom/archive/2010/11/25/1887495.html

總結

以上是生活随笔為你收集整理的Nvelocity 模板引擎 实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。