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