如何正确设置加载速度路径

sto*_*ter 14 spring velocity spring-mvc

我希望我的velocityengine从设计的路径中寻找模板.我这样做了:

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
 <property name="velocityProperties">
   <value>
     resource.loader=class
     class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
     class.resource.loader.resourceLoaderPath=/mytemplates
   </value>
 </property>
Run Code Online (Sandbox Code Playgroud)

但仍在类文件夹中查找模板.任何的想法?

Rag*_*ram 22

如spring 文档中所示,您可以尝试以下操作:

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
  <property name="velocityProperties">
    <props>
      <prop key="resource.loader">file</prop>
      <prop key="file.resource.loader.class">
        org.apache.velocity.runtime.resource.loader.FileResourceLoader
      </prop>
      <prop key="file.resource.loader.path">${webapp.root}/WEB-INF/velocity</prop>
      <prop key="file.resource.loader.cache">false</prop>
    </props>
  </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

或者,您可以在a中声明这些属性velocity.properties并指定它

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
  <property name="configLocation" value="/WEB-INF/velocity.properties"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,你是如何定义$ {webapp.root}的? (10认同)
  • 你如何定义$ {webapp.root}? (3认同)

Sub*_*der 17

试试这个:

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="resourceLoaderPath" value="/email_templates/"/>
</bean>

<bean name="mailTest" class="com.crisil.web.MailTestController">
    <property name="velocityEngine" ref="velocityEngine"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

  • 这是一个更好的解决方案.它使用Spring的抽象来避免必须设置Velocity属性. (4认同)