(一)前言     

刚刚进入新公司,看公司的项目,发现开发流程几乎和以前的完全不同,再看看页面布局竟然没有发现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. centos7 ssh 设置key认证

    vi /etc/ssh/sshd_config 查找RSAAuthentication.StrictModes.PubkeyAuthentication.AuthorizedKeysFile把所在行修 ...

  2. 单例(Singleton)模式

    最简单的单例「饿汉式」  public class Singleton{ private static Singleton instance=new Singleton();    //other f ...

  3. PHP用mb_string函数库处理与windows相关中文字符

    昨天想批处理以前下载的一堆文件,把文件里的关键内容用正则匹配出来,集中处理.在操作文件时遇到一个问题,就是windows操作系统中的编码问题. 我们都知道windows中(当然是中文版),文件名和文件 ...

  4. 字典dictionary

    字典,即我们可以通过索引来查找更详细的内容.每个item都是由一对index:value构成的 索引可以有副本,但是试图根据存在副本的索引访问元素时,只会取最靠后的那个. 索引必须是immutable ...

  5. Android中dip、dp、sp、pt和px的区别

    1.概述 过去,程序员通常以像素为单位设计计算机用户界面.例如:图片大小为80×32像素.这样处理的问题在于,如果在一个每英寸点数(dpi)更高的新显示器上运行该程序,则用户界面会显得很小.在有些情况 ...

  6. Mac OSX下卸载Nodejs

    有时手贱看到新版本就升级,升级后发现一堆模块不能用了,心情好慢慢调,但也有调不好的时候,只能卸载重装低版本的node了. 我的机器环境如下 1. Mac OSX 10.10.3 2. node为0.1 ...

  7. 利用div实现遮罩层效果

    利用div实现遮罩层效果就是利用一个全屏.半透明的div遮住页面上其它元素,典型的例子就是百度的登录界面.下面贴出示例代码: <!DOCTYPE html> <html> &l ...

  8. STM32学习笔记——USART串口

    转载自:http://www.cnblogs.com/microxiami/p/3752715.html 一.USART简介 通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异 ...

  9. VMworld 2015 感受:VMware “Ready For Any”

    今年有机会参加在旧金山举行的 VMworld 2015.今天是正式开始的第一天.争取每天写一篇文章分享所见所听所感.第一天的主要活动,包括上午的 General Session,由 VMware 的几 ...

  10. [转]Hide or Remove jquery ui tab based on condition

    本文转自:http://stackoverflow.com/questions/19132970/hide-or-remove-jquery-ui-tab-based-on-condition 问: ...