Spring mvc和velocity都是当前流行的框架,在我当前的项目中就用到了这两个框架,在搭建的过程中,虽然对于配置的步骤及使用的过程都比较清楚,但是要没有任何参照一下写出所有的配置记住所有的配置类名等,还是不大可能做到的,本文将讲解spring mvc和velocity的整合步骤,顺便做一下备忘。
web.xml配置,这里只讲mvc部分,不包含spring本身的配置:
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mvc.xml</param-value>
- <description>加载Spring MVC的配置文件</description>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>*.shtml</url-pattern>
- </servlet-mapping>
这样,所有的*.shtml的请求,都会被springmvc这个servlet处理。这里如果没有指定contextConfigLocation这个参数,将会按照默认规则在classpath下寻找名称为{servlet-name}-servlet.xml的配置文件。
在spring-mvc.xml文件中对spring mvc和velocity进行配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- 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">
- <context:component-scanbase-package="com.tyyd.dw.controller"/>
- <mvc:annotation-driven/>
- <beanid="velocityConfig"class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
- <propertyname="resourceLoaderPath"value="/WEB-INF"/>
- <propertyname="configLocation"value="classpath:velocity.properties"/>
- </bean>
- <beanid="viewResolver"class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
- <propertyname="suffix"value=".vm"/>
- <propertyname="prefix"value="/"/>
- <!-- 使用springMacro的对象 -->
- <propertyname="exposeSpringMacroHelpers"value="true"/>
- <propertyname="requestContextAttribute"value="content"/>
- <propertyname="exposeRequestAttributes"value="true"/>
- <propertyname="exposeSessionAttributes"value="true"/>
- <propertyname="contentType"value="text/html;charset=UTF-8"/>
- <!-- spring的日期格式化 -->
- <propertyname="dateToolAttribute"value="dateTool"/>
- <!-- velocity toolbox -->
- <propertyname="toolboxConfigLocation"value="/WEB-INF/toolbox.xml"/>
- </bean>
- </beans>
当然也支持layout,只需要把上面的velocity的解析器改为:
- org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver
并增加属性指定layout文件:
- <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的属性:
- #velocimacro.library = /WEB-INF/templates/common/page.vm,/WEB-INF/templates/common/global_library.vm
- tools.view.servlet.layout.directory =/WEB-INF/templates/layout/
- tools.view.servlet.layout.default.template=default.vm
- default.contentType=text/html;charset=utf-8
- input.encoding = UTF-8
- output.encoding = UTF-8
- class.resource.loader.cache=false
- velocimacro.library.autoreload=true
- directive.set.null.allowed =true
- runtime.log.error.stacktrace =true
- runtime.log.warn.stacktrace =true
- runtime.log.info.stacktrace =true
- runtime.log.logsystem.class= org.apache.velocity.runtime.log.SimpleLog4JLogSystem
- runtime.log.logsystem.log4j.category = velocity_log
这些属性也可以通过org.springframework.web.servlet.view.velocity.VelocityConfigurer类在spring的配置文件中进行配置,不推荐用这种方式,这里就不再列出。
在WEB-INF下建立toolbox文件,按需添加工具类对象:
- <?xml version="1.0"?>
- <toolbox>
- <tool>
- <key>date</key>
- <scope>request</scope>
- <class>
- org.apache.velocity.tools.generic.DateTool
- </class>
- <parametername="format"value="yyyy-MM-dd HH:mm:ss"/>
- </tool>
- <tool>
- <key>link</key>
- <scope>request</scope>
- <class>org.apache.velocity.tools.view.tools.LinkTool</class>
- </tool>
- <tool>
- <key>stringUtils</key>
- <scope>request</scope>
- <class>org.apache.velocity.util.StringUtils</class>
- </tool>
- <tool>
- <key>math</key>
- <scope>application</scope>
- <class>org.apache.velocity.tools.generic.MathTool</class>
- </tool>
- <tool>
- <key>esc</key>
- <scope>request</scope>
- <class>org.apache.velocity.tools.generic.EscapeTool</class>
- </tool>
- <tool>
- <key>params</key>
- <scope>request</scope>
- <class>org.apache.velocity.tools.view.tools.ParameterParser</class>
- </tool>
- </toolbox>
至些,整合算是完成了。没错,就是这么的简单,接下来你就可以创建一个Controller来测试使用了。
示例Controller:
- @Controller
- publicclassLoginController{
- @RequestMapping(value ="login", method =RequestMethod.GET)
- publicString login(){
- return"login";
- }
- }
定义了一个login的Controller,当访问{项目路径}/login.shtml时,将进入login()方法,该方法返回字符串“login”将自动在WEB-INF目录下找寻login.vm页面文件,找到后将跳转到该页面。