velocity merge作为工具类从web上下文和jar加载模板的两种常见情形
2016-09-21 13:09
246 查看
很多时候,处于各种便利性或折衷或者通用性亦或是限制的原因,会借助于模板生成结果,在此介绍两种使用velocity merge的情形,第一种是和spring mvc一样,将模板放在velocityConfigurer属性指定的路径下,如:
<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>
这个时候,只要模板放在views下,然后直接借助于定义这个bean的mvc context即可,如下:
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);
这是在web下使用比较便利的一种方式,也是推荐的。
还有些时候,我们可能需要以jar方式发布应用,此时模板也要打包到jar中,无法直接使用上述工具类进行合并。这时候需要给velocityengine传递上下文信息,如下所示:
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();
//从 /WEB-INF/lib/XXX.jar中加载YYY.vm模板 vm.jar的目录结构为vm/YYY.vm
velocityEngine.mergeTemplate("vm/hello.vm", "gbk", context, writer);
<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>
这个时候,只要模板放在views下,然后直接借助于定义这个bean的mvc context即可,如下:
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);
这是在web下使用比较便利的一种方式,也是推荐的。
还有些时候,我们可能需要以jar方式发布应用,此时模板也要打包到jar中,无法直接使用上述工具类进行合并。这时候需要给velocityengine传递上下文信息,如下所示:
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();
//从 /WEB-INF/lib/XXX.jar中加载YYY.vm模板 vm.jar的目录结构为vm/YYY.vm
velocityEngine.mergeTemplate("vm/hello.vm", "gbk", context, writer);
相关文章推荐
- 嵌入式Linux2.6 Kernel Module模板动态加载实例和常见问题解决方法
- 使用web.xml方式加载 Spring时,获取Spring context的两种方式:
- java web项目中classes文件夹下的class和WEB-INF/lib中jar里的class文件加载顺序
- 应用Tomcat的WebappClassLoader加载指定目录的jar文件
- web前端 js中函数的两种常见写法
- WEB中加载ApplicationContext的两种方式
- 【Eclipse_部署web项目篇】修改eclipse默认指向的WebContent目录作为启动运行加载
- Android开发模板------WebView加载时显示ProgressBar进度条
- 38-UI基础常见问题(UIButton和UIImage区别,UIImage两种加载方式区别,frame,bounds,center作用和区别)
- WebView加载页面,常见的问题
- 如何让weblogic优先加载WEB-INF目录下的jar包。而非weblogic 自带的lib包
- 两种必须使用显示模板实参的情形
- java web项目中classes文件夹下的class和WEB-INF/lib中jar里的class文件加载顺序
- java web项目中classes文件夹下的class和WEB-INF/lib中jar里的class文件加载顺序
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
- jstl学习常见错误一: The absolute uri: http://java.sun.com/jst/core cannot be resolved in either web.xml or the jar files deployed with
- ios: webview 中 加载状态显示两种方法
- webbug xerces.jar导致加载struts加载错误
- web图片延时加载两种方法
- webView 加载页面的两种请求方式: get 和 post