@Bean public VelocityConfigurer velocityConfig() { VelocityConfigurer cfg = new VelocityConfigurer(); cfg.setResourceLoaderPath("/WEB-INF/velocity/"); cfg.setPreferFileSystemAccess(true); // cfg.setConfigLocation(context.getResource("/WEB-INF/velocity.properties")); Properties velocityProperties = new Properties(); // velocityProperties.put("velocimacro.permissions.allow.inline", "true"); // velocityProperties.put("velocimacro.permissions.allow.inline.to.replace.global", "true"); // velocityProperties.put("velocimacro.permissions.allow.inline.local.scope", "true"); // velocityProperties.put("input.encoding", "UTF-8"); // velocityProperties.put("output.encoding", "UTF-8"); // velocityProperties.put("resource.loader", "webapp, class"); // velocityProperties.put("class.resource.loader.description", "Velocity Classpath Resource Loader"); // velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader "); // velocityProperties.put("webapp.resource.loader.class", "org.apache.velocity.tools.view.WebappResourceLoader"); // velocityProperties.put("webapp.resource.loader.path", "/WEB-INF/velocity/"); // velocityProperties.put("webapp.resource.loader.cache", "true"); cfg.setVelocityProperties(velocityProperties); return cfg; }
@Bean public VelocityConfig velocityConfig() throws IOException, VelocityException { Properties config = new Properties(); config.setProperty("input.encoding", "UTF-8"); config.setProperty("output.encoding", "UTF-8"); config.setProperty("default.contentType", "text/html;charset=UTF-8"); config.setProperty("resource.loader", "class"); config.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); VelocityConfigurer velocityConfigurer = new VelocityConfigurer(); velocityConfigurer.setVelocityProperties(config); velocityConfigurer.afterPropertiesSet(); return velocityConfigurer; }
@Bean @ConditionalOnMissingBean(VelocityConfig.class) public VelocityConfigurer velocityConfigurer() { VelocityConfigurer configurer = new VelocityConfigurer(); applyProperties(configurer); return configurer; }
/** * Register Velocity view resolver with an empty default view name * prefix and a default suffix of ".vm". * * <p><strong>Note</strong> that you must also configure Velocity by adding a * {@link org.springframework.web.servlet.view.velocity.VelocityConfigurer} bean. */ public UrlBasedViewResolverRegistration velocity() { if (this.applicationContext != null && !hasBeanOfType(VelocityConfigurer.class)) { throw new BeanInitializationException("In addition to a Velocity view resolver " + "there must also be a single VelocityConfig bean in this web application context " + "(or its parent): VelocityConfigurer is the usual implementation. " + "This bean may be given any name."); } VelocityRegistration registration = new VelocityRegistration(); this.viewResolvers.add(registration.getViewResolver()); return registration; }
@Before public void setUp() { StaticWebApplicationContext context = new StaticWebApplicationContext(); context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class); context.registerSingleton("velocityConfigurer", VelocityConfigurer.class); context.registerSingleton("tilesConfigurer", TilesConfigurer.class); context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class); context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class); this.registry = new ViewResolverRegistry(); this.registry.setApplicationContext(context); this.registry.setContentNegotiationManager(new ContentNegotiationManager()); }
@Test public void customVelocitySettings() { registerAndRefreshContext( "spring.velocity.properties.directive.parse.max.depth:10"); assertThat(this.context.getBean(VelocityConfigurer.class).getVelocityEngine() .getProperty("directive.parse.max.depth")).isEqualTo("10"); }
@Test public void renderTemplate() throws Exception { registerAndRefreshContext(); VelocityConfigurer velocity = this.context.getBean(VelocityConfigurer.class); StringWriter writer = new StringWriter(); Template template = velocity.getVelocityEngine().getTemplate("message.vm"); template.process(); VelocityContext velocityContext = new VelocityContext(); velocityContext.put("greeting", "Hello World"); template.merge(velocityContext, writer); assertThat(writer.toString()).contains("Hello World"); }
/** * 过期是由于velocity六年没有更新过了,spring官方推荐使用FreeMarker或者Thymeleaf * * @return */ @Bean(name = "velocityConfig") public VelocityConfigurer velocityConfigurer() { VelocityConfigurer configurer = new VelocityConfigurer(); configurer.setResourceLoaderPath("/WEB-INF/template/"); Properties properties = new Properties(); //解决乱码问题 properties.setProperty("input.encoding", "utf-8"); properties.setProperty("output.encoding", "utf-8"); configurer.setVelocityProperties(properties); return configurer; }
@Test public void customVelocitySettings() { registerAndRefreshContext( "spring.velocity.properties.directive.parse.max.depth:10"); assertThat( this.context.getBean(VelocityConfigurer.class).getVelocityEngine() .getProperty("directive.parse.max.depth"), equalTo((Object) "10")); }
@Test public void renderTemplate() throws Exception { registerAndRefreshContext(); VelocityConfigurer velocity = this.context.getBean(VelocityConfigurer.class); StringWriter writer = new StringWriter(); Template template = velocity.getVelocityEngine().getTemplate("message.vm"); template.process(); VelocityContext velocityContext = new VelocityContext(); velocityContext.put("greeting", "Hello World"); template.merge(velocityContext, writer); assertThat(writer.toString(), containsString("Hello World")); }
@Bean public VelocityConfig velocityConfig() { Properties p = new Properties(); p.put( "resource.loader", "webapp" ); p.put( "webapp.resource.loader.path", new File( webapp, "html" ).toString() ); p.put( "webapp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader" ); VelocityConfigurer vc = new VelocityConfigurer(); VelocityEngine engine = new VelocityEngine( p ); engine.init(); vc.setVelocityEngine( engine ); return vc; }
@Bean public VelocityEngine velocityEngine(VelocityConfigurer configurer) throws VelocityException, IOException { return configurer.getVelocityEngine(); }
@Bean public VelocityConfigurer velocityConfigurer() { VelocityConfigurer configurer = new VelocityConfigurer(); configurer.setResourceLoaderPath("/WEB-INF/"); return configurer; }
@Bean public VelocityConfigurer velocityConfigurer() { return new VelocityConfigurer(); }
@Test public void defaultConfiguration() { registerAndRefreshContext(); assertThat(this.context.getBean(VelocityViewResolver.class)).isNotNull(); assertThat(this.context.getBean(VelocityConfigurer.class)).isNotNull(); }
@Test public void customCharset() throws Exception { registerAndRefreshContext("spring.velocity.charset:ISO-8859-1"); assertThat(this.context.getBean(VelocityConfigurer.class).getVelocityEngine() .getProperty("input.encoding")).isEqualTo("ISO-8859-1"); }
@Test public void defaultConfiguration() { registerAndRefreshContext(); assertThat(this.context.getBean(VelocityViewResolver.class), notNullValue()); assertThat(this.context.getBean(VelocityConfigurer.class), notNullValue()); }
@Test public void customCharset() throws Exception { registerAndRefreshContext("spring.velocity.charset:ISO-8859-1"); assertThat(this.context.getBean(VelocityConfigurer.class).getVelocityEngine() .getProperty("input.encoding"), equalTo((Object) "ISO-8859-1")); }
@Bean VelocityConfigurer velocityConfig() { return new VelocityConfigurer(); }
@Bean public VelocityConfigurer velocityConfig() { VelocityConfigurer velocityConfigurer = new VelocityConfigurer(); velocityConfigurer.setResourceLoaderPath("/WEB-INF/html/"); return velocityConfigurer; }