zoukankan      html  css  js  c++  java
  • NVelocity学习笔记一——linq2sql+NVelocity完整demo

    (一)前言     

    刚刚进入新公司,看公司的项目,发现开发流程几乎和以前的完全不同,再看看页面布局竟然没有发现html。神马情况????一番探究发现使用了NVelocity模板引擎开发的。于是乎花了半天的时间比划着公司的项目做了一个小demo,也算是熟悉一下这种开发方式吧,同时也给将要学习这方面东西的朋友留个小教程。

    (二)实战     

    说明:因为公司项目数据库操作使用的是linq2sql,所以我也就拿这个操作数据库了(汗,其实我也没用过linq2SQL,都是EF,其实都差不多)。

    第一步:

    新建解决方案ProTest,不说了。

    第二步:

    新建类库LinqToSql,右击类库添加LinqToSql类。如图:

    第三步:

    将需要的表拖放到设计器上,会自动生成一些方法。

    第四步:

    新建一个操作数据库的实例类。

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LinqToSql
    {
        public class DBContextFactory
        {
            public static  DBDataContext GetDBContext()
            {
                return new DBDataContext(ConfigurationManager.ConnectionStrings["school"].ConnectionString);
            }       
        }
    }
    

    第五步:

    新建网站,引用类库linqtosql,同时添加System.Data.Linq.dll和NVelocity.dll类库(可以在官网http://nvelocity.codeplex.com/下载)。

    新建如下文件夹:

    ClassView类用于取数据,Template文件夹下为静态模板。如下:

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5     <meta name="description" content="">
     6     <meta name="keywords" content="">
     7     <title>$Title</title>   
     8 </head>
     9 
    10 <body>
    11   <table>
    12     #foreach($item in $ClassView.GetList(30))
    13     <tr><td>$item.Id</td><td>$item.Name</td></tr>
    14     #end
    15   </table>
    16 </body>
    17 </html>

    可以看出其中标记了一些NVelocity模板引擎的语法。

    第六步:

    清空aspx页面中的html代码,只保留第一行声明。如图:

    第七步:

    在aspx.cs页面中做如下处理:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 //
     8 using Commons.Collections;
     9 using NVelocity;
    10 using NVelocity.App;
    11 using NVelocity.Runtime;
    12 using System.Configuration;
    13 using System.Runtime.Remoting.Contexts;
    14 using NVelocity.Context;
    15 using System.IO;
    16 public partial class ClassList : System.Web.UI.Page
    17 {
    18     protected void Page_Load(object sender, EventArgs e)
    19     {
    20 
    21         //1.创建VelocityEngine实例对象
    22         VelocityEngine engine = new VelocityEngine();
    23 
    24         //2.读取模板路径,可以定义几套模板利于动态换肤
    25         string templateRoad = ConfigurationManager.AppSettings["tmpDir"];
    26 
    27         //3.使用设置初始化VelocityEngine
    28         ExtendedProperties props = new ExtendedProperties();
    29         props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
    30         props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
    31         props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
    32         props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Server.MapPath(templateRoad));
    33         engine.Init(props);
    34 
    35         //4.模板引擎从文件中读取模板
    36         Template tmp = engine.GetTemplate(@"class_tmp.vm");
    37 
    38         //5.为模板变量赋值
    39         IContext context = new VelocityContext();
    40         context.Put("Title", "first nvelocity");
    41         context.Put("ClassView",new ClassView());
    42 
    43         //6.合并模板和流写出器
    44         StringWriter write = new StringWriter();
    45         tmp.Merge(context, write);
    46         Response.Write(write.ToString());
    47         
    48     }
    49 }

    第八步:

    结果如下:

    (三)总结

    这里面的东西其实还有很多,这里我也是刚刚了解………………

  • 相关阅读:
    LRU Algorithm Gym
    Running Routes Kattis
    Box HDU
    manjaro 安装 tim 后无法输入中文
    Angle Beats Gym
    Fish eating fruit 沈阳网络赛(树形dp)
    请求接口模板
    Droppable 拖拽实例
    线程处理
    网站的配置文件XML读写
  • 原文地址:https://www.cnblogs.com/eggTwo/p/3822471.html
Copyright © 2011-2022 走看看