Question

I'm following the instruction as shown in the example to render a velocity template:

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");
        }
    });

}

but velocity does not seem to find my template:

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]

I'm not sure what the issue is. I tried to play around with velocity but it never seem to find my template.

For info, here is how my project looks like:

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

Solution

Actually, it works for me. I created a simple spark application with the velocity templating engine.

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;
        }
    }
}

My directory structure is similar

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

And it does pick up the template.

Which version of spark-core and spark-template-velocity are you using? Can you check if the template file is getting copied to the target\classes folder?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top