/** * 视图解析器 * velocity * 过期是由于velocity六年没有更新过了,spring官方推荐使用FreeMarker或者Thymeleaf * 后期考虑spring官方的Thymeleaf */ @Bean public ViewResolver viewResolver() { /* InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setContentType("text/html;charset=utf-8"); viewResolver.setPrefix("/static/"); viewResolver.setSuffix(".html"); return viewResolver;*/ VelocityViewResolver velocityViewResolver = new VelocityViewResolver(); velocityViewResolver.setCache(true); velocityViewResolver.setPrefix(""); velocityViewResolver.setSuffix(".html"); velocityViewResolver.setContentType("text/html;charset=utf-8"); //关于Toolbox路径问题:如何路径开始头没有/会自动加上/,所以你如果写classpath:之类的识别不了 velocityViewResolver.setToolboxConfigLocation("/WEB-INF/velocityToolBox.xml"); velocityViewResolver.setViewClass(VelocityToolboxView.class); return velocityViewResolver; }
@Bean VelocityViewResolver velocityViewResolver() { VelocityViewResolver resolver = new VelocityViewResolver(); resolver.setSuffix(this.environment.getProperty("suffix", ".vm")); resolver.setPrefix(this.environment.getProperty("prefix", "/public/")); // Needs to come before any fallback resolver (e.g. a // InternalResourceViewResolver) resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 20); Properties p = new Properties(); p.put(Velocity.FILE_RESOURCE_LOADER_PATH, "/public/"); p.put("input.encoding", "utf-8"); p.put("output.encoding", "utf-8"); resolver.setAttributes(p); resolver.setContentType("text/html;charset=utf-8"); return resolver; }
@Override public void applyToViewResolver(Object viewResolver) { super.applyToViewResolver(viewResolver); VelocityViewResolver resolver = (VelocityViewResolver) viewResolver; resolver.setToolboxConfigLocation(getToolboxConfigLocation()); resolver.setDateToolAttribute(getDateToolAttribute()); resolver.setNumberToolAttribute(getNumberToolAttribute()); }
@Test public void allowSessionOverride() { registerAndRefreshContext("spring.velocity.allow-session-override:true"); AbstractTemplateViewResolver viewResolver = this.context .getBean(VelocityViewResolver.class); assertThat(viewResolver).extracting("allowSessionOverride").containsExactly(true); }
private MockHttpServletResponse render(String viewName) throws Exception { VelocityViewResolver resolver = this.context.getBean(VelocityViewResolver.class); View view = resolver.resolveViewName(viewName, Locale.UK); assertThat(view).isNotNull(); HttpServletRequest request = new MockHttpServletRequest(); request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); MockHttpServletResponse response = new MockHttpServletResponse(); view.render(null, request, response); return response; }
@Test public void allowSessionOverride() { registerAndRefreshContext("spring.velocity.allow-session-override:true"); AbstractTemplateViewResolver viewResolver = this.context .getBean(VelocityViewResolver.class); assertThat((Boolean) ReflectionTestUtils.getField(viewResolver, "allowSessionOverride"), is(true)); }
private MockHttpServletResponse render(String viewName) throws Exception { VelocityViewResolver resolver = this.context.getBean(VelocityViewResolver.class); View view = resolver.resolveViewName(viewName, Locale.UK); assertThat(view, notNullValue()); HttpServletRequest request = new MockHttpServletRequest(); request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); MockHttpServletResponse response = new MockHttpServletResponse(); view.render(null, request, response); return response; }
/** * actual resolver for the html files * * @return */ @Bean public VelocityViewResolver velocityViewResolver() { VelocityViewResolver vvw = new VelocityViewResolver(); vvw.setPrefix( "" ); vvw.setSuffix( ".html" ); // TODO in production we might actuall enable caching vvw.setCache( false ); vvw.setExposeSpringMacroHelpers( true ); return vvw; }
@Bean VelocityViewResolver velocityViewResolver() { VelocityViewResolver resolver = new VelocityViewResolver(); resolver.setSuffix(this.environment.getProperty("suffix", ".vm")); resolver.setPrefix(this.environment.getProperty("prefix", "/templates/")); // Needs to come before any fallback resolver (e.g. a // InternalResourceViewResolver) resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 20); return resolver; }
@Bean public ViewResolver velocityViewResolver() { VelocityViewResolver viewResolver = new VelocityViewResolver(); viewResolver.setSuffix(".vm"); viewResolver.setExposeSpringMacroHelpers(true); viewResolver.setOrder(1); viewResolver.setContentType("text/html; charset=UTF-8"); return viewResolver; }
private VelocityRegistration() { super(new VelocityViewResolver()); getViewResolver().setSuffix(".vm"); }
@Test public void velocity() { this.registry.velocity().prefix("/").suffix(".vm").cache(true); VelocityViewResolver resolver = checkAndGetResolver(VelocityViewResolver.class); checkPropertyValues(resolver, "prefix", "/", "suffix", ".vm", "cacheLimit", 1024); }
@Test public void velocityDefaultValues() { this.registry.velocity(); VelocityViewResolver resolver = checkAndGetResolver(VelocityViewResolver.class); checkPropertyValues(resolver, "prefix", "", "suffix", ".vm"); }
@Test public void defaultConfiguration() { registerAndRefreshContext(); assertThat(this.context.getBean(VelocityViewResolver.class)).isNotNull(); assertThat(this.context.getBean(VelocityConfigurer.class)).isNotNull(); }
@Test public void disableCache() { registerAndRefreshContext("spring.velocity.cache:false"); assertThat(this.context.getBean(VelocityViewResolver.class).getCacheLimit()) .isEqualTo(0); }
@Test public void usesEmbeddedVelocityViewResolver() { registerAndRefreshContext("spring.velocity.toolbox:/toolbox.xml"); VelocityViewResolver resolver = this.context.getBean(VelocityViewResolver.class); assertThat(resolver).isInstanceOf(EmbeddedVelocityViewResolver.class); }
@Test public void defaultConfiguration() { registerAndRefreshContext(); assertThat(this.context.getBean(VelocityViewResolver.class), notNullValue()); assertThat(this.context.getBean(VelocityConfigurer.class), notNullValue()); }
@Test public void disableCache() { registerAndRefreshContext("spring.velocity.cache:false"); assertThat(this.context.getBean(VelocityViewResolver.class).getCacheLimit(), equalTo(0)); }
@Test public void usesEmbeddedVelocityViewResolver() { registerAndRefreshContext("spring.velocity.toolbox:/toolbox.xml"); VelocityViewResolver resolver = this.context.getBean(VelocityViewResolver.class); assertThat(resolver, instanceOf(EmbeddedVelocityViewResolver.class)); }
@Bean public VelocityViewResolver viewResolver() { VelocityViewResolver resolver = new VelocityViewResolver(); resolver.setSuffix(".html"); return resolver; }