我想首先说明我是 90% 的新手,这意味着 Java、Eclipse、模板引擎。
在我尝试创建一个按预期运行的速度模板时,我使用了 Eclipse Luna(EE)、Tomcat 7、Apache Velocity1.7、Velocity Tools 2.0、VelocityToolsView 2.0
问题 是,当我从 Eclipse 启动 Tomcat(如果可能的话,基本上运行项目),然后转到“localhost:8080/VelocityTemplateHomework4/index.vm”,它只会给我一个完整的页面出现错误“HTTP 状态 500 - 实例化 servlet 类 org.apache.velocity.tools.view.servlet.VelocityViewServlet 时出错”
在 eclipse (EE) 中,我基本上遵循了这个教程:
http://thegeekhead.blogspot.ro/2009/06/how-to-configure-eclipse-tomact-55.html
无论如何我都会发布我的代码,但也请检查链接(也许那里有问题,因为它有点过时了)
SimpleServlet.java
package myPackage;
public class SimpleServlet {
private String message = "Hello Damn World!!!";
public String getMessage() {
return message;
}
public void setMessage(String m) {
message = m;
}
/** To test exception handling in templates. */
public boolean whine() {
throw new IllegalArgumentException();
}
}
网络.xml
<?xml version='1.0' encoding='utf-8'?>
<web-app>
<!-- Define Velocity template compiler -->
<servlet>
<servlet-name>velocity</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>
</servlet>
<!-- Map *.vm files to Velocity -->
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
</web-app>
工具箱.xml
<?xml version="1.0"?>
<toolbox>
<xhtml>true</xhtml>
<tool>
<key>serv</key>
<scope>request</scope>
<request-path>index.vm</request-path>
<class>SimpleServlet</class>
</tool>
<data type="number">
<key>version</key>
<value>1.1</value>
</data>
<data type="boolean">
<key>isSimple</key>
<value>true</value>
</data>
<data type="string">
<key>foo</key>
<value>this is foo.</value>
</data>
<data type="string">
<key>bar</key>
<value>this is bar from velocity.</value>
</data>
<tool>
<key>map</key>
<scope>session</scope>
<class>java.util.HashMap</class>
</tool>
<tool>
<key>date</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.DateTool</class>
</tool>
</toolbox>
速度.属性
webapp.resource.loader.path=/WEB-INF/templates/
...和一个简单的 index.vm(例如,如果您在教程网站上注意到有一个不正确的结束 html 标记)
<html>
<body>
<h2>My text is : $serv.getMessage() </h2>
</body>
</html>
左边的文件结构:
抱歉发了这么长的帖子,我们将不胜感激!
最佳答案
它似乎没有找到您的库。
我会尝试将速度库移动到您的 WEB-INF/lib 文件夹中,以便它们与您的应用程序一起部署在服务器上,或者尝试将速度库添加到您的 tomcat 服务器的 lib 文件夹中。
关于java - Eclipse 中的 Apache Velocity 错误 [Java、Eclipse Luna、Velocity 2.0、VelocityView、Tomcat 7],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26594346/