Spring MVC 整合Velocity及用法说明
来源:互联网 发布:充电风扇淘宝网 编辑:程序博客网 时间:2024/05/17 06:43
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"
- <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
- #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
- @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将原样显示。
- Spring MVC 整合Velocity及用法说明
- Spring MVC 整合Velocity及用法说明
- 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整合开发
- 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)
- HashMap,LinkedHashMap,TreeMap对比
- Vert.x核心模块 HTTP客户端推送 (九【4】)
- js post传递中文参数到java后台出现乱码
- Java中ArrayList类的用法
- 深度学习python图像标记工具labelTool
- Spring MVC 整合Velocity及用法说明
- 数据库
- 浏览器兼容播放wav格式
- 【数据结构与算法】万恶的最长回文子串
- 名词解释
- 来自公众号的SPRING(4)
- 封装
- 密码存到数据库时加密
- iOS xcode ‘XXXX’ was compiled with optimization - stepping may behave oddly; variables may not be av