using System; using System.Collections.Generic; using System.Linq; using System.Web; using NVelocity.Runtime; using NVelocity; using NVelocity.App; using System.Collections; namespace czbk { /// <summary> /// dictionary 的摘要说明 /// </summary> public class dictionary : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; //新建一个带索引字典 Dictionary<string, string> dict = new Dictionary<string, string>(); dict["java"] = "java工程师"; dict["net"] = "net工程师"; dict["c"] = "c工程师"; //新建hash表 // Dictionary HashTable // 强类型 弱类型 // 内存小 内存多 //Dictionary<key,value>为HashTable的泛型版本只是 Dictionary<key,value>对存入数据类型有要求,而HashTable可以存入任何类型。 Hashtable hash = new Hashtable(); hash.Add("adc", "寒冰射手"); hash.Add("apc", "九尾妖狐"); //新建数组 string[] strs = new string[] { "ef","linq","mvc","jQueery"}; //新建集合 List<Role> newrole=new List<Role>(); newrole.Add(new Role { Roleid = "001", Rolename = "青铜" }); newrole.Add(new Role { Roleid = "002", Rolename = "白银" }); newrole.Add(new Role { Roleid = "003", Rolename = "黄金" }); newrole.Add(new Role { Roleid = "004", Rolename = "铂金" }); VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/"));//模板文件所在的文件夹 vltEngine.Init(); VelocityContext vltContext = new VelocityContext(); vltContext.Put("dict", dict);//设置参数,在模板中可以通过$data来引用); vltContext.Put("hash", hash); vltContext.Put("strs", strs); vltContext.Put("newrole", newrole); Template vltTemplate = vltEngine.GetTemplate("dictionary.htm"); System.IO.StringWriter vltWriter = new System.IO.StringWriter(); vltTemplate.Merge(vltContext, vltWriter); string html = vltWriter.GetStringBuilder().ToString(); context.Response.Write(html); } public bool IsReusable { get { return false; } } } }
前台界面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> =========字典索引输出=========<br /> $dict.java<br /> $dict.net<br /> $dict.c<br /> =========哈希表输出=========<br /> $hash.adc<br /> $hash.apc<br /> =========数组遍历输出=========<br /> <ul> #foreach($str in $strs) <li> $str </li> #end </ul> =========集合遍历输出=========<br /> <ul> #foreach($r in $newrole) <li> $r.roleid<span style="color:Red;">$r.rolename</span> </li> #end </ul> =========if判断=========<br /> <ul> #foreach($rr in $newrole) $rr.roleid #if($rr.rolename=="铂金") <li>哇!你是华贵的铂金啊!</li> #elseif($rr.rolename=="黄金") <li>哇!你是尊贵的黄金啊!继续保持</li> #else <li>你还需要加油喔!</li> #end #end </ul> </body> </html>
版权声明:本文为博主原创文章,未经博主允许不得转载。
Asp.net动态页面静态化之字典哈希表的输出已及遍历判断的实现
原文:http://blog.csdn.net/l1158513573/article/details/47665957