2017-06-13 167 views
0

我正在學習如何將Apache Velocity與Apache Camel(在Karaf中)用於託管動態網頁,但是當我嘗試在另一個包含速度文件時收到資源錯誤即使他們在同一個目錄中。camel-velocity找不到資源

如果我從第一個文件中刪除行#parse("${page}.vm"),一切都按預期工作。

這是我的整個設置。除安裝camel-velocity和jetty-9外,此處未列出的所有內容均爲默認的ServiceMix 7.0.0安裝程序。

駱駝路線

<route id="web_route"> 
    <from uri="jetty:http://localhost:8080/test?sessionSupport=true&amp;matchOnUriPrefix=true"/> 
    <to uri="velocity:file:web/vm/webpage.vm?contentCache=false"/> 
</route> 

/web/vm/webpage.vm內容

Hello sir $!{headers.name}! Welcome to the VM World! 
#set($page="$headers.CamelHttpUrl") 
#set($page="$page.substring($page.lastIndexOf('/')).substring(1)") 
#parse("${page}.vm") 

/web/vm/hello.vm內容

Hello World! 

本地主機:8080 /測試/你好

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'hello.vm' 
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474) 
    at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352) 
    at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533) 
    at org.apache.velocity.runtime.directive.Parse.render(Parse.java:197) 
    at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:207) 
    at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:342) 
    at org.apache.velocity.runtime.RuntimeInstance.render(RuntimeInstance.java:1378) 
    at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1314) 
    at org.apache.velocity.app.VelocityEngine.evaluate(VelocityEngine.java:272) 
    at org.apache.camel.component.velocity.VelocityEndpoint.onExchange(VelocityEndpoint.java:212) 
    at org.apache.camel.impl.ProcessorEndpoint$1.process(ProcessorEndpoint.java:71) 
    at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) 
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145) 
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) 
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460) 
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196) 
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196) 
    at org.apache.camel.component.jetty.CamelContinuationServlet.service(CamelContinuationServlet.java:191) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669) 
    at org.eclipse.jetty.servlets.MultiPartFilter.doFilter(MultiPartFilter.java:146) 
    at org.apache.camel.component.jetty.CamelFilterWrapper.doFilter(CamelFilterWrapper.java:43) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) 
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) 
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221) 
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) 
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) 
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) 
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) 
    at org.eclipse.jetty.server.Server.handle(Server.java:499) 
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311) 
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) 
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) 
    at java.lang.Thread.run(Thread.java:745) 

那麼,爲什麼不能將其導入hello.vm?

+0

你在哪裏保持你的速度文件?資源? – soorapadman

+0

@soorapadman我將速度文件保存在%KARAF_HOME%/ web/vm /下,沒有設置資源 – Tezra

+1

在這種情況下,您不應該這樣通過。檢查此Url:http://camel.apache.org/using-propertyplaceholder.html – soorapadman

回答

0

我發現#parse()和#包括()引用文件相對%karaf_home%,而不是當前目錄。所以如果你想在%karaf_home%/web/vm/hello.vm包含資源,你必須使用#include("/web/vm/hello.vm")

1

您需要指定引用Karaf主目錄的文件位置。

您應該使用

<to uri="velocity:file:${karaf.home}/web/vm/webpage.vm?contentCache=false"/> 

看到更多細節在:http://camel.apache.org/using-propertyplaceholder.html

+0

webpage.vm不是問題,它加載得很好。錯誤是當我嘗試在webpage.vm中包含hello.vm時。如果我刪除'#parse(「$ {page} .vm」)',頁面加載正常。 (另外,camel在文件部分中抱怨我不能使用動態$ {}}如果我註釋掉該行,我會看到它的計算結果爲'#parse(「hello.vm」)',並且我希望能夠工作引用當前目錄 – Tezra