Velocity模板技术学习笔记
来源:互联网 发布:并行计算的编程模型 编辑:程序博客网 时间:2024/06/03 22:32
基本语句语法可以查看https://www.ibm.com/developerworks/cn/java/j-lo-velocity1/
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.comtoday is $date.#foreach($i in $list)$i$velocityCount#end$iAmvariable
输出:
welcome velocity to velocity.comtoday is Thu Jul 20 20:50:09 CST 2017.1122good!
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>
阅读全文
0 0
- Velocity模板技术学习笔记
- Java学习笔记27:模板Velocity使用方法
- 模板技术Velocity应用
- velocity模板技术
- Velocity学习笔记3——Velocity 模板语言(VTL: Velocity Template Language)简介
- velocity模板学习
- Velocity模板学习
- velocity模板引擎学习
- 模板技术-freemarker与velocity
- Velocity学习笔记5——VTL模板中的注释
- Velocity模板引擎(template engine)学习笔记二
- Velocity技术学习
- velocity模板引擎学习(1)
- velocity模板引擎学习(1)
- Velocity模板相关学习资料
- velocity学习笔记
- velocity学习笔记
- Velocity学习笔记(一)
- python---模块与包
- Builder设计模式和AlertDialog的源码分析
- zoj 2100 DFS
- a链接的访问状态
- <div class="article_content tracking-ad" id="article_content" data-mod="popu_307" data-dsm="post"> <
- Velocity模板技术学习笔记
- 初学者的Selenium自动化测试指南,基于Python(七)——cookie相关操作
- 单片机应用程序架构
- POJ 2749 Building roads (2-SAT)
- Spring源码学习——AbstractApplicationContext
- Android新特性介绍,ConstraintLayout完全解析
- Oracle学习笔记(六)
- SceneLayer的见解
- form表单内容