(一)前言     

刚刚进入新公司,看公司的项目,发现开发流程几乎和以前的完全不同,再看看页面布局竟然没有发现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文件夹下为静态模板。如下:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="">
<meta name="keywords" content="">
<title>$Title</title>
</head> <body>
<table>
#foreach($item in $ClassView.GetList(30))
<tr><td>$item.Id</td><td>$item.Name</td></tr>
#end
</table>
</body>
</html>

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

第六步:

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

第七步:

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;
using System.Configuration;
using System.Runtime.Remoting.Contexts;
using NVelocity.Context;
using System.IO;
public partial class ClassList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ //1.创建VelocityEngine实例对象
VelocityEngine engine = new VelocityEngine(); //2.读取模板路径,可以定义几套模板利于动态换肤
string templateRoad = ConfigurationManager.AppSettings["tmpDir"]; //3.使用设置初始化VelocityEngine
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Server.MapPath(templateRoad));
engine.Init(props); //4.模板引擎从文件中读取模板
Template tmp = engine.GetTemplate(@"class_tmp.vm"); //5.为模板变量赋值
IContext context = new VelocityContext();
context.Put("Title", "first nvelocity");
context.Put("ClassView",new ClassView()); //6.合并模板和流写出器
StringWriter write = new StringWriter();
tmp.Merge(context, write);
Response.Write(write.ToString()); }
}

第八步:

结果如下:

(三)总结

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

NVelocity学习笔记一——linq2sql+NVelocity完整demo的更多相关文章

  1. JPG学习笔记3(附完整代码)

    #topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...

  2. JPG学习笔记4(附完整代码)

    #topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...

  3. Cesium学习笔记(七):Demo学习(自由控制飞行的飞机)[转]

    https://blog.csdn.net/umgsoil/article/details/74923013# 这是官方的教程Demo,名字叫Use HeadingPitchRoll,顾名思义,就是教 ...

  4. python学习笔记十四:wxPython Demo

    一.简介 wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的.功能键全的GUI用户界面. wxPython是作为优秀的跨平台GUI库wxWidgets ...

  5. webrtc学习笔记2(Android端demo代码结构)

    最近正在修改webrtc的Android端demo和服务器交互的内容,介绍一下demo的大体结构吧,以便能快速回忆. 环境:Android5.0以上.libjingle_peerconnection_ ...

  6. Django学习笔记----settings and database_based App demo

    原文参考docs.djangoproject.com, what can manage.py utility do? find here. 1.Database Setup 编辑settings.py ...

  7. 大数据学习笔记——Spark完全分布式完整部署教程

    Spark完全分布式完整部署教程 继Mapreduce之后,作为新一代并且是主流的计算引擎,学好Spark是非常重要的,这一篇博客会专门介绍如何部署一个分布式的Spark计算框架,在之后的博客中,更会 ...

  8. JPG学习笔记1(附完整代码)

    #topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...

  9. JPG学习笔记2(附完整代码)

    #topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...

随机推荐

  1. Java并发之工具类 ForkJoin 任务分解

    Fork/Join框架的介绍 第一步分割任务.首先我们需要有一个fork类来把大任务分割成子任务,有可能子任务还是很大,所以还需要不停的分割,直到分割出的子任务足够小. 第二步执行任务并合并结果.分割 ...

  2. js实现页面跳转的几种方式

    第一种:    <script language="javascript" type="text/javascript">           wi ...

  3. php遍历循环数组实现方法

    简单利用foreach for list each while来遍历数组,包括普通的一维数组与二维数组遍历方法,下面详细的介绍了每个函数的使用方法. $foreach = array(1,2,3); ...

  4. Android 的 Handler 总结

    <一> Handler的定义: 主要接受子线程发送的数据, 并用此数据配合主线程更新UI. 解释: 当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管 ...

  5. android 动态设置Framelayout,view,imageView,Layout高度

    直接:测试可以 Framelayout.getLayoutParams().width=600;Framelayout.getLayoutParams().height=400; 如:view,ima ...

  6. C语言中链表怎么删除结点?

    第一个方法: /*根据姓名删除链表的中的学生记录*/ void deleteByName(struct STUDENT * head) { struct STUDENT *p,*q; ]; if(he ...

  7. FastCV安装报错---LaunchAnyWhere错误:载入Java VM时Windows出现错误:2

  8. 微信小程序开发初体验--教你开发小程序

    微信小程序 微信小程序面世以来受到的关注颇多,直到最近我才动手尝试进行了小程序的开发,总体上感觉还是不错的,有一点不适应的就是要摆脱Web APP开发对DOM的操作.在这里我就把我是如何利用API开发 ...

  9. Windows Azure文件共享服务--File Service

    部署在Windows Azure上的虚拟机之间如何共享文件?例如:Web Server A和Web Server B组成负载均衡集群,两个服务器需要一个共享目录来存储用户上传的文件.通常,大家可能首先 ...

  10. [转]ASP.NET MVC IOC 之AutoFac攻略

    本文转自:http://www.cnblogs.com/WeiGe/p/3871451.html 一.为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用A ...