Spring MVC中配置Velocity


在前面一篇文章中我们介绍了关于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包即可

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!--velocity start-->  
  2. <dependency>  
  3.     <groupId>org.apache.velocity</groupId>  
  4.     <artifactId>velocity</artifactId>  
  5.     <version>1.6</version>  
  6. </dependency>  
  7. <dependency>  
  8.     <groupId>org.apache.velocity</groupId>  
  9.     <artifactId>velocity-tools</artifactId>  
  10.     <version>2.0</version>  
  11. </dependency>  
  12. <dependency>  
  13.     <groupId>org.springframework</groupId>  
  14.     <artifactId>spring-context-support</artifactId>  
  15.     <version>4.2.5.RELEASE</version>  
  16. </dependency>  
  17.   
  18. <!--velocity end-->  

2、在配置文件里添加配置信息

VelocityConfigurer负责在spring中设置Velocity引擎。这里,通过属性resourceLoaderPath告诉Velocity到哪里寻找它的模板。建议将模板放到WEB-INF下的某个子目录下,可以保证这些模板不能被直接访问。

在配置属性时,有两种配置方法:

(1)通过配置文件方式

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!--模板信息配置-->  
  2.     <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">  
  3.         <property name="resourceLoaderPath" value="/WEB-INF/velocity/" /><!-- 模板存放的路径 -->  
  4.         <property name="configLocation" value="classpath:velocity.properties"/>  
  5.     </bean>  


velocity.properties:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. input.encoding=UTF-8  
  2. output.encoding=UTF-8  
  3. directive.foreach.counter.name=loopCounter  
  4. directive.foreach.counter.initial.value=0  


(2)通过属性的方式:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">    
  2.     <property name="resourceLoaderPath"  value="WEB-INF/velocity/" /><!-- 設置模板防止位置-->    
  3.     <property name="velocityProperties">    
  4.         <props>    
  5.             <prop key="directive.foreach.counter.name">loopCounter</prop>    
  6.             <prop key="directive.foreach.counter.initial.value">0</prop>    
  7.             <prop key="input.encoding">UTF-8</prop><!-- 指定模板引擎进行模板处理的编码 -->    
  8.             <prop key="output.encoding">UTF-8</prop><!-- 指定输出流的编码 -->    
  9.         </props>    
  10.     </property>    
  11. </bean>    


我第一次配置时,用的第一种方式,出现一个问题,就是走完后台后,页面不进行跳转,一直是layout.vm。

后来改为第二种方式,问题解决,再改为第一种配置,问题再现不了。如果有朋友碰到类似问题,可以试试另一种方式配置。

 

3、配置velocity解析视图

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <!-- 配置视图的显示 -->  
  2.     <bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">  
  3.         <property name="cache" value="true" />  
  4.         <property name="prefix" value="/" /><!-- 视图文件的前缀,即存放的路径 -->  
  5.         <property name="suffix" value=".vm" /><!-- 视图文件的后缀名 -->  
  6.         <!--<property name="toolboxConfigLocation" value="/WEB-INF/tools.xml" /><!–toolbox配置文件路径–>-->  
  7.         <property name="dateToolAttribute" value="date" /><!--日期函数名称-->  
  8.         <property name="numberToolAttribute" value="number" /><!--数字函数名称-->  
  9.         <property name="contentType" value="text/html;charset=UTF-8" />  
  10.         <property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持-->  
  11.         <property name="exposeRequestAttributes" value="true" /><!--是否开放request属性-->  
  12.         <property name="requestContextAttribute" value="rc"/><!--request属性引用名称-->  
  13.     </bean>  

VelocityViewResolver和Velocity的关系与InternalResourceViewResolver和JSP的关系相似。

InternalResourceViewResolver使用prefix属性和suffix属性由视图的逻辑名构造出模板文件路径,这样在Controller中的ModelAndView中直接通过文件名找模板。


4、测试页面

Action方法:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. @RequestMapping(value="hello")  
  2. public ModelAndView printWelcome(HttpServletRequest request,HttpServletResponse response) {  
  3.     ModelAndView mav= new ModelAndView();  
  4.     mav.addObject("city","test");  
  5.     mav.setViewName("hello");  
  6.     return mav;  
  7. }  

5、在/WEB-INF/velocity/ 路径下信件hello.vm

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <html>  
  2. <body>  
  3.     <h1>${city}</h1>  
  4. </body>  
  5. </html>  


6、启动项目,访问http://localhost:8080/hello.action



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值