Spring MVC 集成Velocity
来源:互联网 发布:中国的未来在哪里 知乎 编辑:程序博客网 时间:2024/05/17 02:18
环境说明
Maven + Idea-2017.2.6
具体配置
1. 此处不再介绍Sping MVC的搭建,直接配置Velocity.
2. 需要在pom文件中加入Velocity的依赖,如下:
3. 其中Spring的版本为(Spring版本不同,可能对velocity的支持上有所差别,这里版本是匹配过的)
<spring.version>4.2.2.RELEASE</spring.version>
4. 在<servletName>-servlet.xml中配置试图解析器为Velocity:
注意上图中的配置,在velocityConfigurer中需要指定configLocation,其中velocity.properties的配置如下图,必须指定输入输出的编码为UTF-8,不然输出到页面的中文内容为乱码:
其次在viewResolver的配置中,同时指定ContentType,且charset=UTF-8,不然中文依旧为乱码。
在viewResolver的配置中还指定了属性toolboxConfigLocation的路径配置(此处经测试不能以classpath*:toolbox.xml的方式配置,不会生效,原因未知),该属性主要用于页面可直接调用服务端提供的一些封装好的方法(Spring默认提供了大量的封装类,此处不做介绍,可自行上网查阅)。
下面看下toolbox的具体配置,简单如下:
其中key为页面调用时使用,class为类的全路径。
DateUtil的编写和其他类没什么区别,供外部调用的方法声明为public即可。
测试
以下为测试的Controller中的部分实现
页面的vm文件大致为:
其中#foreach为velocity的基本用法,不做介绍,$!today为服务端的传值,$!DateUtil即为前面在toolbox中配置的可调用服务端的key值,format方法为服务端DateUtil类中的方法。
5. 效果如下:
- Spring MVC 集成 Velocity
- Spring MVC 集成 Velocity
- Spring MVC 集成Velocity
- spring mvc 集成velocity模板
- spring mvc 集成velocity模板
- velocity集成到spring mvc中
- spring集成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+maven
- Spring MVC 与 Velocity 整合
- CentOS7上MySQL返回 Too many connections
- Spring Boot ,Spring Security的Filter在Tomcat正常,部署在Weblogic 12c Filter顺序出错,导致拦截权限问题
- 项目
- 一些C++的小程序(一)
- tomcat域名配置
- Spring MVC 集成Velocity
- [StudyNotes] 左偏树
- python2中一些模块到python3中名称的变化
- maven 打包可执行jar
- 使用window.location.hash解决ajax刷新和导航问题
- AutoMapper官方文档(三)【约定】
- 课后作业之邮箱类
- MySQL学习笔记
- 如果其他类型更适合,则尽量避免使用字符串