闲话不多说,以下是本人整合springMVC+velocity的一些心得和体会
applicationcontext.xml
- <!-- 配置velocity引擎 -->
- <bean id="velocityConfigurer"
- class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
- <property name="resourceLoaderPath" value="/WEB-INF/templates/" /><!-- 模板存放的路径 -->
- <property name="configLocation" value="classpath:velocity.properties" />
- </bean>
- <!-- 配置视图的显示 -->
- <bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
- <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属性引用名称-->
- <property name="layoutUrl" value="layout/default.vm"/><!--指定layout文件-->
- </bean>
<!-- 配置velocity引擎 -->
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/templates/" /><!-- 模板存放的路径 -->
<property name="configLocation" value="classpath:velocity.properties" />
</bean>
<!-- 配置视图的显示 -->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<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属性引用名称-->
<property name="layoutUrl" value="layout/default.vm"/><!--指定layout文件-->
</bean>
pom.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <dependencies>
- <!-- spring核心包 -->
- <!-- springframe start -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-oxm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- springframe end -->
- <!-- mybatis核心包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${mybatis.version}</version>
- </dependency>
- <!-- mybatis/spring包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.2.2</version>
- </dependency>
- <!-- mysql驱动包 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.29</version>
- </dependency>
- <!-- junit测试包 -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <!-- 日志文件管理包 -->
- <!-- log start -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <!-- log end -->
- <dependency>
- <groupId>net.sourceforge.htmlcleaner</groupId>
- <artifactId>htmlcleaner</artifactId>
- <version>2.8</version>
- </dependency>
- <dependency>
- <groupId>tomcat</groupId>
- <artifactId>servlet-api</artifactId>
- <version>5.5.23</version>
- </dependency>
- <dependency>
- <groupId>org.apache.velocity</groupId>
- <artifactId>velocity</artifactId>
- <version>1.7</version>
- </dependency>
- <dependency>
- <groupId>org.apache.velocity</groupId>
- <artifactId>velocity-tools</artifactId>
- <version>2.0</version>
- </dependency>
- </dependencies>
- <groupId>qb</groupId>
- <artifactId>qb</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>war</packaging>
- <url>http://maven.apache.org</url>
- <properties>
- <!-- spring版本号 -->
- <spring.version>3.2.4.RELEASE</spring.version>
- <!-- mybatis版本号 -->
- <mybatis.version>3.2.4</mybatis.version>
- <!-- log4j日志文件管理包版本 -->
- <slf4j.version>1.6.6</slf4j.version>
- <log4j.version>1.2.9</log4j.version>
- </properties>
- <build>
- <finalName>qbcrawl</finalName>
- </build>
- </project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<!-- spring核心包 -->
<!-- springframe start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- springframe end -->
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<!-- junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>net.sourceforge.htmlcleaner</groupId>
<artifactId>htmlcleaner</artifactId>
<version>2.8</version>
</dependency>
<dependency>
<groupId>tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>5.5.23</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<groupId>qb</groupId>
<artifactId>qb</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<url>http://maven.apache.org</url>
<properties>
<!-- spring版本号 -->
<spring.version>3.2.4.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.4</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.9</log4j.version>
</properties>
<build>
<finalName>qbcrawl</finalName>
</build>
</project>
tool.xml和velocity.properties 两个文件,在jar包中都可以找到的
velocity.jar velocity.properties -->> org.apache.velocity.runtime.defaults.velocity.properties
velocity-tool.jar tools.xml -->> org\apache\velocity\tools\generic\tools.xml 记得改下标签。
velocity.properties要改一下
- velocimacro.library = /macros.vm
velocimacro.library = /macros.vm
- input.encoding=UTF-8
- output.encoding=UTF-8
input.encoding=UTF-8
output.encoding=UTF-8
macros.vm这个随你自己想不想要,反正默认也是会加载好多spring的宏的。编码是一定要设置的。
这些设置完了,还有一点非常重要,这点纠结了我很久 。
就是你明明已经都配置好了,自己都觉得没问题了,action都进了,最后跳到模板引擎去加载模板的时候,告诉你这个模板找不到!!
经过我仔细排查,是velocity.properties多了句配置,自己需要注释一下。
- #file.resource.loader.path = .
#file.resource.loader.path = .
将这句配置注释,这是说,路径为properties文件路径,但如果你的properties跟你的templates不在一个目录,这时候就会出问题了。我调进去看过,它默认就是templates目录,上面宏的地址,也是相对于templates的目录。