Velocity模板技术学习笔记
2017-07-20 20:52
507 查看
基本语句语法可以查看https://www.ibm.com/developerworks/cn/java/j-lo-velocity1/
HelloVelocity.java
Hellovelocity.vm
输出:
velocity融合springmvc的配置
HelloVelocity.java
public class HelloVelocity { public static void main(String[] args) { //初始化并取得Velocity引擎 VelocityEngine ve = new VelocityEngine(); //设置一些配置 ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); //初始化引擎 ve.init(); //取得velocity的模板 Template t = ve.getTemplate("Hellovelocity.vm"); //取出velocity的上下文context VelocityContext ctx = new VelocityContext(); //把数据填入上下文 ctx.put("name", "velocity"); ctx.put("date", (new Date()).toString()); //创建一个list List temp = new ArrayList(); temp.add(1); temp.add(2); //将list填入上下文 ctx.put("list", temp); //输出流 StringWriter sw = new StringWriter(); //转化输出 t.merge(ctx, sw); System.out.println(sw.toString()); } }
Hellovelocity.vm
#set($iAmvariable = "good!") welcome $name to velocity.com today is $date. #foreach($i in $list) $i $velocityCount #end $iAmvariable
输出:
welcome velocity to velocity.com today is Thu Jul 20 20:50:09 CST 2017. 1 1 2 2 good!
velocity融合springmvc的配置
<!-- 配置velocity引擎 --> <bean id="velocityConfigurer" name="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <!--模板存放的路径 --> <property name="resourceLoaderPath"> <value>/WEB-INF/velocity</value> </property> <property name="velocityProperties"> <props> <prop key="input.encoding">UTF-8</prop> <!-- 指定模板引擎进行模板处理的编码 --> <prop key="output.encoding">UTF-8</prop> <!-- 指定输出流的编码 --> <prop key="contentType">text/html;charset=UTF-8</prop> <prop key="velocimacro.library">macro/macros.vm</prop> <!-- <prop key="eventhandler.referenceinsertion.class">com.sde.common.web.escape.reference.NoEscapeHtmlReference</prop> <prop key="eventhandler.noescape.html.match">/(?:screen_content)|(?:noescape_.*)/</prop> --> </props> </property> </bean> <!-- 配置视图解析器 --> <bean id="vmViewResolver" class="com.ailk.web.VelocityLayoutViewResolverExt"> <property name="order" value="1"/> <property name="cache" value="false" /> <property name="prefix" value="" /> <!--视图前缀,即存放路径 --> <property name="suffix" value=".vm" /> <!-- 视图文件的后缀名 --> <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml" /> <!--toolbox配置文件路径 --> <property name="dateToolAttribute" value="dateTool" /> <!--日期函数名称 --> <property name="numberToolAttribute" value="number" /> <!--数字函数名称 --> <property name="exposeSpringMacroHelpers" value="true" /> <!-- 是否使用spring对宏定义的支持 --> <property name="exposeRequestAttributes" value="true" /> <!--是否开放request属性 --> <property name="requestContextAttribute" value="rc" /> <!--request属性引用名称 --> <!-- <property name="layoutUrl" value="templates/layout/default.vm"/> --><!--指定layout文件 --> <property name="contentType" value="text/html;charset=UTF-8" /> <!-- 页面内容的字符编码 --> <property name="layoutUrl" value="layout/layout.vm" /> <!-- 设置系统默认的模板路径 --> <property name="layoutKey" value="layout" /> <!-- 设定模板文件键值,设定该值后可以在vm文件种使用 该键值设置模板路径 --> <property name="screenContentKey" value="screen_content" /> <!-- 指定vm文件显示位置 --> <property name="attributes"> <props> <prop key="relase">true</prop><!-- relase;debug --> </props> </property> </bean>
相关文章推荐
- Velocity学习笔记3——Velocity 模板语言(VTL: Velocity Template Language)简介
- Velocity学习笔记5——VTL模板中的注释
- 模板引擎 NVelocity 初步学习笔记
- ThinkPHP学习笔记(二):ThinkPHP框架的模板技术
- mangos 学习笔记之一 ------ 模板技术使用
- Java学习笔记27:模板Velocity使用方法
- 闪回技术(学习笔记)
- 【C++】学习笔记草稿版18(模板)
- 虚拟机VMWare学习笔记之一 ― 虚拟化技术的优势
- VC++技术内幕』学习笔记(10)
- ThinkPHP学习笔记(十三)多模板支持和多语言支持
- Java核心技术(第8版)学习笔记_继承(2)_反射
- Python OpenCV学习笔记之:图像模板匹配
- C++ Template学习笔记之函数模板(6)——模板显式特化
- 驾驶技术学习笔记第八讲 公路掉头和靠边停车
- java web Servlet 学习笔记 -3 会话管理技术
- Asp.net控件开发学习笔记(十一)----服务器控件模板
- html5学习笔记 1 文档模板
- JAVA核心技术学习笔记(第七版,Ⅱ高级特性)
- 流媒体技术学习笔记之(九)减少VLC 延迟的方法