Velocity templates Use

Source: Internet
Author: User

The introduced JAR package:

1 Import org.apache.velocity.Template; 2 Import Org.apache.velocity.VelocityContext; 3 Import org.apache.velocity.app.Velocity; 4 Import Org.apache.velocity.app.VelocityEngine; 5 Import Org.apache.velocity.tools.generic.DateTool; 6 import Org.apache.velocity.tools.generic.NumberTool;

Jar package can be downloaded to the official website

http://velocity.apache.org/engine/devel/

Sample code:

1 /**2 * Generate VMS3      * @paramVmpackage The package name where the VM resides4      * @paramVmname5      * @paramListName6      * @paramObjects7      * @return8      * @throwsException9      */Ten      Public StaticString GENERATEVM (String vmpackage, String vmname, String listname, list<?extendsObject> objects)throwsexception{ One         if(Objects.size () >0){ AProperties p =NewProperties (); -String classpath = velocityutils.class. GetResource ("/"). GetPath ();//get the path to SRC -P.setproperty (Velocityengine.file_resource_loader_path, Classpath +vmpackage); the             //set the velocity encoding -P.setproperty (Velocity.encoding_default, "UTF-8");  -P.setproperty (velocity.input_encoding, "UTF-8");  -P.setproperty (velocity.output_encoding, "UTF-8");  +             //Initialize and get velocity engine -Velocityengine ve =Newvelocityengine (); + Ve.init (p); A             //get Velocity's template atTemplate T =NULL; -t =ve.gettemplate (vmname); -             //Get Velocity's contextual context -Velocitycontext context =NewVelocitycontext (); -Context.put ("Number",NewNumbertool ()); -Context.put ("Date",NewDatetool ()); in Context.put (ListName, objects); -             //output Stream toStringWriter writer =NewStringWriter (); +             //Conversion Output - T.merge (context, writer); the             returnwriter.tostring (); *}Else{ $             return NULL;Panax Notoginseng         } -}

The above function is called:

1String vmpagckage = "com/cpinfo/his/template/emr/";2String vmname = "PARTOGRAM_DETAIL_INFOS.VM";3String vm = VELOCITYUTILS.GENERATEGRIDVM (vmpagckage, Vmname, "BS", EMS);4             5Response.setcontenttype ("Text/xml;charset=utf-8");6PrintWriter PW =Response.getwriter ();7 Pw.print (VM);8 Pw.flush ();9Pw.close ();

Template file Content Examples:

1<?xml version= "1.0" encoding= "UTF-8"?>2<rows>3 #foreach ($b in $bs)4<row id= "$b. Detailinfoid" >5<cell title= "$!date.format (' Yyyy-mm-dd hh:mm ', $b. Time)" >$!date.format (' hh:mm ', $b. Time) </cell>6<cell>$!number.format ($b. Gongkoukaida) </cell>7<cell>$!number.format ($b. Xianluxiajiang) </cell>8#if($b. taiermianchu==1)9<cell> is </cell>Ten#Else One<cell> No </cell> A #end -<cell>$!b.taixinyin</cell> -<cell>$!b.mailv</cell> the#if($b. Xueyagao | |$b. Xueyadi) -<cell>$!b.xueyagao/$!b.xueyadi</cell> -#Else -<cell></cell> + #end -<cell>$!b.teshuzhiliao</cell> +<cell>$!b.qianming</cell> A<cell><! [Cdata[]]></cell> at</row> - #end -</rows>

Template file syntax, etc. can go to the official website to see the document.

Velocity templates Use

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.