在前面一篇文章中我们介绍了关于Velocity模板引擎的相关内容,下面我们介绍下如何在Spring mvc中使用Velocity。
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Velocity是一种Java模板引擎。
和JSP,Freemarker差不多,都是用来展示网页内容的。
和JSP不同的是velocity只能显示Action中的数据,不能处理数据。不能写java代码,但是可以使用Velocity标记。
Velocity的页面(模版)可是是任何类型(text/html)的文件。
当Velocity应用于web开发时,Velocity将java代码从web页面中分离出来,界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点,
也就是说,页面设计人员可以只关注页面的显示效果,而由java程序开发人员关注业务逻辑编码。
下面简要描述一下在spring mvc中配置velocity的步骤:
1、引入velocity所需要的包
2、添加配置信息
3、测试
步骤详解:
1、引入velocity包:velocity-1.7.jar、velocity-tools-2.0.jar、spring-context-support-4.2.5.RELEASE.jar(无视版本号)
pom文件配置的话,引入下面jar包即可
- <!--velocity start-->
- <dependency>
- <groupId>org.apache.velocity</groupId>
- <artifactId>velocity</artifactId>
- <version>1.6</version>
- </dependency>
- <dependency>
- <groupId>org.apache.velocity</groupId>
- <artifactId>velocity-tools</artifactId>
- <version>2.0</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>4.2.5.RELEASE</version>
- </dependency>
- <!--velocity end-->
2、在配置文件里添加配置信息
VelocityConfigurer负责在spring中设置Velocity引擎。这里,通过属性resourceLoaderPath告诉Velocity到哪里寻找它的模板。建议将模板放到WEB-INF下的某个子目录下,可以保证这些模板不能被直接访问。
在配置属性时,有两种配置方法:
(1)通过配置文件方式
- <!--模板信息配置-->
- <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
- <property name="resourceLoaderPath" value="/WEB-INF/velocity/" /><!-- 模板存放的路径 -->
- <property name="configLocation" value="classpath:velocity.properties"/>
- </bean>
velocity.properties:
- input.encoding=UTF-8
- output.encoding=UTF-8
- directive.foreach.counter.name=loopCounter
- directive.foreach.counter.initial.value=0
(2)通过属性的方式:
- <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
- <property name="resourceLoaderPath" value="WEB-INF/velocity/" /><!-- 設置模板防止位置-->
- <property name="velocityProperties">
- <props>
- <prop key="directive.foreach.counter.name">loopCounter</prop>
- <prop key="directive.foreach.counter.initial.value">0</prop>
- <prop key="input.encoding">UTF-8</prop><!-- 指定模板引擎进行模板处理的编码 -->
- <prop key="output.encoding">UTF-8</prop><!-- 指定输出流的编码 -->
- </props>
- </property>
- </bean>
我第一次配置时,用的第一种方式,出现一个问题,就是走完后台后,页面不进行跳转,一直是layout.vm。
后来改为第二种方式,问题解决,再改为第一种配置,问题再现不了。如果有朋友碰到类似问题,可以试试另一种方式配置。
3、配置velocity解析视图
- <!-- 配置视图的显示 -->
- <bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
- <property name="cache" value="true" />
- <property name="prefix" value="/" /><!-- 视图文件的前缀,即存放的路径 -->
- <property name="suffix" value=".vm" /><!-- 视图文件的后缀名 -->
- <!--<property name="toolboxConfigLocation" value="/WEB-INF/tools.xml" /><!–toolbox配置文件路径–>-->
- <property name="dateToolAttribute" value="date" /><!--日期函数名称-->
- <property name="numberToolAttribute" value="number" /><!--数字函数名称-->
- <property name="contentType" value="text/html;charset=UTF-8" />
- <property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持-->
- <property name="exposeRequestAttributes" value="true" /><!--是否开放request属性-->
- <property name="requestContextAttribute" value="rc"/><!--request属性引用名称-->
- </bean>
VelocityViewResolver和Velocity的关系与InternalResourceViewResolver和JSP的关系相似。
InternalResourceViewResolver使用prefix属性和suffix属性由视图的逻辑名构造出模板文件路径,这样在Controller中的ModelAndView中直接通过文件名找模板。
4、测试页面
Action方法:
- @RequestMapping(value="hello")
- public ModelAndView printWelcome(HttpServletRequest request,HttpServletResponse response) {
- ModelAndView mav= new ModelAndView();
- mav.addObject("city","test");
- mav.setViewName("hello");
- return mav;
- }
5、在/WEB-INF/velocity/ 路径下信件hello.vm
- <html>
- <body>
- <h1>${city}</h1>
- </body>
- </html>
6、启动项目,访问http://localhost:8080/hello.action