• NVelocity 实现简单的留言板


    留言版简单实现

    -----------------------------------------------------------------------------------------------------------------------------------

    项目截图:

    1,新建留言板数据库:LiuYanbanDB

    USE [LiuYanbanDB]
    GO
    
    /****** Object:  Table [dbo].[LiuYan]    Script Date: 2015/5/15 22:06:11 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE TABLE [dbo].[LiuYan](
        [id] [bigint] IDENTITY(1,1) NOT NULL,
        [Title] [nvarchar](250) NULL,
        [Message] [nvarchar](max) NULL,
        [NickName] [nvarchar](50) NULL,
        [IsAnonymous] [bit] NULL,
        [IPAddress] [nvarchar](18) NULL,
        [PostDate] [datetime] NOT NULL
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    
    GO
    
    ALTER TABLE [dbo].[LiuYan] ADD  CONSTRAINT [DF_LiuYan_PostDate]  DEFAULT (getdate()) FOR [PostDate]
    GO
    View Code

    2,新建项目,添加 Head.html 和 Foot.html 作为所有页面的页头和页尾, 其他新建HTML页面,用#parse("Head.html")来调用

    3,准备好 Nvelocity模板处理类:Common_Nvelocity.cs

    using Commons.Collections;
    using NVelocity;
    using NVelocity.App;
    using NVelocity.Runtime;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    namespace LiuYanban
    {
        public class Common_Nvelocity
        {
    
            /// <summary>
            /// 通过NVelocity模板引擎,把Data数据参数中的数据传递到Template模板中
            /// </summary>
            /// <param name="TemplateName">模板页面名称</param>
            /// <param name="Data">参数数据</param>
            /// <returns>返回HTML直接渲染生成 页面</returns>
            public static string RenderHTML(string TemplateName, object Data)
            {
    
                //创建NVlecocity模板引擎的实例对象
                VelocityEngine vlEngine = new VelocityEngine();
    
                //初始化实例对象,定义对象的部分属性
                ExtendedProperties props = new ExtendedProperties();
                props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");//声明模板是在file中
                props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/Templates"));  //指定模板文件所在文件夹
                vlEngine.Init(props);
    
                //模板中的数据的来源
                VelocityContext vltContext = new VelocityContext();
                vltContext.Put("data", Data);  //设置参数,在HTML模板中可以通过$Data读取数据
    
    
                //模板文件名
                Template VltTemp = vlEngine.GetTemplate(TemplateName);
                //合并模板
                StringWriter writer = new StringWriter();
                VltTemp.Merge(vltContext, writer);
    
                //转化为字符串
                string html = writer.GetStringBuilder().ToString();
    
                return html;
            }
        }
    }
    View Code

    4,添加 新增留言处理程序和对应的HTML模板页面: MessageNew.ashx , MessageNew.html 实现新增留言功能

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace LiuYanban
    {
        /// <summary>
        /// MessageNew 的摘要说明
        /// </summary>
        public class MessageNew : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/HTML";
                string html = "";
                SQLHelper sqlH = new SQLHelper();
    
    
                string strButtonSave = context.Request["Save"];
                if (string.IsNullOrEmpty(strButtonSave))
                {
                    var data = new { PageTitle = "请发表您的留言" };
                    html = Common_Nvelocity.RenderHTML("MessageNew.html", data);
                    context.Response.Write(html);
                }
                else
                {
                    //  把留言提交
                    string strTitle = context.Request["title"];
                    string strMsg = context.Request["message"];
                    string strNickName = context.Request["NickName"];
                    bool FlagIsAnonymous = context.Request["IsAnonymous"] == "on";
                    string strIPAddress = context.Request.UserHostAddress;
    
                    //数据校验
                    //写入数据库
    
                    string sqlInsert = " insert into  [LiuYanbanDB].[dbo].[LiuYan]([Title],[Message],[NickName],[IsAnonymous],[IPAddress]) values(@Title,@Message,@NickName,@IsAnonymous,@IPAddress)";
                    SqlParameter[] sqlParas = new SqlParameter[]{ 
                        new SqlParameter("@Title",strTitle),
                        new SqlParameter("@Message",strMsg),
                        new SqlParameter("@NickName",strNickName),
                        new SqlParameter("@IsAnonymous",FlagIsAnonymous),
                        new SqlParameter("@IPAddress",strIPAddress) 
                    };
                    int numSucc = 0;
                    numSucc = sqlH.ExecuteNonQuery(sqlInsert, sqlParas, CommandType.Text);
    
                    if (numSucc == 1)
                    {
                        context.Response.Redirect("MessageShow.ashx");
                    }
                    else
                    {
                        context.Response.Write("留言失败");
                    }
                    //strReminder = "发表留言成功";
                    //DataTable dt = sqlH.ExecuteQuery(sqlMsg, CommandType.Text);
                    //var data = new { PageTitle = "发表您的留言", MsgList = dt.Rows, PageReminder = strReminder };
                    //html = Common_Nvelocity.RenderHTML("MessageNew.html", data);
                    //context.Response.Write(html);
                }
    
    
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    View Code
    #parse("Head.html")
    
    <p>$data.PageTitle</p>
    <form action="MessageNew.ashx" method="post">
        <table>
    
            <tbody>
                <tr>
                    <td>昵称</td>
                    <td>
                        <input name="NickName" /><input type="checkbox" name="IsAnonymous" /><lable for="IsAnonymous">匿名</lable></td>
                </tr>
                <tr>
                    <td>标题</td>
                    <td>
                        <input type="text" name="Title" /></td>
                </tr>
                <tr>
                    <td>内容</td>
                    <td>
                        <textarea cols="50" rows="5" name="Message"></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" name="Save" value="发表" /></td>
                </tr>
            </tbody>
        </table>
    
    </form>
    
    #parse("Foot.html")
    View Code

    5,添加 留言查看对应的处理程序和对应的HTML模板页面: MessageShow.ashx , MessageShow.html 实现显示留言列表

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    
    namespace LiuYanban
    {
        /// <summary>
        /// MessageShow 的摘要说明
        /// </summary>
        public class MessageShow : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/html";
    
    
                SQLHelper sqlH = new SQLHelper();
                string sqlMsg = " select * FROM [LiuYanbanDB].[dbo].[LiuYan] order by PostDate desc";
                DataTable dt = sqlH.ExecuteQuery(sqlMsg, CommandType.Text);
                var data = new { PageTitle = "留言列表", MsgList = dt.Rows };
                string html = Common_Nvelocity.RenderHTML("MessageShow.html", data);
                context.Response.Write(html);
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    View Code
    #parse("Head.html")
    <style type="text/css">
        table {
            border-collapse: collapse;
            border: none;
             800px;
        }
    
        td {
            border: solid #000 1px;
        }
    </style>
    <h1>$data.PageTitle</h1>
    <a href="MessageNew.ashx">新增留言</a><br />
    
    <table>
        #foreach($Msg in $data.MsgList)
                    <tr>
                        <td>昵称:</td>
                        <td>$Msg.NickName</td>
                        <td>发表时间:</td>
                        <td>$Msg.PostDate</td>
                        <td>IP地址:</td>
                        <td>$Msg.IPAddress</td>
                    </tr>
        <tr>
            <td>留言标题:</td>
            <td colspan="5">$Msg.Title</td>
        </tr>
        <tr>
            <td>留言内容:</td>
            <td colspan="5">$Msg.Message</td>
        </tr>
        #end
    </table>
    #parse("Foot.html")
    View Code

    简单留言板Demo下载

    坚持,菜鸟终有一天变大牛!
  • 相关阅读:
    刚子扯谈 活着 没那么简单
    改写整数
    刚子扯谈:一起聊聊微信这孙子
    刚子扯谈:未完待续的微信5.0
    Citrix 服务器虚拟化之十 Xenserver高可用性HA
    JS实现——俄罗斯方块
    一种文件捆绑型病毒研究
    XP系统登录界面,需要手动点击用户帐户后才会出现输入密码的界面
    加密javascript代码
    Python的在线编辑环境
  • 原文地址:https://www.cnblogs.com/chengzish/p/4506949.html
Copyright © 2020-2023  润新知