Spring.NET学习笔记26——整合NVelocity(应用篇) Level 200

来源:互联网 发布:怎么编写一个软件 编辑:程序博客网 时间:2024/05/19 16:36

  NVelocity是一个基于.NET的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由.NET代码定义的对象。

当NVelocity应用于web开发时,界面设计人员可以和.NET程序开发人员同步开发一个遵循MVC架构的web站点,也就是说,页面设计人员可以只关注页面的显示效果,而由.NET程序开发人员关注业务逻辑编码。NVelocity将.NET代码从web页面中分离出来,这样为web站点的长期维护提供了便利,同时也为我们在aspx之外又提供了一种可选的方案。NVelocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。NVelocity也可以为很多web开发架构提供模板服务(template service)。我们的系统就提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。

  相关语法请查看Velocity手册

 

  Spring.NET集成NVelocity的方法封装在Spring.Template.Velocity.dll程序集里,调用NVelocity的AIP的类是Spring.Template.Velocity.VelocityEngineUtils,

该类有两个方法MergeTemplate和MergeTemplateIntoString,分别为把模板调换为文件和字符串。以下是我实现整合NVelocity的代码:

 

 


        private string GetTemplateString()
        {
            WebApplicationContext ctx 
= ContextRegistry.GetContext() as WebApplicationContext;

            IList
<Person> list = this.GetPersonList();
            var templatePage 
= new
            {
                Title 
= "强大的NVelocity模板引擎",
                List 
= list,
                OutputDate 
= DateTime.Now,
                CopyRight 
= "博客园-刘冬.NET"
            };

            Hashtable modelTable 
= new Hashtable();
            modelTable.Add(
"TemplatePage", templatePage);
            VelocityContext velocityContext 
= new VelocityContext(modelTable);
            VelocityEngine velocityEngine 
= ctx.GetObject("VelocityEngine"as VelocityEngine;
            
string mergedTemplate = VelocityEngineUtils.MergeTemplateIntoString(velocityEngine, "PersonList.htm", Encoding.UTF8.WebName, modelTable);

            
return mergedTemplate;
        }

 

 


<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" xmlns:nv="http://www.springframework.net/nvelocity">

  
<nv:engine id="VelocityEngine" >
    
<nv:nvelocity-properties>
      
<!--输入编码-->
      
<entry key="input.encoding"  value="UTF-8"/>
      
<!--输出编码-->
      
<entry key="output.encoding" value="UTF-8"/>
    
</nv:nvelocity-properties>
    
<nv:resource-loader>
      
<nv:spring uri="assembly://Template/Template.HtmlTemplate/"/>
    
</nv:resource-loader>

  
</nv:engine>

</objects>

 


<!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>
    
<title>$TemplatePage.Title</title>
</head>
<body>
    
<table><thead>
        
<tr>
            
<th>序号</th>
            
<th>编号</th>
            
<th>姓名</th>
            
<th>年龄</th>
            
<th>性别</th>
        
</tr>
        
</thead>
        
<tbody>
            #set( $sn = 0 )
            #foreach( $item in $TemplatePage.List ) 
            
<tr>#set( $sn = $sn + 1 )
                
<td>$sn</td>
                
<td>$item.PersonID</td>
                
<td>$item.Name</td>
                
<td>$item.Age</td>
                
<td>#if($item.Sex) 男 #else 女 #end</td>
            
</tr>
            #end
        
</tbody>
    
</table>
    
<br />
    
<div>
        
<label>版权所有:</label>
        
<label>$TemplatePage.CopyRight</label>
    
</div>
        
<div>
        
<label>生成日期:</label>
        
<label>$TemplatePage.OutputDate.ToString("yyyy年MM月dd日")</label>
    
</div>
    
<br />
    
<href="/Default.aspx">返回首页</a>
</body>
</html>

 

  输出效果:

 

 

  代码下载

 

  返回目录

Tag标签: Spring.NET,NVelocity
原创粉丝点击