2

我正在为我的应用程序切换嵌入式 Web 服务器,但无法弄清楚如何让 VelocityLayoutServlet 在 Jetty 中加载其资源。每个请求都是 404,我没有想法。

请注意,应用程序要求 servlet 可以通过 root 访问,即http://example.com/index.vm。服务器也必须嵌入,因为它只是一个正在运行的应用程序的视图。

基本测试代码(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

在我清理构建、运行、访问和... 404中有一个基本index.vm文件src/main/resourceshttp://localhost/index.vm

我尝试过的步骤

  1. 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);
    
  2. 设置servletHandler的上下文路径。

    servletHandler.setContextPath("/");
    
  3. 在速度属性文件中显式设置tools.view.servlet.layout.directoryfile.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 以加载资源?

4

0 回答 0