1 16 17 package org.springframework.web.servlet.view.velocity; 18 19 import java.io.IOException ; 20 21 import org.apache.velocity.app.VelocityEngine; 22 import org.apache.velocity.exception.VelocityException; 23 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; 24 25 import org.springframework.beans.factory.InitializingBean; 26 import org.springframework.context.ResourceLoaderAware; 27 import org.springframework.ui.velocity.VelocityEngineFactory; 28 29 69 public class VelocityConfigurer extends VelocityEngineFactory 70 implements VelocityConfig, InitializingBean, ResourceLoaderAware { 71 72 73 private static final String SPRING_MACRO_RESOURCE_LOADER_NAME = "springMacro"; 74 75 76 private static final String SPRING_MACRO_RESOURCE_LOADER_CLASS = "springMacro.resource.loader.class"; 77 78 79 private static final String SPRING_MACRO_LIBRARY = "org/springframework/web/servlet/view/velocity/spring.vm"; 80 81 82 private VelocityEngine velocityEngine; 83 84 91 public void setVelocityEngine(VelocityEngine velocityEngine) { 92 this.velocityEngine = velocityEngine; 93 } 94 95 101 public void afterPropertiesSet() throws IOException , VelocityException { 102 if (this.velocityEngine == null) { 103 this.velocityEngine = createVelocityEngine(); 104 } 105 } 106 107 112 protected void postProcessVelocityEngine(VelocityEngine velocityEngine) { 113 velocityEngine.setProperty( 114 SPRING_MACRO_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName()); 115 velocityEngine.addProperty( 116 VelocityEngine.RESOURCE_LOADER, SPRING_MACRO_RESOURCE_LOADER_NAME); 117 velocityEngine.addProperty( 118 VelocityEngine.VM_LIBRARY, SPRING_MACRO_LIBRARY); 119 if (logger.isInfoEnabled()) { 120 logger.info("ClasspathResourceLoader with name '" + SPRING_MACRO_RESOURCE_LOADER_NAME + 121 "' added to configured VelocityEngine"); 122 } 123 } 124 125 public VelocityEngine getVelocityEngine() { 126 return this.velocityEngine; 127 } 128 129 } 130 | Popular Tags |