• Velocity之Hello World(tomcat下配置Velocity)


    本文主要参考:http://hi.baidu.com/dalianjingying/item/1fb3a98ad64dcac299255f72

                                 http://wangbaoaiboy.blog.163.com/blog/static/52111910200710234721806/


    1、配置需要的jar包:

        (1.1)下载 velocity-tools-2.0.zip ,下载地址 http://archive.apache.org/dist/velocity/tools/


         (1.2)解压velocity-tools-2.0.zip ,拷贝解压后到目录下的 lib 文件夹下,拷贝所有的jar包 到D:Program Files (x86)apache-tomcat-7.0.42-windows-x64apache-tomcat-7.0.42webappswebDemoWEB-INFlib (如果没有lib,则自己建立lib)


    PS1:velocity-tools-2.0目录结构:

     D:Program Files (x86)velocity-1.6.3velocity-tools-2.0velocity-tools-2.0 的
    目录


    2013/08/08  11:28    <DIR>          .
    2013/08/08  11:28    <DIR>          ..
    2013/08/08  11:28               618 CONTRIBUTORS
    2013/08/08  11:28    <DIR>          docs
    2013/08/08  11:28    <DIR>          examples
    2013/08/08  11:28    <DIR>          lib
    2013/08/08  11:28            11,558 LICENSE
    2013/08/08  11:28               465 NOTICE
    2013/08/08  11:28             2,690 README.txt
    2013/08/08  11:28             1,416 STATUS
    2013/08/08  11:28               772 WHY_THREE_JARS.txt
                   6 个文件         17,519 字节
                   5 个目录 68,890,578,944 可用字节


    PS2:

     tools里有很多VTL页面用到的常用工具模块,相对jsp的常用工具来说,VTL将直接引用tools里的method进行事务处理,而不像jsp需要引用对象的属性来进行操作,省去了很多冗余的代码,方便页面制作者的眼睛。


    2、配置velocityViewServlet

       (2.1)在web.xml里面添加如下  <servlet> ***</servlet-mapping> 之间的配置

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!--
     Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at
    
          http://www.apache.org/licenses/LICENSE-2.0
    
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    -->
    
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0"
      metadata-complete="true">
      
      <servlet>
         <servlet-name>velocityView</servlet-name>
         <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
      <init-param>
         <param-name>org.apache.velocity.toolbox</param-name>
         <param-value>/WEB-INF/toolbox.xml</param-value>
      </init-param>
      
      <!--
      <init-param>
       <param-name>org.apache.velocity.properties</param-name>
       <param-value>/WEB-INF/velocity.properties</param-value>
      </init-param>
      -->
      
      <load-on-startup>10</load-on-startup>
    </servlet>
    <servlet-mapping>
         <servlet-name>velocityView</servlet-name>
         <url-pattern>*.vm</url-pattern>
    </servlet-mapping>
      
    </web-app>

           作下解释,velocityView的servlet定义, 也就是VTL处理时将全权交给这个servlet,而下面几行servlet-mapping代码,定义了处理文件的后缀为.vm的文件将全数交给viewServlet处理,很简单吧?hoho

    继续解释中间那段param设定,这里将用到一个toolbox的定义,引用toolbox的定义来初始化viewServlet

        

        (2.2)在web.xml同级目录下新建 toolbox.xml,内容如下

    <?xml version="1.0"?>
    <toolbox>
       <tool>
         <key>date</key>
         <scope>application</scope>
         <class>org.apache.velocity.tools.generic.DateTool</class>
     </tool>
     <tool>
        <key>math</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.MathTool</class>
     </tool>
    </toolbox>

    其实就是将一个标准时间和数学计算的class加载到viewServlet里,使得VTL可以处理简单的计算和时间显示:>

    以上完成velocity的VTL配置。

    3、重启tomcat,完成配置


    4、写vm,验证vm运行结果

    //HelloVelocity.vm

    <html>
    <body>
    #set( $foo = "Velocity" )
    Hello $foo World!
    </body>
    <html>


    验证结果:
    http://localhost:8888/webDemo/Velocity/HelloVelocity.vm

    Hello Velocity World!



    附录:

    1、Velocity Tools 是 Velocity 模板引擎的一个子项目,用来将 Velocity 与 Web开发环境集成的工具包。例如你可以利用 VelocityTools 来集成 Velocity 和 Struts 框架,同时 VelocityTools 还提供 Velocity 的布局模板,以及很多常用的工具包。[1]

  • 相关阅读:
    PHP字符串
    PHP第四章数组2
    oop soa cbd
    spring 时间组件
    IReport 常见问题整理
    maven 与 jenkins 集成的时候,打包出现错误处理
    Mybatis 传递参数中的_paramter 的理解
    JS 之表单特殊控制
    JQuery 中设置AJAX 的全局函数
    spring + ehcache 整合
  • 原文地址:https://www.cnblogs.com/wxdlut/p/3246936.html
Copyright © 2020-2023  润新知