velocity使用(二)--velocity与spring整合

来源:互联网 发布:明代斗彩天字罐淘宝 编辑:程序博客网 时间:2024/06/03 21:20

一、简介

在文章velocity使用(一)--简介中,我们对velocity进行了基本介绍。在本文,

将介绍velocity与spring的整合,以及自定义指定的使用。

二、velocity与spring整合的开发流程

1、添加maven依赖(spring的依赖在此略写)

<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>
2、配置velocity配置文件velocity.properties

input.encoding=UTF-8output.encoding=UTF-8contentType=text/html;charset=UTF-8resource.loader=webapp, classclass.resource.loader.description=Velocity Classpath Resource Loaderclass.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoaderwebapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoaderwebapp.resource.loader.path=/webapp.resource.loader.cache=false        userdirective=com.dragon.velocitystudy.velocity.ShowDirective
3、dispatchServlet配置dispatcher-servlet.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:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-3.2.xsd       http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">    <context:annotation-config />    <context:component-scan base-package="com.dragon.velocitystudy" />    <mvc:annotation-driven />    <mvc:resources mapping="/bootstrap/**" location="/bootstrap/"/>    <!--规定模板文件的类型和位置-->    <bean id="velocityConfigurer"          class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">        <property name="resourceLoaderPath" value="/views" />        <property name="configLocation" value="classpath:velocity.properties" />    </bean>    <!--配置附加工具,以及将后缀为vm的文件交给下面的VelocityViewResolver处理-->    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">        <property name="viewResolvers">            <list>                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />                <bean class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">                    <property name="prefix" value="/views/" />                    <property name="suffix" value=".vm" />                    <property name="contentType" value="text/html;charset=UTF-8" />                </bean>            </list>        </property>        <property name="defaultViews">            <list>                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />            </list>        </property>    </bean></beans>
4、web.xml配置

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring-context.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <servlet>        <servlet-name>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>                classpath:dispatcher-servlet.xml            </param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcher</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>/</welcome-file>    </welcome-file-list></web-app>
5、在配置路径/views/下添加vm文件即可

三、自定义指令开发流程

在使用velocity做模板时,除了默认的#foreach 、#if等指令外,还可以自定义指令。

1、实现org.apache.velocity.runtime.directive.Directive接口,创建自定义指令的控制类。

public class ShowDirective extends Directive {    /**     * 自定义指令名称     * @return     */    @Override    public String getName() {        return "show";    }    /**     * 自定义指令类型(包块模块和行模块)     * @return     */    @Override    public int getType() {        return BLOCK;    }    /**     * 指令操作     * @param context     * @param writer     * @param node     * @return     * @throws IOException     * @throws ResourceNotFoundException     * @throws ParseErrorException     * @throws MethodInvocationException     */    @Override    public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {        Node childNode1 = node.jjtGetChild(0);  //获取第一个参数        String p1 = childNode1.literal();        Node body = node.jjtGetChild(1); //获取最后一个参数的下一个,即是自定义指令内包含的内容        String bodyHtml = body.literal();        if("true".equals(p1)){  //显示原文            writer.write(bodyHtml);        }else{  //不显示原文            String cacheHtml = "no content";            writer.write(cacheHtml);        }        return true;    }}

2、配置文件velocity.properties添加自定义指令

input.encoding=UTF-8output.encoding=UTF-8contentType=text/html;charset=UTF-8resource.loader=webapp, classclass.resource.loader.description=Velocity Classpath Resource Loaderclass.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoaderwebapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoaderwebapp.resource.loader.path=/webapp.resource.loader.cache=false        userdirective=com.dragon.velocitystudy.velocity.ShowDirective

3、自定义指令使用

<!doctype html><htm>    <head>    </head>    <body>        #show(true)            content one        #end    <br/>        #show(false)            content two        #end    </body></htm>
输出:

content one 
no content




原创粉丝点击