2014-10-10 20 views
1

是否有任何方法可以使vm文件中所做的更改自动反映,而无需每次重新启动服务器。我现在处于开发阶段,我需要做很多改变。我是速度模板的新手,因此它会非常有帮助如果有人可以提出相同的方法。我尝试使用以下属性,但它不起作用。我正在使用tomcat服务器。更改速度模板以反映服务器未重新启动

<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 
      velocimacro.library.autoreload=true 
      class.resource.loader.cache=false 
      velocimacro.permissions.allow.inline.to.replace.global=true 
     </value> 
    </property> 
</bean> 

回答

1

如果我的理解是正确的,那么您试图在服务器上热部署速度模板(vm扩展名)。 如果我们参考documentation,你应该选择“面向文件”的属性。

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> 
<property name="velocityProperties"> 
     <value> 
      resource.loader=file    
      file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader 
      velocimacro.library.autoreload=true 
      file.resource.loader.cache=true 
      file.resource.loader.path=/WEB-INF/views 
      velocimacro.permissions.allow.inline.to.replace.global=true 
      file.resource.loader.modificationCheckInterval=2 
     </value> 
    </property> 
</bean> 

确保您的视图路径正确(file.resource.loader.path)。我添加了file.resource.loader.modificationCheckInterval,因为在缓存模板时,应该给出两次检查之间的秒数。

+0

以这种方式,我无法加载模板文件,因为它无法找到根路径。因为我使用velocity来渲染一些html部分,只是不把速度视为渲染引擎。所以我发布我的这里的方式... – cleverpig 2015-01-08 03:36:40

0

你可以通过覆盖速度的FileResourceLoader类的FileResourceLoader,该类将Concat的WEB_APP_ROOT路径和路径项的速度CONFIGRATION:

public class WebAppFileResourceLoader extends FileResourceLoader { 
private Logger log= LoggerFactory.getLogger(WebAppFileResourceLoader.class); 

@Override 
public void init(ExtendedProperties configuration) { 
    String webAppRootPath= (String) this.rsvc.getApplicationAttribute(WEB_APP_ROOT); 
    if (webAppRootPath!=null) { 
     Vector<String> pathsVector = configuration.getVector("path"); 
     Vector<String> tempVector = new Vector<String>(pathsVector.size()); 
     for (String path : pathsVector) { 
      log.debug("path:{}", path); 
      tempVector.add(webAppRootPath + File.separator + path); 
     } 
     configuration.clearProperty("path"); 
     for (String path : tempVector) { 
      configuration.addProperty("path", path); 
     } 
    } 
    super.init(configuration); 
} 

如何获得WEB_APP_ROOT变量,我们可以把它当极速引擎INIT:

public class SpringVelocityEngineFactoryBean extends VelocityEngineFactory 
    implements FactoryBean<VelocityEngine>, InitializingBean, ResourceLoaderAware,ServletContextAware { 

private VelocityEngine velocityEngine; 
private ServletContext servletContext; 
private Logger log= LoggerFactory.getLogger(SpringVelocityEngineFactoryBean.class); 

@Override 
protected VelocityEngine newVelocityEngine() throws IOException, VelocityException { 
//inject servletContext to velocity runtime's applicationAttribute 
    VelocityEngine velocityEngine= new VelocityEngine(); 
velocityEngine.setApplicationAttribute(ServletContext.class.getName(), this.servletContext); 
    log.debug("webappRoot:{}",this.servletContext.getRealPath("")); 
    velocityEngine.setApplicationAttribute(WEB_APP_ROOT,this.servletContext.getRealPath("")); 
return velocityEngine; 
} 
.... 
相关问题