如何安装和使用 Apache Velocity? [英] How do I install and use Apache Velocity?

查看:42
本文介绍了如何安装和使用 Apache Velocity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了 Apache 服务器 2.4 和 Ant 1.8.3,我已经下载了 Velocity 1.7 和 Velocity 工具 2.0.我已经阅读了 Apache Velocity 的安装文档大约十次,并在谷歌上搜索了两天,我仍然不知道如何处理这些文件.任何人都可以提供有关如何安装 Velocity 的详细说明吗?

I have installed Apache server 2.4, and Ant 1.8.3, I have downloaded Velocity 1.7, and Velocity tools 2.0. I have read the installation documentation for Apache Velocity about ten times, and Googled it for two days, I still have no idea what to do with these files. Can anyone provide a detailed description on how to install Velocity please?

我希望使用 Eclipse 进行开发,所以我也安装了它.

I was hoping to develop using Eclipse, so I have also installed this.

如果我遇到困难,我将不胜感激.

I would appreciate any assistance as I am stuck.

谢谢.

更新:

我已经将 Eclipse 配置为使用 Tomcat 并按照我的方式完成了教程并设法使这一切正常工作,但我不知道如何在 Eclipse 中启动速度项目,如果有人使用 Eclipse 构建速度项目,我将不胜感激有关如何设置的建议.

I have configured Eclipse to use Tomcat and have worked my way through a tutorial and managed to get this all working, but I do not know how exactly to start a velocity project in Eclipse, if anyone has used Eclipse to build a Velocity project, I would appreciate some advice on how to set that up.

谢谢.

推荐答案

您还需要一个 Java EE 容器(又名 servlet 容器),例如 Apache Tomcat.Apache HTTP 服务器对 Java 一无所知.请注意,Tomcat 本身可以直接用作 HTTP 服务器 - 您通常会选择将 Apache HTTP 服务器保留在生产服务器上的 Tomcat 前面,以加快静态文件请求以及其他原因(Apache HTTP 可以使用 mod_proxy_ajp 将请求转发到 Tomcat模块).但是一开始,直接使用Tomcat提供的HTTP服务比较容易.

You will also need a Java EE container (aka servlet container) like Apache Tomcat. The Apache HTTP server doesn't know anything about Java. Please note that Tomcat can itself be used directly as an HTTP server - you would typically choose to keep Apache HTTP server in front of Tomcat on production servers to speed up static files requests among other reasons (Apache HTTP can forward requests to Tomcat with the mod_proxy_ajp module). But for the beginning, it's easier to directly use the HTTP service provided by Tomcat.

然后,您需要熟悉Web 应用程序的概念.它只不过是特定的文件层次结构(可以将其压缩为带有 .war 扩展名的 jar 文件).对于使用 Velocity 的 Web 应用程序,这通常是:

Then, you need to get familiar with the concept of a Web Application. It's nothing more than a specific hierarchy of files (that can be compressed into a jar file with the .war extension). For a web application using Velocity, that would typically be:

./←Web 应用程序的根目录
./index.vhtml ←您的欢迎页面模板
./foo/bar.vhtml ←包含您的网络资源的任何其他文件或子目录
./WEB-INF/←WEB-INF 目录包含所有 Web 应用程序配置
./WEB-INF/web.xml ←将 HTTP 请求映射到过滤器和 servlet
./WEB-INF/tools.xml ←Velocity 自定义工具的可选配置文件
./WEB-INF/velocity.properties ←用于调整 Velocity 配置的可选文件
./WEB-INF/lib/←包含您的 Web 应用程序所需的所有库
./WEB-INF/src/←包含您的自定义 Java 类源代码
./WEB-INF/classes/←包含您的自定义 Java 类

./ ← root of your web application
./index.vhtml ← your welcome page template
./foo/bar.vhtml ← any other file or subdirectory containing your web resources
./WEB-INF/ ← the WEB-INF directory contains all web application configuration
./WEB-INF/web.xml ← maps HTTP requests towards filters and servlets
./WEB-INF/tools.xml ← optional configuration file for your Velocity custom tools
./WEB-INF/velocity.properties ← optional file to tune Velocity configuration
./WEB-INF/lib/ ← contains all the libraries needed by your web application
./WEB-INF/src/ ← contains your custom Java classes source code
./WEB-INF/classes/ ← contains your custom Java classes

您的 web.xml 文件必须将足够的请求映射到 VelocityViewServlet.它看起来像这样:

Your web.xml file has to map adequate requests towards the VelocityViewServlet. It will look like this:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <servlet>
    <servlet-name>view</servlet-name>
    <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>view</servlet-name>
    <url-pattern>*.vhtml</url-pattern>
  </servlet-mapping>

</web-app>

我无法为您提供 Eclipse 的帮助,因为我不使用它,但是如果您搜索 tomcat + eclipse,似乎有几个在线教程.这里有一个看起来很有趣:
http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html

I cannot help you much with Eclipse as I don't use it, but there seems to be several tutorials online if you search for tomcat + eclipse. Here's one that looks interesting:
http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html

这篇关于如何安装和使用 Apache Velocity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆