Solved the "unable to find resource" XXX "error when loading the nvelocity template.

Source: Internet
Author: User

Nvelocity now belongs to the castle project. Introduction: http://www.castleproject.org/others/nvelocity/index.html

Usage: http://www.castleproject.org/others/nvelocity/usingit.html

It is found that many people will encounter the unable to find resource "XXX" error when using the above method.

Solution:

Used before engine. INIT (props );

Props. addproperty ("file. Resource. loader. Path", server. mappath ("."));

The 2nd parameter server. mappath (".") indicates the directory where the current template file is located. If the complete path of your template file is: C: \ XX. VM

Replace server. mappath (".") with c: \ xx.

ExampleCode:

Myfirsttemplate. VM and nvelocitydemo. aspx are in the same directory.

1. myfirsttemplate. VM file:

From: $ from
To: $
Subject: $ subject

Hello, $ customer

2. nvelocitydemo. aspx. CS code:

Using commons. collections;
Using nvelocity;
Using nvelocity. app;
Using nvelocity. context;
Using system. IO;
Using system. text;
Namespace webtest
{
Public partial class nvelocitydemo: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Velocityengine engine = new velocityengine ();
Extendedproperties props = new extendedproperties ();
// Server. mappath (".")
Props. addproperty ("file. Resource. loader. Path", server. mappath ("."));

Engine. INIT (props );

Template template = engine. gettemplate ("myfirsttemplate. VM ");

Velocitycontext context = new velocitycontext ();
Context. Put ("from", "Somewhere ");
Context. Put ("to", "someone ");
Context. Put ("subject", "Welcome to nvelocity ");
Context. Put ("customer", "Jon Zhao ");

Stringwriter writer = new stringwriter ();
Template. Merge (context, writer );
Response. Write (writer. getstringbuilder (). tostring ());

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.