velocity模板在spring中的简单配置
来源:互联网 发布:淘宝真皮女装退货率 编辑:程序博客网 时间:2024/05/21 10:35
1.首先需要配置的是velocity引擎自己,可以通过以下方式在spring配置文件中声明一个VelocityConfigurer Bean:
spring-webmvc-3.2.0.M1.jar
VelocityConfigurer负责在spring中设置Velocity引擎。这里,通过属性resourceLoaderPath告诉Velocity到哪里寻找它的模板。建议将模板放到WEB-INF下的某个子目录下,可以保证这些模板不能被直接访问。
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath"> <value>WEB-INF/Template/</value> </property> </bean>
在配置属性时有两种方式,一是通过下面这种属性的方式:
<property name="velocityProperties"> <props> <prop key=""> </prop> <prop key=""> </prop> </props></property>
二是通过配置文件的方式(velocity.properties)
<property name="configLocation"> <value>/WET-INF/config/velocity.properties</value></property>
2.解析velocity视图
配置视图解析器
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.velocity.VelocityView</value> </property> <property name="suffix"> <value>.vm</value> </property> </bean>
VelocityViewResolver和Velocity的关系与InternalResourceViewResolver和JSP的关系相似。InternalResourceViewResolver使用prefix属性和suffix属性由视图的逻辑名构造出模板文件路径,这样在Controller中的ModelAndView中直接通过文件名找模板。如下所示:
< bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.InternalResourceView</value> </property> < property name="prefix"> <value>/WEB-INF/JSP/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>
在这里我们仅仅设置了suffix属性为.vm扩展名。由于模版文件的路径已经通过VelocityConfigurer的resourceLoaderPath属性配置好了。
3.渲染页面
例如map.put("hello","helloWorld");
返回信息为return new ModelAndView("index",map);
视图的逻辑名为index。当解析这个视图时,index加上后缀.vm构成一个模板名index.vm。VelocityViewResolver会在WEB-INF/vilocity路径下寻找这个模板。
然后再index.vm下通过如下就可以显示数据:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><body><h1>${hello}</h1></body></html>
- velocity模板在spring中的简单配置
- velocity模板在spring中的简单配置
- Velocity 在Spring中的配置
- Velocity初探小结--Velocity在spring中的配置和使用
- Spring中配置Velocity模板
- spring mvc 配置 velocity 模板
- Velocity 模板引擎在复杂邮件业务中的简单举例
- Spring boot配置freemarker,jsp,velocity模板
- 【笔记】Velocity模板 和 Spring的整合配置 解决页面乱码的最佳方案, 简单Demo了解Velocity
- Quartz在Spring中的简单配置
- Velocity 语法及其在springMVC中的配置
- Spring+Mybatis+Velocity配置
- Spring MVC配置Velocity
- Spring中使用Velocity模板
- spring 邮件(Velocity模板)
- spring mvc 集成velocity模板
- Spring MVC Velocity模板引擎
- spring boot 使用velocity模板
- java 代码链接SFTP,上传下载
- html与css的配合
- codefroces#337 A Pasha and Stick【思维】
- android:descendantFocusability用法简析
- Flash设置网页最底层,兼容IE6-IE8、FF、Chrome、Opera
- velocity模板在spring中的简单配置
- C#调用存储过程的几个方法
- JAVA中堆栈和内存分配详解
- LINUX使用常用命令
- HTML5上传图片预览(不需通过服务器)
- 二叉查找树的层序遍历
- POJ 1182 食物链(带权并查集)
- C++类体系中,不能被派生类继承的
- HttpWebRequest介绍