Spring MVC 整合Velocity及用法说明
2015-10-23 17:20
573 查看
Velocity是一个基于java的模板引擎(template engine),它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。
配置:
1.在pom.xml增加依赖的velocity包
[html]
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
2.在servlet-context.xml中增加以下内容,如果有jsp的配置先注释掉
[html]
<beans:bean id="velocityConfig"
<span style="white-space:pre"> </span>class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<beans:property name="resourceLoaderPath" value="/WEB-INF/views" />
<beans:property name="configLocation" value="classpath:common/velocity.properties" />
</beans:bean>
<beans:bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<beans:property name="suffix" value=".htm" />
</beans:bean>
3.在resources/common目录下创建velocity.properties
[html]
#encoding
input.encoding =UTF-8
output.encoding=UTF-8
contentType=text/html;charset=UTF-8
#autoreload when vm changed
file.resource.loader.cache=false
file.resource.loader.modificationCheckInterval =1
velocimacro.library.autoreload=false
4.新建testController
[java]
@RequestMapping(value="/test")
@Controller
public class TestController {
@RequestMapping(value="/index")
public String index(Model model) {
String name = "tester";
model.addAttribute("name", name);
return "test/index";
}
}
5.新建test/index.htm模板
<html>
<head>
</head>
<body>
hello $name!
</body>
</html>
6.访问http://localhost/test/index
显示 hello tester!
---------------------------------------------------------------------------------------------------------------------------------------------------------------
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
2、"$"用来标识一个对象(或理解为变量);如
如:$i、$msg、$TagUtil.options(...)等。
3、"{}"用来明确标识Velocity变量;
比如在页面中,页面中有一个$someonename,此时,Velocity将把someonename作为变量名,若我们程序是想在someone这 个变量的后面紧接着显示name字符,则上面的标签应该改成${someone}name。
4、"!"用来强制把不存在的变量显示为空白。
如当页面中包含$msg,如果msg对象有值,将显示msg的值,如果不存在msg对象同,则在页面中将显示$msg字符。这是我们不希望的,为了把不存 在的变量或变量值为null的对象显示为空白,则只需要在变量名前加一个“!”号即可。
如:$!msg
5、循#foreach( $info in $list) $info.someList #end,环读取集合list中的对象
#foreach( $info in $hotL包含文件#inclue("模板文件名")或#parse("模板文件名")st1)
<a href="/blog/list?&cid=$!info.cid" target="_blank">$!info.title</a><br>
#end
上面的脚本表示循环遍历hotList1集合中的对象,并输出对象的相关内容。
6、包含文件#inclue("模板文件名")或#parse("模板文件名")
主要用于处理具有相同内容的页面,比如每个网站的顶部或尾部内容。
使用方法,可以参考EasyJF开源Blog及EasyJF开源论坛中的应用!
如:#parse("/blog/top.html")或#include("/blog/top.html")
parse与include的区别在于,若包含的文件中有Velocity脚本标签,将会进一步解析,而include将原样显示。
配置:
1.在pom.xml增加依赖的velocity包
[html]
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
2.在servlet-context.xml中增加以下内容,如果有jsp的配置先注释掉
[html]
<beans:bean id="velocityConfig"
<span style="white-space:pre"> </span>class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<beans:property name="resourceLoaderPath" value="/WEB-INF/views" />
<beans:property name="configLocation" value="classpath:common/velocity.properties" />
</beans:bean>
<beans:bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<beans:property name="suffix" value=".htm" />
</beans:bean>
3.在resources/common目录下创建velocity.properties
[html]
#encoding
input.encoding =UTF-8
output.encoding=UTF-8
contentType=text/html;charset=UTF-8
#autoreload when vm changed
file.resource.loader.cache=false
file.resource.loader.modificationCheckInterval =1
velocimacro.library.autoreload=false
4.新建testController
[java]
@RequestMapping(value="/test")
@Controller
public class TestController {
@RequestMapping(value="/index")
public String index(Model model) {
String name = "tester";
model.addAttribute("name", name);
return "test/index";
}
}
5.新建test/index.htm模板
<html>
<head>
</head>
<body>
hello $name!
</body>
</html>
6.访问http://localhost/test/index
显示 hello tester!
---------------------------------------------------------------------------------------------------------------------------------------------------------------
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
2、"$"用来标识一个对象(或理解为变量);如
如:$i、$msg、$TagUtil.options(...)等。
3、"{}"用来明确标识Velocity变量;
比如在页面中,页面中有一个$someonename,此时,Velocity将把someonename作为变量名,若我们程序是想在someone这 个变量的后面紧接着显示name字符,则上面的标签应该改成${someone}name。
4、"!"用来强制把不存在的变量显示为空白。
如当页面中包含$msg,如果msg对象有值,将显示msg的值,如果不存在msg对象同,则在页面中将显示$msg字符。这是我们不希望的,为了把不存 在的变量或变量值为null的对象显示为空白,则只需要在变量名前加一个“!”号即可。
如:$!msg
5、循#foreach( $info in $list) $info.someList #end,环读取集合list中的对象
#foreach( $info in $hotL包含文件#inclue("模板文件名")或#parse("模板文件名")st1)
<a href="/blog/list?&cid=$!info.cid" target="_blank">$!info.title</a><br>
#end
上面的脚本表示循环遍历hotList1集合中的对象,并输出对象的相关内容。
6、包含文件#inclue("模板文件名")或#parse("模板文件名")
主要用于处理具有相同内容的页面,比如每个网站的顶部或尾部内容。
使用方法,可以参考EasyJF开源Blog及EasyJF开源论坛中的应用!
如:#parse("/blog/top.html")或#include("/blog/top.html")
parse与include的区别在于,若包含的文件中有Velocity脚本标签,将会进一步解析,而include将原样显示。
相关文章推荐
- Java引用类型变量
- 我对Java Serializable(序列化)的理解和总结
- JAVA 集合的详解(转)
- Spring MVC 集成 Velocity
- Java中的Random()函数
- 在Eclipse中配置Hadoop2.6.0
- java程序——凯撒加密
- 添加Jamon到项目中
- Java中HashMap和TreeMap的区别深入理解
- Java Web基础——Action+Service +Dao三层的功能划分
- Java 版 (精华区)--Frame和Panel的区别
- eclipse下打包实践
- java HashMap的简单实现
- Java_Ant详解
- Java多线程(二)synchronized wait notify
- Struts框架学习01
- power designer 提示Could not Initialize JavaVM! 的解决方法
- java中获取当前时间的字符串以及设置时间的格式
- 【JAVA】【SSH】【Initialization of bean failed】【AopConfigException】【BeanCreationException】
- Eclipse 查看源码