ASP. Output Object Properties (Entity Association) of the dynamic page static
The template engine not only outputs a single piece of data, but also outputs the properties of the object directly
Using system;using system.collections.generic;using system.linq;using system.web;using NVelocity.Runtime;using Nvelocity;using nvelocity.app;namespace czbk{///<summary>//Handler2 Summary description///</summary> Publ IC class Handler2:ihttphandler {public void ProcessRequest (HttpContext context) {CONTEXT.R Esponse. ContentType = "text/html";//plain indicates that normal text format output static should output HTML format text User user = new user (); User. Username = "King Sledgehammer"; User. Userphone = "18312345678"; Role role = new role (); Role. Roleid = "1001"; Role. Rolename = "Super Funny King"; User. User_role1 = role;//Assigns a value velocityengine vltengine = new Velocityengine () to the associated object; Vltengine.setproperty (Runtimeconstants.resource_loader, "file"); Vltengine.setproperty (Runtimeconstants.file_resource_loader_path, System.Web.Hosting.HostingEnvironment.MapPath ("~/"));/ /template file is located in the directory Vltengine.init (); Velocitycontext vltcontext = new Velocitycontext (); Vltcontext.put ("U", user);//Set the number of parameters. Template vlttemplate = vltengine.gettemplate ("users.htm") can be referenced through $u in templates; 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; }}} public class User {private string username; public string Username {get {return Username;} set {username = value;} } private string Userphone; public string Userphone {get {return userphone;} set {Userphone = value;} The private role user_role;//introduces an associated class object public Role User_role1 {get {return user_role; } set {user_role = value;} }} public class Role {private string Roleid; public string Roleid {get {return roleid;} set {Roleid = value;} } private string RoleName; public string Rolename {get {return Rolename;} set {rolename = value;} } }}
Front Code
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">