spring mvc整合velocity

Spring mvc和velocity都是当前流行的框架,在我当前的项目中就用到了这两个框架,在搭建的过程中,虽然对于配置的步骤及使用的过程都比较清楚,但是要没有任何参照一下写出所有的配置记住所有的配置类名等,还是不大可能做到的,本文将讲解spring mvc和velocity的整合步骤,顺便做一下备忘。

web.xml配置,这里只讲mvc部分,不包含spring本身的配置:

  1. <servlet>
  2. <servlet-name>springmvc</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <init-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:spring-mvc.xml</param-value>
  7. <description>加载Spring MVC的配置文件</description>
  8. </init-param>
  9. <load-on-startup>1</load-on-startup>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>springmvc</servlet-name>
  13. <url-pattern>*.shtml</url-pattern>
  14. </servlet-mapping>

这样,所有的*.shtml的请求,都会被springmvc这个servlet处理。这里如果没有指定contextConfigLocation这个参数,将会按照默认规则在classpath下寻找名称为{servlet-name}-servlet.xml的配置文件。

在spring-mvc.xml文件中对spring mvc和velocity进行配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  7.  
  8. <context:component-scanbase-package="com.tyyd.dw.controller"/>
  9. <mvc:annotation-driven/>
  10.  
  11. <beanid="velocityConfig"class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
  12. <propertyname="resourceLoaderPath"value="/WEB-INF"/>
  13. <propertyname="configLocation"value="classpath:velocity.properties"/>
  14. </bean>
  15.  
  16. <beanid="viewResolver"class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
  17. <propertyname="suffix"value=".vm"/>
  18. <propertyname="prefix"value="/"/>
  19. <!-- 使用springMacro的对象 -->
  20. <propertyname="exposeSpringMacroHelpers"value="true"/>
  21. <propertyname="requestContextAttribute"value="content"/>
  22. <propertyname="exposeRequestAttributes"value="true"/>
  23. <propertyname="exposeSessionAttributes"value="true"/>
  24. <propertyname="contentType"value="text/html;charset=UTF-8"/>
  25. <!-- spring的日期格式化 -->
  26. <propertyname="dateToolAttribute"value="dateTool"/>
  27. <!-- velocity toolbox -->
  28. <propertyname="toolboxConfigLocation"value="/WEB-INF/toolbox.xml"/>
  29. </bean>
  30. </beans>

 

当然也支持layout,只需要把上面的velocity的解析器改为:

  1. org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver

 

并增加属性指定layout文件:

  1. <propertyname="layoutUrl"value="layout.vm"/>

 

这里要注意layout用到的vm文件需要放到classpath路径而不是webroot,不然会找不到文件。

在视图解析器的定义中,

"exposeSpringMacroHelpers"设置是否通过Spring的宏库暴露一个RequestContext(名为springBindRequestContext)供外部使用,默认值为false。它暴露了处理表单和验证错误信息的宏操作;

"requestContextAttribute"把Spring的RequestContext对象暴露为变量content。利用${content.contextPath}来获取应用程序的contextPath;利用${content.getMessage("user.name")}读取/WEB-INF/classes/messages.properties本地化信息。此对象可以为那些不访问serlvet请求的View技术(类似Velocity和FreeMarker模板)提供不少的方便。 

exposeRequestAttributes:默认值false,设置是否所有的request属性在与模板进行合并之前添加到model中。(request范围内包含的所有对象,而不是一个Request对象。) 

exposeSessionAttributes:默认值false,设置是否所有的session属性在与模板进行合并之前添加到model中。(理解同上) 

在velocity.properties中配置velocity的属性:

  1. #velocimacro.library = /WEB-INF/templates/common/page.vm,/WEB-INF/templates/common/global_library.vm
  2. tools.view.servlet.layout.directory =/WEB-INF/templates/layout/
  3. tools.view.servlet.layout.default.template=default.vm
  4. default.contentType=text/html;charset=utf-8
  5. input.encoding = UTF-8
  6. output.encoding = UTF-8
  7. class.resource.loader.cache=false
  8. velocimacro.library.autoreload=true
  9. directive.set.null.allowed =true
  10. runtime.log.error.stacktrace =true
  11. runtime.log.warn.stacktrace =true
  12. runtime.log.info.stacktrace =true
  13. runtime.log.logsystem.class= org.apache.velocity.runtime.log.SimpleLog4JLogSystem
  14. runtime.log.logsystem.log4j.category = velocity_log

这些属性也可以通过org.springframework.web.servlet.view.velocity.VelocityConfigurer类在spring的配置文件中进行配置,不推荐用这种方式,这里就不再列出。

在WEB-INF下建立toolbox文件,按需添加工具类对象:

  1. <?xml version="1.0"?>
  2. <toolbox>
  3. <tool>
  4. <key>date</key>
  5. <scope>request</scope>
  6. <class>
  7. org.apache.velocity.tools.generic.DateTool
  8. </class>
  9. <parametername="format"value="yyyy-MM-dd HH:mm:ss"/>
  10. </tool>
  11. <tool>
  12. <key>link</key>
  13. <scope>request</scope>
  14. <class>org.apache.velocity.tools.view.tools.LinkTool</class>
  15. </tool>
  16. <tool>
  17. <key>stringUtils</key>
  18. <scope>request</scope>
  19. <class>org.apache.velocity.util.StringUtils</class>
  20. </tool>
  21. <tool>
  22. <key>math</key>
  23. <scope>application</scope>
  24. <class>org.apache.velocity.tools.generic.MathTool</class>
  25. </tool>
  26. <tool>
  27. <key>esc</key>
  28. <scope>request</scope>
  29. <class>org.apache.velocity.tools.generic.EscapeTool</class>
  30. </tool>
  31. <tool>
  32. <key>params</key>
  33. <scope>request</scope>
  34. <class>org.apache.velocity.tools.view.tools.ParameterParser</class>
  35. </tool>
  36. </toolbox>

至些,整合算是完成了。没错,就是这么的简单,接下来你就可以创建一个Controller来测试使用了。

示例Controller:

  1. @Controller
  2. publicclassLoginController{
  3. @RequestMapping(value ="login", method =RequestMethod.GET)
  4. publicString login(){
  5. return"login";
  6. }
  7. }

定义了一个login的Controller,当访问{项目路径}/login.shtml时,将进入login()方法,该方法返回字符串“login”将自动在WEB-INF目录下找寻login.vm页面文件,找到后将跳转到该页面。

猜你喜欢

转载自himo-zhang.iteye.com/blog/2183273