Velocity 语法及其在springMVC中的配置
强烈推荐具体的整合博客:http://blog.csdn.net/duqi_2009/article/details/47752169
整合文章中有几处问题:
- xml中配置的vm视图解析器,应该按照本文的配置来。
- spring的版本,使用3.1.1.RELEASE,mabatis的版本,建议使用3.1.1,mybatis-spring,建议使用1.1.1。
Velocity是一个基于Java的模板引擎(template engine),它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。作为一个比较完善的模板引擎,Velocity的功能是比较强大的,但强大的同时也增加了应用复杂性。
一、基本语法
1、”#”用来标识Velocity的脚本语句,包括#set、#if 、#else、#end、#foreach、#end、#iinclude、#parse、#macro等;
如:
#if($info.imgs)
<img src="$info.imgs" border=0>
#else
<img src="noPhoto.jpg">
#end
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
2、”$
“用来标识一个对象(或理解为变量);如
如:$i、$msg、$TagUtil.options(...)
等。
3、”{}”用来明确标识Velocity变量;
比如在页面中,页面中有一个$someonename
,此时,Velocity将把someonename
作为变量名,若我们程序是想在someone
这个变量的后面紧接着显示name
字符,则上面的标签应该改成${someone}name
。
4、”!”用来强制把不存在的变量显示为空白。
如当页面中包含$msg
,如果msg对象有值,将显示msg的值,如果不存在msg对象同,则在页面中将显示$msg
字符。这是我们不希望的,为了把不存在的变量或变量值为null的对象显示为空白,则只需要在变量名前加一个“!”号即可。
如:$!msg
我们提供了五条基本的模板脚本语句,基本上就能满足所有应用模板的要求。这四条模板语句很简单,可以直接由界面设计人员来添加。在当前很多EasyJWeb的应用实践中,我们看到,所有界面模板中归纳起来只有下面四种简单模板脚本语句即可实现:
1、$!obj
直接返回对象结果。
如:在html标签中显示java对象msg的值。<p>$!msg</p>
在html标签中显示经过HtmlUtil对象处理过后的msg对象的值
<p>$!HtmlUtil.doSomething($!msg)</p>
- 1
- 1
2、#if($!obj) #else #end
判断语句
如:在EasyJWeb各种开源应用中,我们经常看到的用于弹出提示信息msg的例子。
#if($msg)
<script>
alert('$!msg');
</script>
#end
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
上面的脚本表示当对象msg对象存在时,输出<script>
等后面的内容。
3、#foreach( $info in $list) $info.someList #end
循环读取集合list中的对象,并作相应的处理。
如:EasyJF开源论坛系统中论(0.3)坛首页显示热门主题的html界面模板脚本:
#foreach( $info in $hotList1)
<a href="/bbsdoc.ejf?easyJWebCommand=show&&cid=$!info.cid" target="_blank">$!info.title</a><br>
#end
- 1
- 2
- 3
- 1
- 2
- 3
上面的脚本表示循环遍历hotList1集合中的对象,并输出对象的相关内容。
4、#macro(macroName)#end
脚本函数(宏)调用,不推荐在界面模板中大量使用。
如:在使用EasyJWeb Tools快速生成的添删改查示例中,可以点击列表的标题栏进行升降排序显示,这是我们在EasyJWeb应用中经常看到的一个排序状态显示的模板内容。
函数(宏)定义,一般放在最前面
#macro(orderPic $type)
#if ($orderField.equals($type))
<img src="/images/ico/${orderType}.gif">
#end
#end
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
具体的调用如:<font color="#FFFFFF">头衔#orderPic("title")</font>
5、包含文件#inclue("模板文件名")
或#parse("模板文件名")
主要用于处理具有相同内容的页面,比如每个网站的顶部或尾部内容。
使用方法,可以参考EasyJF开源Blog及EasyJF开源论坛中的应用!
如:#parse("/blog/top.html")
或#include("/blog/top.html")
parse与include的区别在于,若包含的文件中有Velocity脚本标签,将会进一步解析,而include将原样显示。
关于#set的使用
在万不得已的时候,不要在页面视图自己声明Velocity脚本变量,也就是尽量少使用#set。有时候我们需要在页面中显示序号,而程序对象中又没有包含这个序号属性同,可以自己定义。如在一个循环体系中,如下所示:
#set ($i=0)
#foreach($info in $list)
序号:$i
#set($i=$i+1)
#end
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
Velocity脚本语法摘要
1、声明:#set ($var=XXX)
左边可以是以下的内容
Variable reference
String literal
Property reference
Method reference
Number literal #set ($i=1)
ArrayList #set ($arr=["yt1","t2"])
算术运算符
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2、注释:
单行## XXX
多行#* xxx
xxxx
xxxxxxxxxxxx*#
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
References 引用的类型
3、变量 Variables
以 “$” 开头,第一个字符必须为字母。character followed by a VTL Identifier. (a .. z or A .. Z).
变量可以包含的字符有以下内容:
alphabetic (a .. z, A .. Z)
numeric (0 .. 9)
hyphen ("-")
underscore ("_")
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
4、Properties
$Identifier.Identifier
$user.name
hashtable user中的的name值.类似:user.get("name")
- 1
- 2
- 3
- 1
- 2
- 3
5、Methods
object user.getName() = $user.getName()
- 1
- 1
6、Formal Reference Notation
用{}把变量名跟字符串分开
如
#set ($user="csy"}
${user}name
- 1
- 2
- 1
- 2
返回csyname
$username
$!username
- 1
- 2
- 1
- 2
$
与$!
的区别
当找不到username的时候,$username
返回字符串”$username
“,而$!username
返回空字符串""
7、双引号 与 引号
#set ($var="helo")
test"$var" 返回testhello
test'$var' 返回test'$var'
- 1
- 2
- 3
- 1
- 2
- 3
可以通过设置 stringliterals.interpolate=false
改变默认处理方式
8、条件语句
#if( $foo )
<strong>Velocity!</strong>
#end
#if($foo)
#elseif()
#else
#end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
当$foo
为null或为Boolean对象的false值执行.
9、逻辑运算符:== && || !
10、循环语句#foreach($var in $arrays )
// 集合包含下面三种Vector, a Hashtable or an Array
#foreach( $product in $allProducts )
<li>$product</li>
#end
#foreach( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key)</li>
#end
#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
11、velocityCount变量在配置文件中定义
# Default name of the loop counter
# variable reference.
directive.foreach.counter.name = velocityCount
# Default starting value of the loop
# counter variable reference.
directive.foreach.counter.initial.value = 1
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
12、包含文件
#include( "one.gif","two.txt","three.htm" )
- 1
- 1
13、Parse导入脚本
#parse("me.vm" )
- 1
- 1
14、#stop
停止执行并返回
15、定义宏Velocimacros ,相当于函数 支持包含功能
定义:
#macro( d )
<tr><td></td></tr>
#end
调用
#d()
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
16、带参数的宏
#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
<tr><td bgcolor=$color>$something</td></tr>
#end
#end
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
17、Range Operator
#foreach( $foo in [1..5] )
- 1
- 1
三 Spring MVC 中 Volecity 的设置
<!-- 配置vm视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityLayoutView"/>
<property name="toolboxConfigLocation" value="/WEB-INF/classes/toolbox.xml"/>
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="prefix" value=""/>
<property name="suffix" value=".vm" />
<property name="layoutUrl" value="layout/default.vm"></property>
<property name="layoutKey" value="layout"></property>
<property name="exposeSpringMacroHelpers" value="true" />
<property name="screenContentKey" value="screen_content" />
<property name="exposeRequestAttributes" value="true" /><!-- if open request Attributes-->
<property name="requestContextAttribute" value="rc"/><!-- request Attribute name-->
<property name="dateToolAttribute"><value>dateTool</value></property>
<property name="numberToolAttribute"><value>numberTool</value></property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
解释:
配置layoutUrl设定系统默认的模板路径, layoutKey设定模板文件键值,设定该值后就可以在vm文件中使用该键值设置模板路径,screenContentKey表示指定vm文件显示位置
通过以上配置后普通页面velocity会自动套用layout/default.vm模板
如果登录页面需套用自己独特的模板则如下
可以在登录页面中添加:#set($layout="login_layout.vm")
则登录页面将套用”login_layout.vm”模板
<!-- 配置velocity引擎 -->
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/templates/" /><!-- 模板存放的路径 -->
<property name="configLocation" value="classpath:velocity.properties" />
</bean>
<!-- 配置视图的显示 -->
<bean id="ViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="prefix" value="/" /><!-- 视图文件的前缀,即存放的路径 -->
<property name="suffix" value=".vm" /><!-- 视图文件的后缀名 -->
<property name="toolboxConfigLocation" value="/WEB-INF/tools.xml" /><!--toolbox配置文件路径-->
<property name="dateToolAttribute" value="date" /><!--日期函数名称-->
<property name="numberToolAttribute" value="number" /><!--数字函数名称-->
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持-->
<property name="exposeRequestAttributes" value="true" /><!--是否开放request属性-->
<property name="requestContextAttribute" value="rc"/><!--request属性引用名称-->
<property name="layoutUrl" value="layout/default.vm"/><!--指定layout文件-->
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
tool.xml和velocity.properties 两个文件,在jar包中都可以找到的
velocity.jar velocity.properties -->> org.apache.velocity.runtime.defaults.velocity.properties
velocity-tool.jar tools.xml -->> org\apache\velocity\tools\generic\tools.xml 记得改下标签。
- 1
- 2
- 1
- 2
这些设置完了,还有一点非常重要,这点纠结了我很久 。
就是你明明已经都配置好了,自己都觉得没问题了,action都进了,最后跳到模板引擎去加载模板的时候,告诉你这个模板找不到!!
经过我仔细排查,是velocity.properties多了句配置,自己需要注释一下。
#file.resource.loader.path = .
- 1
- 1
将这句配置注释,这是说,路径为properties文件路径,但如果你的properties跟你的templates不在一个目录,这时候就会出问题了。我调进去看过,它默认就是templates目录,上面宏的地址,也是相对于templates的目录。
转自: http://blog.csdn.net/lllliulin/article/details/51659189
Velocity 语法及其在springMVC中的配置的更多相关文章
- SpringMVC中定时任务配置
在项目中使用定时任务是常有的事,比如每天定时进行数据同步或者备份等等. 以前在从事C语言开发的时候,定时任务都是通过写个shell脚本,然后添加到linux定时任务中进行调度的. 现在使用Spring ...
- 【转】SpringMVC中DispatcherServlet配置中url-pattern 配置/*和/的区别
原文地址:http://m.blog.csdn.net/blog/liuxiao723846/43733287 在使用springmvc时,都会在web.xml中配置一个dispatchservlet ...
- SpringMVC 中xml 配置多数据源
1,配置jdbc.properties jdbc.driver_one=... jdbc.url_one=..... jdbc.username_one=... jdbc.password_one=. ...
- springMVC中Dispatcher中的/和/*的区别
1. 首先 / 这个是表示默认的路径,及表示:当没有找到可以匹配的URL就用这个URL去匹配.2. 在springmvc中可以配置多个DispatcherServlet,比如: 配置多个Dispatc ...
- JavaEE开发之SpringMVC中的静态资源映射及服务器推送技术
在上篇博客中,我们聊了<JavaEE开发之SpringMVC中的自定义拦截器及异常处理>.本篇博客我们继续的来聊SpringMVC的东西,下方我们将会聊到js.css这些静态文件的加载配置 ...
- springmvc两种配置方法
基于配置文件xml方式, 配置springmvc步骤: 1.在pom文件中引入jar包: <!--导入springmvc的jar包--> <dependency> <gr ...
- 在springmvc中配置jedis:
主要学习https://github.com/thinkgem/jeesite.一下代码均参考于此并稍作修改. 1.jedis 首先,需要添加jedis: <!--jedis--> < ...
- 第五节 关于SpringMVC中Ajax的配置和应用[下午]
成熟,不是学会表达,而是学会咽下,当你一点一点学会克制住很多东西,才能驾驭好人生. 还有一周,祥云19就算结算了,一个半月的相处希望,胖先生算一个合格的老师 小白,小蔡,2婷婷,小猴,小恒,小崔,小龙 ...
- SpringMVC中采用简洁的配置实现文件上传
文件上传我们一般会有两种策略,一种是通过IO流上传,还有一种是通过表单上传,其实这两种在客户端实现起来都是很简单的,在服务端处理会略有差别,个人感觉IO上传代码简单,但是也有很多硬伤,还是表单上传更合 ...
随机推荐
- X5 Blink下文字自动变大
在X5 Blink中,页面排版时会主动对字体进行放大,会检测页面中的主字体,当某一块的字体在我们的判定规则中,认为字体的字号较小,并且是页面中的主要字体,就会采用主动放大的操作.这显然不是我们想要的. ...
- JavaScript数据结构与算法(三) 优先级队列的实现
TypeScript方式实现源码 // Queue类和PriorityQueue类实现上的区别是,要向PriorityQueue添加元素,需要创建一个特殊的元素.这个元素包含了要添加到队列的元素(它可 ...
- thinphp验证码的简单实现
index.html <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- [NOI 2010]超级钢琴
Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙 的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙 ...
- [HNOI 2015]菜肴制作
Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予 1到N的顺序编号,预估质量最高的菜肴编号 ...
- BZOJ4424: Cf19E Fairy
树上差分的代码很简洁,dfs+差分即可 这题很多坑点啊,比如重边自环好坑 #include<cstdio> #include<cstdlib> #include<algo ...
- hdu 5119(2014北京)
题意: 随机选择一个数,如果后面有比他小的就进行交换,直到没有为止(算一轮).求多少轮后为递增序列 思路: 倒着找,如果有比经过的最小数大的,ans+1 #include <iostream&g ...
- 51Nod 1326 遥远的旅途
题目描述: 一个国家有N个城市,这些城市被标为0,1,2,...N-1.这些城市间连有M条道路,每条道路连接两个不同的城市,且道路都是双向的.一个小鹿喜欢在城市间沿着道路自由的穿梭,初始时小鹿在城市0 ...
- C语言程序设计第五次作业——循环结构1
(一)改错题 输出华氏摄氏温度转换表:输入两个整数lower和upper,输出一张华氏摄氏温度转换表,华氏温度的取值范围是{lower,upper},每次增加2℉.计算公式如下: c = 5×(f-3 ...
- Automap sqlalchemy.ext.automap 自动映射数据库表结构
from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import Session from sqlalchemy i ...