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文件夹下为静态模板。如下:
<!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的更多相关文章
- JPG学习笔记3(附完整代码)
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...
- JPG学习笔记4(附完整代码)
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...
- Cesium学习笔记(七):Demo学习(自由控制飞行的飞机)[转]
https://blog.csdn.net/umgsoil/article/details/74923013# 这是官方的教程Demo,名字叫Use HeadingPitchRoll,顾名思义,就是教 ...
- python学习笔记十四:wxPython Demo
一.简介 wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的.功能键全的GUI用户界面. wxPython是作为优秀的跨平台GUI库wxWidgets ...
- webrtc学习笔记2(Android端demo代码结构)
最近正在修改webrtc的Android端demo和服务器交互的内容,介绍一下demo的大体结构吧,以便能快速回忆. 环境:Android5.0以上.libjingle_peerconnection_ ...
- Django学习笔记----settings and database_based App demo
原文参考docs.djangoproject.com, what can manage.py utility do? find here. 1.Database Setup 编辑settings.py ...
- 大数据学习笔记——Spark完全分布式完整部署教程
Spark完全分布式完整部署教程 继Mapreduce之后,作为新一代并且是主流的计算引擎,学好Spark是非常重要的,这一篇博客会专门介绍如何部署一个分布式的Spark计算框架,在之后的博客中,更会 ...
- JPG学习笔记1(附完整代码)
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...
- JPG学习笔记2(附完整代码)
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...
随机推荐
- CentOS vsftp安装与配置
详细配置说明:. http://www.cnblogs.com/app-lin/p/5189762.html 1.安装vsftpd yum install vsftpd 2.启动/重启/关闭vsftp ...
- Access restriction: The type 'RSACipher' is not API
解决方法: http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-re ...
- Autofac全面解析系列(版本:3.5) – [使用篇(推荐篇):1.类型注册]
前言 Autofac Autofac是一套高效的依赖注入框架. Autofac官方网站:http://autofac.org/ Autofac在Github上的开源项目:https://github. ...
- Android ViewPager再探:增加滑动指示条
上一篇:<Android ViewPager初探:让页面滑动起来> ViewPager只是左右滑动有些丑,也不知道当前位于第几页面. 可以在上方加入滑动指示条,来确定当前位置. 只需要修改 ...
- openfire+asmack搭建的安卓即时通讯(七) 15.5.27
本地化之章! 往期传送门: 1.http://www.cnblogs.com/lfk-dsk/p/4398943.html 2.http://www.cnblogs.com/lfk-dsk/p/441 ...
- Core Dump [Linux]
I have known Core-Dump for a long time. But I just know it little. Now, I am about to study it.
- 【HTML5】嵌入另一张HTML文档、通过插件嵌入内容、嵌入数字表现形式
1.嵌入另一张HTML文档 iframe 元素允许在现有的HTML文档中嵌入另一张文档.下面代码展示了iframe元素的用法: <!DOCTYPE html> <html lang= ...
- 三维网格形变算法(Gradient-Based Deformation)
将三角网格上的顶点坐标(x,y,z)看作3个独立的标量场,那么网格上每个三角片都存在3个独立的梯度场.该梯度场是网格的微分属性,相当于网格的特征,在形变过程中随控制点集的移动而变化.那么当用户拖拽网格 ...
- Vijos1451圆环取数[环形DP|区间DP]
背景 小K攒足了路费来到了教主所在的宫殿门前,但是当小K要进去的时候,却发现了要与教主守护者进行一个特殊的游戏,只有取到了最大值才能进去Orz教主…… 描述 守护者拿出被划分为n个格子的一个圆环,每个 ...
- OAuth2授权原理
最近在做第三方接入的,初步定下使用OAuth2协议,花了些时间对OAuth2的授权方式做了些了解. 我还记得一两年前,跟一位同事聊起互联网时,当时我说过一个想法: 目前不少较为稀有的资源,很多都是论坛 ...