0

我按照示例中所示的说明来渲染速度模板:

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
4

1 回答 1

2

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

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 文件夹吗?

于 2014-04-05T04:22:19.703 回答