在Spring MVC中加载Velocity模板时出错(org.apache.velocity.exception.ResourceNotFoundException:无法找到资源)

时间:2017-02-03 18:07:19

标签: spring-mvc velocity

我得到了 org.apache.velocity.exception.ResourceNotFoundException:无法找到资源错误。有人可以帮我弄清楚这个问题。我已经附上了我的代码。感谢任何帮助。谢谢。

I placed the .vm in the class path src folder.

Controller look like

@RequestMapping("/velocity") public String velocity(final HttpServletRequest request, final HttpServletResponse response) { final VelocityEngine ve = new VelocityEngine(); ve.setProperty("resource.loader", "class"); ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); ve.init(); /* next, get the Template */ final Template t = ve.getTemplate("index.vm"); /* create a context and add data */ final VelocityContext context = new VelocityContext(); context.put("members", "sharat"); /* now render the template into a StringWriter */ final StringWriter writer = new StringWriter(); t.merge(context, writer); final String Html = writer.toString(); return Html; }

弹簧servlet.xml中

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <property name="resourceLoaderPath" value="/" />
</bean>

 <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
    <property name="cache" value="true"/>
    <property name="prefix" value=""/>
    <property name="suffix" value=".vm"/>
</bean>

错误:

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'index.vm'
org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:474)
org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:352)
org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1533)
org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1514)
org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:373)

velocity.properties

resource.loader = class file.resource.loader.description = Velocity File Resource Loader file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader file.resource.loader.path = vm file.resource.loader.cache = false file.resource.loader.modificationCheckInterval = 0

1 个答案:

答案 0 :(得分:0)

您需要替换

resource.loader =  class

resource.loader =  file

此外,如果您使用的是网络应用,则应检查WebappResourceLoader子项目中的Velocity Tools,这将帮助您指定相对于网络应用根目录的路径。您可以轻松找到在线how to configure Spring with Velocity Tools

否则,您必须为file.resource.loader.path属性指定绝对路径,或者确保webapp容器的当前执行路径包含一个带有模板的vm目录。

相关问题