Appache Velocity 的新手并尝试在 Eclipse 中运行我的第一个 vm 页面(没有成功)。以下是我目前采取的步骤:
- 下载 Velocity 引擎分发版:http://velocity.apache.org/download.cgi#Engine
- 在 Eclipse 中创建动态 Web 项目
- 选择 Tomcat 作为服务器
- 在 WebContent 文件夹中创建一个简单的 index.vm 文件
- 将 velocity-1.7.zip 中的所有 jar 文件添加到我的项目类路径
- 运行 index.vm,按原样输出所有 VTL(不是“vm-ized”)。
index.vm:
<html>
<body>
#set( $foo = "Velocity" )
Hello $foo World!
</body>
<html>
非常感谢任何帮助!
最佳答案
好的,让它在 Eclipse 中运行。只需将所有 Velocity Jar 文件添加到我的动态 Web 项目中 WEB-INF 文件夹的 lib 目录中。然后像这样更新 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Velocity_Test</display-name>
<welcome-file-list>
<welcome-file>index.vm</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Define Velocity template compiler -->
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
</servlet>
<!-- Map *.vm files to Velocity -->
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
</web-app>
关于Eclipse + Apache 速度引擎?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9363643/