2010-08-09 129 views
17

有些事情是错的,这是非常令人沮丧的。我在velocity的主页上看过,当我运行一个webapp时,应该设置一些属性。我已经这样做了,但无论我做什么,我都会收到同样的错误。
这是我设置的道具和使用速度速度找不到资源

public class ConfirmationMailGenerator implements MailGenerator { 

    private BasicUser user; 
    private String htmlTemplate = "HTMLConfirmationMailTemplate.vsl"; 
    private String plainTemplate = "PlainConfirmationMailTemplate.vsl"; 

    public ConfirmationMailGenerator(BasicUser user) { 
     this.user = user; 
    } 

    public StringWriter generateHTML() throws Exception { 
     Properties props = new Properties(); 
     props.setProperty("resource.loader", "wepapp"); 
     props.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.WebappResourceLoader"); 
     props.setProperty("webapp.resource.loader.path", "/WEB-INF/mailtemplates/"); 
     VelocityEngine engine = new VelocityEngine(props); 
     VelocityContext context = new VelocityContext(); 

     engine.init(); 

     Map map = createDataModel(); 
     context.put("user", map); 

     Template template = engine.getTemplate(htmlTemplate); 
     StringWriter writer = new StringWriter(); 
     template.merge(context, writer); 

     return writer; 
    } 
... 
} 

的文件当然是保存在/ WEB-INF/mailtemplates /的。
如果我用这个,我得到这个错误:

SEVERE: ResourceManager : unable to find resource 'HTMLConfirmationMailTemplate.vsl' in any resource loader. 
SEVERE: The log message is null. 

感谢您的时间:)

回答

27

您正在使用Webapp资源加载器,该加载器专用于Velocity Tools servlet提供的页面。 (它需要一些特殊的初始化来查找servlet上下文的根)。

我建议您使用ClasspathResourceLoader,然后将这些文件放入WEB-INF/classes或类路径中的其他地方。这真的是最直接的方法。

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

更多信息是在这里:

https://velocity.apache.org/engine/1.7/apidocs/org/apache/velocity/runtime/resource/loader/ClasspathResourceLoader.html

+1

@Will Glass请解释在哪里编写代码 – gstackoverflow 2015-05-29 18:57:59

1

速度可能使用的类加载器来查找这些文件。我建议将它们放在WEB-INF/classes中,默认情况下它在CLASSPATH中。

+1

我不认为这会奏效。 WebappResourceLoader在webapp的根目录中查找资源,这是因为VelocityEngine在未引用ServletContext的情况下被初始化,所以它不知道。您正在考虑ClasspathResourceLoader - 请参阅我的回应。 – 2010-08-14 21:58:01

2

威尔玻璃的答案是正确的,但配置应该是:

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

注意class在第二行的开头。查看他提供的链接以获取更多详细信息!

注意:由于权限而不是评论而不是回复。

0

我很好它作为跟随,

velocity.properties文件

resource.loader=class, file 
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader 
file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader 
file.resource.loader.path=vm_template 
runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem 
runtime.log.logsystem.log4j.category=velocity 
input.encoding=UTF-8 
output.encoding=UTF-8 

而在我的Java类

import java.io.StringWriter; 
import java.util.Properties; 
import org.apache.log4j.Logger; 
import org.apache.velocity.Template; 
import org.apache.velocity.VelocityContext; 
import org.apache.velocity.app.Velocity; 
import org.apache.velocity.exception.ParseErrorException; 
import org.apache.velocity.exception.ResourceNotFoundException; 
import org.apache.velocity.tools.generic.DateTool; 
import org.apache.velocity.tools.generic.EscapeTool; 
import org.apache.velocity.tools.generic.LoopTool; 
import org.apache.velocity.tools.generic.MathTool; 
import org.apache.velocity.tools.generic.NumberTool; 
import org.apache.velocity.tools.generic.SortTool; 
import org.springframework.beans.factory.InitializingBean; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.Resource; 

public class VelocitySupport implements InitializingBean { 
private static Logger log = Logger.getLogger(VelocitySupport.class); 

@Autowired private Properties properties; 

public final void afterPropertiesSet() throws Exception { 
    location = location.replace("classpath:", ""); 
    Resource res = new ClassPathResource(location); 

    Properties prop = new Properties(); 
    prop.load(res.getInputStream()); 

    String staticDir = System.getProperty("staticDir"); 
    String tempPath = prop.getProperty("file.resource.loader.path"); 
    tempPath = staticDir + "/" + tempPath; 

    prop.setProperty("file.resource.loader.path", tempPath); 
    Velocity.init(prop); 
} 

public static String merge(final String template, final VelocityContext vc) throws Exception { 
    try { 
     vc.put("date", new DateTool()); 
     vc.put("escape", new EscapeTool()); 
     vc.put("math", new MathTool()); 
     vc.put("number", new NumberTool()); 
     vc.put("iterate", new LoopTool()); 
     vc.put("sort", new SortTool()); 

     Template temp = Velocity.getTemplate(template); 

     StringWriter sw = new StringWriter(); 
     temp.merge(vc, sw); 
     sw.flush(); 

     return sw.toString(); 
    } 
    catch (ResourceNotFoundException e) { 
     log.error("", e); 
     throw e; 
    } 
    catch (ParseErrorException e) { 
     log.error("", e); 
     throw e; 
    } 
} 

private String location; 

public final void setLocation(final String location) { 
    this.location = location; 
} 
} 

和接入项目的VM参数作为按照..

-DstaticDir= "your directory for template path" 

这可能对你有帮助 ...

0

为了解决这个错误 --Web-INF/classes和所有的JAR文件在WEB-INF/lib目录在类路径中。尝试使用WEB-INF/classes下的.vm文件移动文件夹012xx- 不要使用例如。如果abc.vm文件位于/ public_html/WEB-INF文件夹中,则为path模板路径放置path =“/public_html/WEB-INF/abc.vm”。