SimpleNVelocityHandler.cs :  » Template-Engines » NVelocity » SimpleHttp » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Template Engines » NVelocity 
NVelocity » SimpleHttp » SimpleNVelocityHandler.cs
using System;
using System.IO;
using System.Web;

using Commons.Collections;

using NVelocity;
using NVelocity.Context;
using NVelocity.Http;

namespace SimpleHttp{

    /// <summary>
    /// Simple handler that show how you can load a template from the same path as the ApplicationPath
    /// </summary>
    public class SimpleNVelocityHandler : NVelocityHandler {

  public SimpleNVelocityHandler() {}

  /// <summary>
  /// Override HandleRequest method so that what is put in the context and what template
  /// is loaded can be determined by this application.
  /// </summary>
  /// <param name="request"></param>
  /// <param name="response"></param>
  /// <param name="ctx"></param>
  /// <returns></returns>
  protected override Template HandleRequest(HttpContext context, IContext ctx ) {

      // invoke handleRequest
      // request.Path holds the entire path to the file from the root of the web (i.e. /nvelocity/examples/simplehttp/foo/bar/template.vm)
      // request.ApplicationPath should hold the path to the root of this application (i.e. /nvelocity/examples/simplehttp)
      // the difference between the (changeing case to lower as they may not be the same) is the path from the root to the file (i.e. /foo/bar/template.vm)
      String template = context.Request.Path;
      if (template.ToLower().StartsWith(context.Request.ApplicationPath.ToLower())) {
    template = template.Substring(context.Request.ApplicationPath.Length)
    ;
      }

      ctx.Put("page", context.Request.Path);
      ctx.Put("template", template)
      ;

      Template t = GetTemplate(template)
      ;
      if (t == null) {
    throw new System.Exception ("HandleRequest(HttpContext, IContext) returned null - no template selected!" );
      }

      return t;
  }


  /// <summary>
  /// overridden LoadConfiguration that will create a properties file on the fly
  /// </summary>
  /// <returns></returns>
  protected override ExtendedProperties LoadConfiguration() {
      ExtendedProperties p = new ExtendedProperties();

      // override the loader path and log file locations based on where the physical application path is
      p.SetProperty(NVelocity.Runtime.RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, context.Request.PhysicalApplicationPath);
      p.SetProperty(NVelocity.Runtime.RuntimeConstants_Fields.RUNTIME_LOG,
        context.Request.PhysicalApplicationPath + p.GetString(NVelocity.Runtime.RuntimeConstants_Fields.RUNTIME_LOG, "nvelocity.log"));

      return p;
  }

    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.