Nvelocity 模板引擎 实例
??看了小泥鰍博客源碼,覺得模板引擎是個好東西,參考了源代碼寫了基于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 模板引擎 实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DQL、DML、DDL、DCL的概念与区
- 下一篇: 去除List集合中的重复值(四种好用的方