velocity模板在spring中的简单配置
来源:互联网 发布:c语言中单引号的作用 编辑:程序博客网 时间:2024/06/04 17:44
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模板
- 微信开发 注意 js接口安全域名 invail url domain
- SQL优化案例
- LeetCode-Basic Calculator
- 132.按规定排列字符
- 兔子--自学ps的整个过程
- velocity模板在spring中的简单配置
- gulp 使用实例解析
- Android getWidth和getMeasuredWidth
- Dubbo亮点总结
- 新版Eclipse找不到Java EE Module Dependencies选项
- springMVC+velocity配置
- Oracle常用sql总结
- 链表的java实现以及基本的增加,删除,排序操作
- STC89c51单片机的定时器2