java学习笔记5 - Spring MVC 整合Velocity及用法说明
来源:互联网 发布:博客数据库设计 编辑:程序博客网 时间:2024/05/29 13:04
Velocity是一个基于java的模板引擎(template engine),它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。
配置:
1.在pom.xml增加依赖的velocity包
<dependency><groupId>velocity</groupId><artifactId>velocity</artifactId><version>1.5</version></dependency>
2.在servlet-context.xml中增加以下内容,如果有jsp的配置先注释掉
<beans:bean id="velocityConfig"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
#encodinginput.encoding =UTF-8output.encoding=UTF-8contentType=text/html;charset=UTF-8#autoreload when vm changedfile.resource.loader.cache=falsefile.resource.loader.modificationCheckInterval =1velocimacro.library.autoreload=false
4.新建testController
@RequestMapping(value="/test")@Controllerpublic 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学习笔记5 - Spring MVC 整合Velocity及用法说明
- Spring MVC 整合Velocity及用法说明
- Spring MVC 整合Velocity及用法说明
- Spring MVC 整合Velocity
- spring mvc 整合 velocity
- spring mvc整合velocity
- Spring MVC整合Velocity
- Spring MVC整合Velocity
- Spring MVC整合Velocity
- Spring MVC 与 Velocity 整合
- spring mvc+velocity整合开发
- 2、Spring MVC整合Velocity
- Spring MVC整合Velocity详解
- 3、spring mvc sitemesh velocity整合
- spring mvc整合velocity源码分析
- Maven 工程整合 Spring mvc + Mybatis +Velocity
- 续spring mvc+velocity之spring mvc + freemarker整合开发
- Spring MVC多视图整合(jsp、velocity、freemarker)
- 关于android的单位dp,dip
- Linux 的cp命令
- BeanPostProcessor接口
- [Android]WebOfCam,远程访问手机的摄像头,破解无广告
- UIButton上image和title的位置调整
- java学习笔记5 - Spring MVC 整合Velocity及用法说明
- Oracle GoldenGate for Oracle 11g(Solaris 10 x86-64) to Oracle 10g(CentOS 4.8) configuration
- Linux mv命令
- 一分钟掌握Spring中bean的生命周期
- extjs class
- C++ 学习书籍
- Error: unterminated string literal
- 提高显示布局文件的性能 1 - 优化布局层级
- Hadoop MR 实现视频拉流框架