找不到速度模板,但为什么?

Velocity template not found but why?

我正在尝试使用 Velocity 模板引擎创建一个 hello world。这是我的代码:

package biz.tugay.app;

/* User: koray@tugay.biz Date: 22/06/15 Time: 10:36 */

import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloTest extends VelocityViewServlet {

    @Override
    protected Template handleRequest
            (HttpServletRequest request, HttpServletResponse response, Context ctx) {
        Template template = null;
        try {
            ctx.put("name", "Velocity Test");
            template = Velocity.getTemplate("hello.vm");
        } catch( Exception e ) {
            System.err.println("Exception caught: " + e.getMessage());
        }
        return template;
    }
}

我会得到:

SEVERE: ResourceManager : unable to find resource 'hello.vm' in any resource loader.
Exception caught: Unable to find resource 'hello.vm'

我在 WEB-INF 中有一个 velocity.properties 文件:

resource.loader = class
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

hello.vm 如果在 WEB-INF/classes 文件夹下找到如下所示:

hello $name

终于web.xml:

<web-app>
    <servlet>
        <servlet-name>HelloTest</servlet-name>
        <servlet-class>biz.tugay.app.HelloTest</servlet-class>
        <init-param>
            <param-name>properties</param-name>
            <param-value>/WEB-INF/velocity.properties</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloTest</servlet-name>
        <url-pattern>/HelloTest</url-pattern>
    </servlet-mapping>

</web-app>

我做错了什么?

好的,感谢@Amos,我让它像这样工作:

Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");                             
Velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ctx.put("name", "Velocity Test");                                                                
template = Velocity.getTemplate("hello.vm");