我正在为我的应用切换嵌入式Web服务器,但无法弄清楚如何让VelocityLayoutServlet在Jetty中加载其资源。每一个请求都是404,我没有想法。
请注意,应用程序要求是通过root,IE http://example.com/index.vm可访问servlet。服务器也必须嵌入,因为它只是一个正在运行的应用程序的视图。
基本测试代码(BotVelocityServlet扩展了VelocityLayoutServlet,因此我可以在fillContext()
中设置上下文变量):
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletHandler.addServlet(BotVelocityServlet.class, "/*");
server.setHandler(servletHandler);
server.start();
}
我的velocity.properties
由Velocity根据日志加载:
class.resource.loader.class = org.apache.velocity.tools.view.WebappResourceLoader
runtime.log.logsystem.class = org.apache.velocity.runtime.log.CommonsLogLogChute
velocimacro.library.autoreload = true
velocimacro.permissions.allow.inline.to.replace.global = true
class.resource.loader.cache = false
file.resource.loader.cache = false
webapp.resource.loader.cache = false
webapp.resource.loader.modificationCheckInterval = 100
# Layout files
tools.view.servlet.layout.directory = /
tools.view.servlet.layout.default.template = Layout.vm
使用index.vm
中的基本src/main/resources
文件,我清理构建,运行,访问http://localhost/index.vm
和... 404
我尝试过的步骤
手动设置jetty.home
。
File classesFolder;
if ((classesFolder = new File("src\\main\\resources")).exists())
rootPath = classesFolder.getAbsolutePath();
else if ((classesFolder = new File("app")).exists())
rootPath = classesFolder.getAbsolutePath();
else
rootPath = new File(".").getAbsolutePath();
log.debug("Current base: " + rootPath);
server.setAttribute("jetty.home", rootPath);
设置servletHandler
的上下文路径。
servletHandler.setContextPath("/");
将速度属性文件中的tools.view.servlet.layout.directory
和file.resource.loader.path
显式设置为`/ src / main / resources /
所有情况下的例外:
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource '/index.vm'
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:483) ~[velocity-1.6.2.jar:1.6.2]
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:354) ~[velocity-1.6.2.jar:1.6.2]
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1400) ~[velocity-1.6.2.jar:1.6.2]
at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:422) ~[velocity-1.6.2.jar:1.6.2]
at org.apache.velocity.tools.view.VelocityView.getTemplate(VelocityView.java:865) ~[velocity-tools-2.0.jar:2.0]
at org.apache.velocity.tools.view.VelocityView.getTemplate(VelocityView.java:821) ~[velocity-tools-2.0.jar:2.0]
at org.apache.velocity.tools.view.VelocityViewServlet.getTemplate(VelocityViewServlet.java:296) [velocity-tools-2.0.jar:2.0]
at org.apache.velocity.tools.view.VelocityViewServlet.handleRequest(VelocityViewServlet.java:257) [velocity-tools-2.0.jar:2.0]
at org.apache.velocity.tools.view.VelocityViewServlet.doRequest(VelocityViewServlet.java:217) [velocity-tools-2.0.jar:2.0]
at org.apache.velocity.tools.view.VelocityViewServlet.doGet(VelocityViewServlet.java:182) [velocity-tools-2.0.jar:2.0]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:735) ~[javax.servlet-3.0.0.v201112011016.jar:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) ~[javax.servlet-3.0.0.v201112011016.jar:na]
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:698) ~[jetty-servlet-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:505) ~[jetty-servlet-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:211) ~[jetty-server-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1094) ~[jetty-server-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:432) ~[jetty-servlet-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175) ~[jetty-server-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1028) ~[jetty-server-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136) ~[jetty-server-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) ~[jetty-server-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.server.Server.handle(Server.java:445) ~[jetty-server-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:267) ~[jetty-server-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:224) ~[jetty-server-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358) ~[jetty-io-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601) ~[jetty-util-9.0.4.v20130625.jar:9.0.4.v20130625]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532) ~[jetty-util-9.0.4.v20130625.jar:9.0.4.v20130625]
at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25]
我做错了什么?如何在嵌入Jetty内部时将VelocityView配置为加载资源?