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"?>
<beans xmlns="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-scan base-package="com.tyyd.dw.controller"/>
<mvc:annotation-driven/>
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF"/>
<property name="configLocation" value="classpath:velocity.properties"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix" value=".vm"/>
<property name="prefix" value="/"/>
<!-- 使用springMacro的对象 -->
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="requestContextAttribute" value="content"/>
<property name="exposeRequestAttributes" value="true"/>
<property name="exposeSessionAttributes" value="true"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
<!-- spring的日期格式化 -->
<property name="dateToolAttribute" value="dateTool"/>
<!-- velocity toolbox -->
<property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/>
</bean>
</beans>
当然也支持layout,只需要把上面的velocity的解析器改为:
org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver
并增加属性指定layout文件:
<property name="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>
<parameter name="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
public class LoginController {
@RequestMapping(value = "login", method = RequestMethod.GET)
public String login() {
return "login";
}
}
定义了一个login的Controller,当访问{项目路径}/login.shtml时,将进入login()方法,该方法返回字符串“login”将自动在WEB-INF目录下找寻login.vm页面文件,找到后将跳转到该页面。