ResourceNotFoundException:用于spark-java和velocity

时间:2014-01-24 19:39:51

标签: java velocity spark-java

我正在按照示例中所示的指令来渲染速度模板:

public static void main(final String[] args) {

    get(new VelocityRoute("/hello") {
        @Override
        public Object handle(final Request request, final Response response) {
            Map<String, Object> model = new HashMap<>();
            model.put("hello", "Velocity World");
            model.put("person", new Person("Foobar"));
            // The wm files are located under the resources directory
            return modelAndView(model, "hello.wm");
        }
    });

}

但速度似乎找不到我的模板:

ERROR spark.webserver.MatcherFilter - 
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'hello.wm'
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474) ~[velocity-1.7.jar:1.7]
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352) ~[velocity-1.7.jar:1.7]
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533) ~[velocity-1.7.jar:1.7]
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1514) ~[velocity-1.7.jar:1.7]
    at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:373) ~[velocity-1.7.jar:1.7]
    at spark.template.velocity.VelocityRoute.render(VelocityRoute.java:117) ~[spark-template-velocity-1.0.jar:na]
    at spark.TemplateViewRoute.render(TemplateViewRoute.java:43) ~[spark-core-1.1.1.jar:na]
    at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:140) ~[spark-core-1.1.1.jar:na]
    at spark.webserver.JettyHandler.doHandle(JettyHandler.java:54) [spark-core-1.1.1.jar:na]
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:179) [jetty-server-9.0.2.v20130417.jar:9.0.2.v20130417]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136) [jetty-server-9.0.2.v20130417.jar:9.0.2.v20130417]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.0.2.v20130417.jar:9.0.2.v20130417]
    at org.eclipse.jetty.server.Server.handle(Server.java:451) [jetty-server-9.0.2.v20130417.jar:9.0.2.v20130417]
    at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:252) [jetty-server-9.0.2.v20130417.jar:9.0.2.v20130417]
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:266) [jetty-server-9.0.2.v20130417.jar:9.0.2.v20130417]
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:240) [jetty-io-9.0.2.v20130417.jar:9.0.2.v20130417]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596) [jetty-util-9.0.2.v20130417.jar:9.0.2.v20130417]
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527) [jetty-util-9.0.2.v20130417.jar:9.0.2.v20130417]
    at java.lang.Thread.run(Thread.java:744) [na:1.7.0_51]

我不确定问题是什么。我试着玩速度,但它似乎永远不会找到我的模板。

有关信息,以下是我的项目的样子:

project-root
 | src/main/java
 |  | com.stackoverflow.my-spark-app
 |  |   | SparkMain.java
 |  |   | Person.java
 | src/main/resources
 |  |   | hello.wm
 | pom.xml

1 个答案:

答案 0 :(得分:2)

实际上,它对我有用。我用速度模板引擎创建了一个简单的火花应用程序。

public class Server {

    public static void main(String[] args) {
        Spark.get(new VelocityRoute("/hello") {
            @Override
            public Object handle(final Request request, final Response response) {
                Map<String, Object> model = new HashMap<String, Object>();
                model.put("hello", "Velocity World");
                model.put("person", new Person("Foobar"));

                // The wm files are located under the resources directory
                return modelAndView(model, "hello.wm");
            }
        });
    }

    public static class Person {
        private String name;

        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

我的目录结构类似

src
    main
        java
            com.stackoverflow
                Server.java
        resources
            hello.wm

它确实拿起了模板。

您使用的是哪种版本的spark-core和spark-template-velocity?你能检查模板文件是否被复制到target \ classes文件夹吗?