Velocity Merge as a tool class two common scenarios for loading templates from web contexts and jars

Source: Internet
Author: User

Many times, the reasons for convenience or compromise, or versatility or limitation, are the result of template generation, which describes two scenarios for using velocity merge, the first of which is the same as spring MVC. Place the template under the path specified by the Velocityconfigurer property, such as:

<bean id= "Velocityconfigurer" class= "Org.springframework.web.servlet.view.velocity.VelocityConfigurer" >
<property name= "Resourceloaderpath" value= "/web-inf/views/"/>
<property name= "configlocation" value= "/web-inf/conf/velocity.properties"/>
</bean>

This time, as long as the template is placed under the views, and then directly through the definition of the bean MVC context can be, as follows:

map<string, object> ctx = new hashmap<string, object> ();
Ctx.put ("Appsnaps", list);
String result = velocityengineutils.mergetemplateintostring (Super.getvelocityconfigurer (). Getvelocityengine (), Constants.flagment_path + "SNAP_ID_SELECT_OPTION.VM", "GBK", CTX);

This is a convenient way to use under the Web, and it is recommended.

There are times when we may need to publish the app as a jar, where the template is packaged in a jar and cannot be merged directly using the tool class described above. The context information needs to be passed to Velocityengine as follows:

Properties Properties=new properties ();
Properties.setproperty ("Resource.loader", "jar");
Properties.setproperty ("Jar.resource.loader.class", " Org.apache.velocity.runtime.resource.loader.JarResourceLoader ");
Properties.setproperty ("Jar.resource.loader.path", "Jar:file:/web-inf/lib/xxx.jar");
Velocityengine velocityengine=new Velocityengine (properties);

Velocitycontext context=new Velocitycontext ();
StringWriter writer=new StringWriter ();
The directory structure of the YYY.VM template Vm.jar loaded from/web-inf/lib/xxx.jar is VM/YYY.VM
Velocityengine.mergetemplate ("Vm/hello.vm", "GBK", context, writer);

Velocity Merge as a tool class two common scenarios for loading templates from web contexts and jars

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.