1. 程式人生 > >springMVC+velocity配置

springMVC+velocity配置

閒話不多說,以下是本人整合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>

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>

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
input.encoding=UTF-8
output.encoding=UTF-8

macros.vm這個隨你自己想不想要,反正預設也是會載入好多spring的巨集的。編碼是一定要設定的。

這些設定完了,還有一點非常重要,這點糾結了我很久 。

就是你明明已經都配置好了,自己都覺得沒問題了,action都進了,最後跳到模板引擎去載入模板的時候,告訴你這個模板找不到!!

經過我仔細排查,是velocity.properties多了句配置,自己需要註釋一下。

#file.resource.loader.path = .

將這句配置註釋,這是說,路徑為properties檔案路徑,但如果你的properties跟你的templates不在一個目錄,這時候就會出問題了。我調進去看過,它預設就是templates目錄,上面巨集的地址,也是相對於templates的目錄。