2010-08-09 15 views
17

Bir şeyler yanlış ve çok sinir bozucu. Bir webapp çalıştırdığımda, bazı özelliklerin ayarlanması gerektiğini hız sayfasının ana sayfasında okudum. Ve bunu yaptım ama ne yaparsam yapayım aynı hatayı almaya devam ediyorum. BenVelocity şu sorguyu bulamıyor

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

dosyaları/WEB-INF/mailtemplates/kaydedilen elbette sahne ve kullanım hızını ayarlamak nerede
budur. Bunu kullanırsanız
bu hatayı alıyorum:

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

:)

cevap

27

Velocity Tools servlet tarafından sunulan sayfalar için tasarlanmış Webapp resourceloader kullanıyorsunuz. (Servlet içeriğinin kökü bulmak için bazı özel başlatma gerektirir).

ClasspathResourceLoader'ı kullanmanızı ve dosyaları WEB-INF/sınıflarına veya sınıfınızın başka bir yerine koymanızı tavsiye ederim. Bu gerçekten en doğru yaklaşımdır.

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

diğer bilgiler burada:

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

+1

@Will Glass, kodunuzu nereye yazacağınızı lütfen açıklayın – gstackoverflow

1

Hız muhtemelen bu dosyaları bulmak için sınıf yükleyicisi kullanıyor zaman için teşekkür ederiz. Varsayılan olarak CLASSPATH'de bulunan WEB-INF/sınıflarına koymanızı öneririz.

+1

Bunun işe yarayacağını düşünmüyorum. WebappResourceLoader, VelocityEngine'nin ServletContext'e başvurulmadan başlatıldığından beri bilmediği webapp kökündeki kaynakları arar.ClasspathResourceLoader'ı düşünüyorsunuz - cevabımı görün. –

2

Will Cam cevabın doğru olduğunu, ancak yapılandırma olmalıdır:

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

Not class ikinci satırın başında . Daha fazla bilgi için onun sağladığı bağlantıları görün.

Not: Ayrıcalıklardan dolayı yorum yerine bir cevap vermek.

0

Ben

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

velocity.properties dosya

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 

In And my java sınıfının en

, o kadar izleyin iyiyim Ve projenin VM argümanlar eklemek takip et ..

Size için yararlı olabilir
-DstaticDir= "your directory for template path" 

... WEB-INF/lib bu hatayı --WEB-INF/sınıfları ve tüm JARs çözme için

0

CLASSPATH'e içindedir. Klasörünüzü WEB-INF/sınıfları altındaki .vm dosyalarıyla taşımayı deneyin. abc.vm dosyası/public_html/WEB-INF klasöründeyse, hız şablon yolu için path = "/public_html/WEB-INF/abc.vm" yazın.