springMVC spring mvc velocity的模板引擎详解编程语言

spring mvc支持velocity的模板引擎, 我们看下如何配置。

从pom.xml文件开始, 在pom文件中需要添加velocity和spring相关依赖:

<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/maven-v4_0_0.xsd"> 
    <parent> 
        <artifactId>j</artifactId> 
        <groupId>hello</groupId> 
        <version>1.0-SNAPSHOT</version> 
    </parent> 
    <modelVersion>4.0.0</modelVersion> 
    <artifactId>hello-web</artifactId> 
    <packaging>war</packaging> 
    <name>hello-web Maven Webapp</name> 
    <url>http://maven.apache.org</url> 
    <properties> 
        <spring.version>3.2.3.RELEASE</spring.version> 
    </properties> 
    <dependencies> 
        <dependency> 
            <groupId>junit</groupId> 
            <artifactId>junit</artifactId> 
            <version>3.8.1</version> 
            <scope>test</scope> 
        </dependency> 
        <!-- spring framework related dependencies --> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-web</artifactId> 
            <version>${spring.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-context</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-context-support</artifactId> 
            <version>${spring.version}</version> 
        </dependency> 
        <!-- velocity related dependencies --> 
        <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> 
    <build> 
        <finalName>hello-web</finalName> 
        <plugins> 
            <plugin> 
                <groupId>org.mortbay.jetty</groupId> 
                <artifactId>maven-jetty-plugin</artifactId> 
                <version>6.1.26</version> 
                <configuration> 
                    <connectors> 
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> 
                            <port>8080</port> 
                        </connector> 
                    </connectors> 
                    <scanIntervalSeconds>10</scanIntervalSeconds> 
                </configuration> 
            </plugin> 
        </plugins> 
    </build> 
</project>

下一步需要配置web.xml文件:

<!DOCTYPE web-app PUBLIC 
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
        "http://java.sun.com/dtd/web-app_2_3.dtd" > 
<web-app> 
    <display-name>Archetype Created Web Application</display-name> 
 
    <servlet> 
        <!-- Override init parameter to avoid nasty --> 
        <!-- file locking issue on windows.         --> 
        <servlet-name>default</servlet-name> 
        <init-param> 
            <param-name>useFileMappedBuffer</param-name> 
            <param-value>false</param-value> 
        </init-param> 
    </servlet> 
 
    <!-- spring mvc servlet --> 
    <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/spring-mvc.xml</param-value> 
            <description>spring mvc config file</description> 
        </init-param> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>springmvc</servlet-name> 
        <url-pattern>/</url-pattern> 
    </servlet-mapping> 
 
</web-app>

在web.xml中指定的spring配置文件位置为spring/spring-mvc.xml, spring-mvc.xml文件内容如下:

<?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="cn.outofmemory.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> 
 
    <!--org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 
        <property name="suffix" value=".vm"/> 
        <property name="prefix" value="/views"/> 
        <!-- 使用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>

请注意看注释内容, 在VelocityViewResolver bean中指定了toolbox.xml, 其内容如下:

<?xml version="1.0" encoding="utf-8"?> 
<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>

另外我们还需要在resources中配置velocity.properties文件,设定velocity的选项, 其内容如下,你可以根据自己需要来做相应的调整

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

以上spring mvc + velocity相关配置就完成了,下面我们用一个具体的例子来做下测试:

新建一个Controller类,添加index方法:

package cn.outofmemory.controller; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
 
/** 
 * Created by yukaizhao on 2015/10/17. 
 */ 
@Controller 
public class IndexController { 
 
    /** 
     *  首页处理方法 
     * @return 首页对应的view名称  
     */ 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String index() { 
        return "/index"; 
    } 
}

然后我们还需要在web项目的WEB-INF目录下建好views目录,并新建index.vm 文件:

<!doctype html> 
<html> 
    <head> 
        <title>hello web application</title> 
    </head> 
    <body> 
        <h1>hello web application</h1> 
    </body> 
</html>

运行项目即可看到vm模板引擎正常工作了, 本文源码提供如下:

spring mvc + velocity 源码

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/8014.html

(0)
上一篇 2021年7月18日
下一篇 2021年7月18日

相关推荐

发表回复

登录后才能评论