Asp.net static dynamic page output object attributes (Object Association)
Asp.net static dynamic page output object attributes (Object Association)
The template engine can not only output a single data, but also directly output object attributes.
Using System; using System. Collections. Generic; using System. Linq; using System. Web; using NVelocity. Runtime; using NVelocity. App; namespace czbk {////// Summary of Handler2 ///Public class Handler2: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = text/html; // plain indicates static output of common text format. html format text User user = new User (); user should be output. username = Wang Damai; user. userphone = 18312345678; Role role = new Role (); role. roleid = 1001; role. rolename = super funny king; user. user_Role1 = role; // assign VelocityEngine vltEngine = new VelocityEngine () to the correlated object; vltEngine. set Property (RuntimeConstants. RESOURCE_LOADER, file); vltEngine. SetProperty (RuntimeConstants. FILE_RESOURCE_LOADER_PATH, System. Web. Hosting. HostingEnvironment. MapPath (~ /); // The vltEngine folder where the template file is located. init (); VelocityContext vltContext = new VelocityContext (); vltContext. put (u, user); // you can use $ u in the Template to reference Template vltTemplate = vltEngine.GetTemplate(Users.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 ;}} 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 ;}} www.bkjia.com private Role User_Role; // introduce 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-end code