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, ...
随机推荐
- java自定义Annotation(载自百度文库)
java中自定义annotation需要@interface关键字和用到几个内置annotation. 用到的注解有@Target,@Retention,@Documented,@Inherited ...
- Hadoop+MongoDB的四种方案
背景: 公司核心业务库现存在MongoDB中,分布在6台MongoDB节点.现面临如下问题: 1.最大的一张表有10多个G,MongoDB在查询方面尚能胜任,但是涉及到复杂计算时会比较吃力. 2.Mo ...
- SQL Server求解最近多少销售记录的销售额占比总销售额的指定比例
看园中SQL Server大V潇潇隐者的博文,发现一边文就是描述了如标题描述的问题. 具体的问题描述我通过潇潇隐者的博文的截图来阐释: 注意:如果以上截取有所侵权,也请作者告知,再次感谢. 当 ...
- 烂泥:gpg加解密软件学习
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 为什么要学习gpg呢?因为要在Linux下把一个邮箱的密码加密,不让其他人看到该邮箱真 ...
- 深度学习笔记------windows系统下进行Linux-Ubuntu14.04双系统安装笔记(二)
在上一篇文章中介绍了新手如何安装Ubuntu14.04的双系统,本文会说明Ubuntu系统下搜狗输入法的安装,并就我遇见的一些bug给出最简单的解决办法. 第一部分.搜狗输入法的安装 本身搜狗输入法的 ...
- 时间同步出现ntpdate[1788]: the NTP socket is in use, exiting
问题: [root@service ~]# ntpdate ntp.api.bz 17 Jun 19:22:23 ntpdate[2701]: the NTP socket is in use, ex ...
- iframe 的使用和登陆退出的实现——整个页面跳转
iframe中如果只是页面跳转的话,我们依然只是部分的加载的了,为了实现整个页面的所有内容跳转,下面提供了整个页面跳转的方法. iframe例子 1.总的iframe页面(访问就访问这个) all. ...
- Server Tomcat v7.0 Server at localhost failed to start.临时解决办法
错误名:Server Tomcat v7.0 Server at localhost failed to start. 解决办法:去掉下面这句话: (通常在代码开头部分,public class前) ...
- 【OpenWRT之旅】LuCI探究
1. 多语言 1)检查: opkg list | grep luci-i18n- 2)安装语言包: opkg install luci-i18n-hungarian 2.uhttpd 这个是LuC ...
- 实现跨云应用——基于DNS的负载均衡
“公有云可以作为传统IT资源的延展,能帮助客户应对不断变化的需求”——这是我们在向客户介绍公有云产品时经常说的一句话.我们来看一个具体的需求: 某客户有一个web站点,部署在自有的数据中心(on-pr ...