【发布时间】:2014-10-27 18:28:21
【问题描述】:
我想首先说明我 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) 中,我基本上遵循了这个 tutorial:
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();
}
}
web.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>
velocity.properties
webapp.resource.loader.path=/WEB-INF/templates/
...和一个简单的 index.vm(例如,如果您在教程网站上注意到有一个不正确的结束 html 标记)
<html>
<body>
<h2>My text is : $serv.getMessage() </h2>
</body>
</html>
左侧的文件结构:
抱歉发了这么长的帖子,如有任何帮助,将不胜感激!
【问题讨论】:
-
您是否尝试过在 org.apache.velocity.tools.view.servlet.VelocityViewServlet 之后删除 web.xml 中的尾随空格?
-
@Jako 我现在试过了,遗憾的是它没有解决问题:(,但谢谢你的回复。我也会编辑这篇文章中的代码。
标签: java eclipse tomcat servlets velocity