/Files/icoolno1/nvelocity.rar
Environment requirements:. net framework 4.0
Velocity process:
Read template file-> parsing syntax-> merge variable execution-> output target file
Below is an example of JAVA:
/*
* Licensed to the Apache Software Foundation (ASF) under one
* Or more contributor license agreements. See the NOTICE file
* Distributed with this work for additional information
* Regarding copyright ownership. The ASF licenses this file
* To you under the Apache License, Version 2.0 (
* "License"); you may not use this file except T in compliance
* With the License. You may obtain a copy of the License
*
* Http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* Software distributed under the License is distributed on
* "As is" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for
* Specific language governing permissions and limitations
* Under the License.
*/
Import org. apache. velocity. app. Velocity;
Import org. apache. velocity. VelocityContext;
Import org. apache. velocity. Template;
Import org. apache. velocity. exception. ParseErrorException;
Import org. apache. velocity. exception. ResourceNotFoundException;
Import java. io .*;
Import java. util. ArrayList;
/**
* This class is a simple demonstration of how the Velocity Template Engine
* Can be used in a standalone application.
*
* @ Author <a href = "mailto: jvanzyl@apache.org"> Jason van Zyl </a>
* @ Author <a href = "mailto: geirm@optonline.net"> Geir Magnusson Jr. </a>
* @ Version $ Id: Example. java 463298 2006-10-12 16: 10: 32Z henning $
*/
Public class Example
{
Public Example (String templateFile)
{
Try
{
/*
* Setup
*/
Velocity. INIT ("velocity. properties ");
/*
* Make a context object and populate with the data. This
* Is where the velocity engine gets the data to resolve
* References (ex. $ list) in the template
*/
VelocityContext context = new VelocityContext ();
Context. put ("list", getNames ());
/*
* Get the Template object. This is the parsed version of your
* Template input file. Note that getTemplate () can throw
* ResourceNotFoundException: if it doesn't find the template
* ParseErrorException: if there is something wrong with the VTL
* Exception: if something else goes wrong (this is generally
* Indicative of as serious problem ...)
*/
Template template = null;
Try
{
Template = Velocity. getTemplate (templateFile); // obtain the template
}
Catch (ResourceNotFoundException rnfe)
{
System. out. println ("Example: error: cannot find template" + templateFile );
}
Catch (ParseErrorException pee)
{
System. out. println ("Example: Syntax error in template" + templateFile + ":" + pee );
}
/*
* Now have the template engine process your template using
* Data placed into the context. Think of it as a 'merge'
* Of the template and the data to produce the output stream.
*/
Bufferedwriter writer = new bufferedwriter (
New outputstreamwriter (system. Out ));
If (template! = NULL)
Template. merge (context, writer); // merge and output to the stream
/*
* Flush and cleanup
*/
Writer. flush ();
Writer. close ();
}
Catch (Exception e)
{
System. out. println (e );
}
}
Public ArrayList getNames ()
{
ArrayList list = new ArrayList ();
List. add ("ArrayList element 1 ");
List. add ("ArrayList element 2 ");
List. add ("ArrayList element 3 ");
List. add ("ArrayList element 4 ");
Return list;
}
Public static void main (string [] ARGs)
{
Example T = new example (ARGs [0]);
}
}
Below is a C # sample code:
StreamWriter sw = new StreamWriter (Path. Combine (AppDomain. CurrentDomain. BaseDirectory, "o.txt "));
Extendedproperties props = new extendedproperties ();
String tplpath = path. Combine (appdomain. currentdomain. basedirectory, "templates ");
Props. addproperty ("file. Resource. loader. Path", tplpath );
Props. addproperty ("input. encoding", "UTF-8 ");
Props. addproperty ("output. encoding", "UTF-8 ");
Props. addproperty ("velocimacro. Library", "vm_global_library.vm ");
Try
{
Velocity. INIT (props );
VelocityContext context = new VelocityContext ();
Context. Put ("test", new Class1 ());
Template template = Velocity. GetTemplate ("t.txt"); // obtain the Template
Template. Merge (context, sw); // Merge and output to the stream
}
Catch (Exception ex)
{
}
Sw. Flush ();
Sw. Close ();
The above two sections are basically the same. The main difference is that the loading configuration method is different. Of course, this difference is also the difference between the two languages.