1. C # basic use of NVelocity in MVC Learning,
I'm a little white guy. I just started learning. Don't laugh ......
NVelocity is a very easy-to-use framework. It can be learned from it and can be developed step by step.
First, create an empty web application and create a general handler Login2.ashx.
Then, introduce NVelocity,
C
Create login.htm on the Html page to implement the logon framework.
NVelocity syntax starts with $
<Html>
Write the Login2.ashx. cs page and use NVelocity to process requests
Public class Login2: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/html"; string username = context. request ["username"]; string password = context. request ["password"]; if (string. isNullOrEmpty (username) & string. isNullOrEmpty (password) {VelocityEngine vltEngine = new VelocityEngine (); vltEngine. setProperty (RuntimeConstants. RESOURCE_LOADER, "File"); vltEngine. SetProperty (RuntimeConstants. FILE_RESOURCE_LOADER_PATH, System. Web. Hosting. HostingEnvironment. MapPath ("~ /Templates "); // vltEngine. init (); VelocityContext vltContext = new VelocityContext (); vltContext. put ("username", ""); // set parameters. In the template, you can use $ data to reference vltContext. put ("password", ""); vltContext. put ("msg", ""); Template vltTemplate = vltEngine. getTemplate ("login.htm"); System. IO. stringWriter vltWriter = new System. IO. stringWriter (); vltTemplate. merge (vltContext, vltWriter); string html = vltWriter. GetStringBuilder (). toString (); context. response. write (html);} else {if (username = "admin" & password = "123") {context. response. write ("Logon successful");} else {VelocityEngine vltEngine = new VelocityEngine (); vltEngine. setProperty (RuntimeConstants. RESOURCE_LOADER, "file"); vltEngine. setProperty (RuntimeConstants. FILE_RESOURCE_LOADER_PATH, System. web. hosting. hostingEnvironment. mapPath ("~ /Templates "); // vltEngine. init (); VelocityContext vltContext = new VelocityContext (); vltContext. put ("username", username); // you can use $ data to reference vltContext in the template. put ("password", password); vltContext. put ("msg", "incorrect user name or password"); Template vltTemplate = vltEngine. getTemplate ("login.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 ;}}
NVelocity is more powerful than that. It can also assign values to object attributes. See the following example.
<Html>
Background processing program:
Context. response. contentType = "text/html"; Person person = new Person (); person. name = "Zhang San"; person. age = 30; Person dad = new Person (); dad. name = "Zhang San's father"; dad. age = 60; person. father = dad; VelocityEngine vltEngine = new VelocityEngine (); vltEngine. setProperty (RuntimeConstants. RESOURCE_LOADER, "file"); vltEngine. setProperty (RuntimeConstants. FILE_RESOURCE_LOADER_PATH, System. web. hosting. hostingEnv Ironment. MapPath ("~ /Templates "); // vltEngine. init (); VelocityContext vltContext = new VelocityContext (); vltContext. put ("p", person); // you can use $ data in the Template to reference Template vltTemplate = vltEngine. getTemplate ("test.htm"); System. IO. stringWriter vltWriter = new System. IO. stringWriter (); vltTemplate. merge (vltContext, vltWriter); string html = vltWriter. getStringBuilder (). toString (); context. response. write (html );
Code: http://download.csdn.net/detail/murongqingsi/9156661
NVelocity: http://download.csdn.net/detail/murongqingsi/9156623