登录使用NVelocity模版实现

模板页:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form action="TestHandler.ashx" method="get">
        name<input type="text" name="userName" /><br />
        age<input type="text" name="userAge" />
        <input type="checkbox" name="isVip" />U are vip?
        <select name="sl">
            <option value="bj">beijing</option>
            <option value="nj">nanjing</option>
        </select>
        <input type="submit" />
    </form>
</body>
</html>

应用处理处理程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;

namespace Demo
{
    /// <summary>
    /// NewLoginHandler 的摘要说明
    /// </summary>
    public class NewLoginHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string name = context.Request["name"];
            string password = context.Request["password"];
            VelocityContext vlContext = new VelocityContext();//实例化一个VelocityContext对象
            if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(password))
            {
                VelocityEngine vlEngine = new VelocityEngine();
                vlEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
                vlEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
                System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//括号内填写模板页所在文件夹
                vlEngine.Init();

                vlContext.Put("name", "");
                vlContext.Put("password", "");
                vlContext.Put("msg", "");

                Template vlTemplate = vlEngine.GetTemplate("NewLogin.html");//模板页
                System.IO.StringWriter vlWriter = new System.IO.StringWriter();
                vlTemplate.Merge(vlContext, vlWriter);

                String html = vlWriter.GetStringBuilder().ToString();
                context.Response.Write(html);
            }
            else
            {
                if (name=="guanlin"&&password=="123")
                {
                    vlContext.Put("name", name);
                    vlContext.Put("password", password);
                    vlContext.Put("msg", "success");
                }
                else
                {
                    vlContext.Put("name", name);
                    vlContext.Put("password", password);
                    vlContext.Put("msg", "false");
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值