/** * Register a script template view resolver with an empty default view name prefix and suffix. * <p><strong>Note</strong> that you must also configure script templating by * adding a {@link ScriptTemplateConfigurer} bean. * @since 5.0.4 */ public UrlBasedViewResolverRegistration scriptTemplate() { if (!checkBeanOfType(ScriptTemplateConfigurer.class)) { throw new BeanInitializationException("In addition to a script template view resolver " + "there must also be a single ScriptTemplateConfig bean in this web application context " + "(or its parent): ScriptTemplateConfigurer is the usual implementation. " + "This bean may be given any name."); } ScriptRegistration registration = new ScriptRegistration(); UrlBasedViewResolver resolver = registration.getViewResolver(); if (this.applicationContext != null) { resolver.setApplicationContext(this.applicationContext); } this.viewResolvers.add(resolver); return registration; }
/** * Register a {@code FreeMarkerViewResolver} with a ".ftl" suffix. * <p><strong>Note</strong> that you must also configure FreeMarker by * adding a {@link FreeMarkerConfigurer} bean. */ public UrlBasedViewResolverRegistration freeMarker() { if (!checkBeanOfType(FreeMarkerConfigurer.class)) { throw new BeanInitializationException("In addition to a FreeMarker view resolver " + "there must also be a single FreeMarkerConfig bean in this web application context " + "(or its parent): FreeMarkerConfigurer is the usual implementation. " + "This bean may be given any name."); } FreeMarkerRegistration registration = new FreeMarkerRegistration(); UrlBasedViewResolver resolver = registration.getViewResolver(); if (this.applicationContext != null) { resolver.setApplicationContext(this.applicationContext); } this.viewResolvers.add(resolver); return registration; }
@Before public void setup() { StaticApplicationContext context = new StaticApplicationContext(); context.refresh(); this.resolver = new UrlBasedViewResolver(); this.resolver.setApplicationContext(context); }
@Test // SPR-15291 public void contentNegotiationWithRedirect() { HandlerResult handlerResult = new HandlerResult(new Object(), "redirect:/", on(Handler.class).annotNotPresent(ModelAttribute.class).resolveReturnType(String.class), this.bindingContext); UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); viewResolver.setApplicationContext(new StaticApplicationContext()); ViewResolutionResultHandler resultHandler = resultHandler(viewResolver); MockServerWebExchange exchange = MockServerWebExchange.from(get("/account").accept(APPLICATION_JSON)); resultHandler.handleResult(exchange, handlerResult).block(Duration.ZERO); MockServerHttpResponse response = exchange.getResponse(); assertEquals(303, response.getStatusCode().value()); assertEquals("/", response.getHeaders().getLocation().toString()); }
/** * Register a {@code FreeMarkerViewResolver} with a ".ftl" suffix. * <p><strong>Note</strong> that you must also configure FreeMarker by * adding a {@link FreeMarkerConfigurer} bean. */ public UrlBasedViewResolverRegistration freeMarker() { if (this.applicationContext != null && !hasBeanOfType(FreeMarkerConfigurer.class)) { throw new BeanInitializationException("In addition to a FreeMarker view resolver " + "there must also be a single FreeMarkerConfig bean in this web application context " + "(or its parent): FreeMarkerConfigurer is the usual implementation. " + "This bean may be given any name."); } FreeMarkerRegistration registration = new FreeMarkerRegistration(); UrlBasedViewResolver resolver = registration.getViewResolver(); resolver.setApplicationContext(this.applicationContext); this.viewResolvers.add(resolver); return registration; }