简体   繁体   English

Spark + Velocity:无法加载资源(ResourceNotFoundException)

[英]Spark + Velocity: Unable to load resources (ResourceNotFoundException)

I'm currently using Spark and trying to use templates via Velocity based on the documentation .我目前正在使用 Spark 并尝试根据文档通过 Velocity 使用模板。

I'm however having some errors, could anyone provide guidance on how I can successfully load the templates?但是,我遇到了一些错误,有人可以提供有关如何成功加载模板的指导吗?

Aug 22, 2017 2:26:43 PM org.apache.velocity.runtime.log.JdkLogChute log
SEVERE: ResourceManager : unable to find resource 'www/templates/template.html' in any resource loader.
[qtp1951434327-14] ERROR spark.http.matching.GeneralError - 
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'www/templates/template.html'
public class Minimal {
        
    public static void main(String[] args) {
        get("test", (req, res) -> {
            Map<String, Object> model = new HashMap<>();
            return render(model, "www/templates/template.html");
        });
    }
        
    public static String render(Map<String, Object> model, String path) {
        return new VelocityTemplateEngine().render(new ModelAndView(model, path));
    }
}

I've read and tried solutions on many Stack Overflow questions, however none of them seem to be helping in my case.我已经阅读并尝试了许多 Stack Overflow 问题的解决方案,但似乎没有一个对我有帮助。

Some solutions have included setting properties, or having a particular project structure etc. Though it appears none of these have solved the issue, and leads to the same exception.一些解决方案包括设置属性,或具有特定的项目结构等。尽管这些解决方案似乎都没有解决问题,并导致相同的异常。

You need to put template inside resources folder.您需要将模板放在资源文件夹中。 See example in spark, you can see maven's pom resources definitions.见spark中的例子,可以看到maven的pom资源定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM