/** * Map a view controller to the given URL path (or pattern) in order to redirect * to another URL. By default the redirect URL is expected to be relative to * the current ServletContext, i.e. as relative to the web application root. * @since 4.1 */ public RedirectViewControllerRegistration addRedirectViewController(String urlPath, String redirectUrl) { RedirectViewControllerRegistration registration = new RedirectViewControllerRegistration(urlPath, redirectUrl); registration.setApplicationContext(this.applicationContext); this.redirectRegistrations.add(registration); return registration; }
/** * Return the {@code HandlerMapping} that contains the registered view * controller mappings, or {@code null} for no registrations. * @since 4.3.12 */ @Nullable protected SimpleUrlHandlerMapping buildHandlerMapping() { if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) { return null; } Map<String, Object> urlMap = new LinkedHashMap<>(); for (ViewControllerRegistration registration : this.registrations) { urlMap.put(registration.getUrlPath(), registration.getViewController()); } for (RedirectViewControllerRegistration registration : this.redirectRegistrations) { urlMap.put(registration.getUrlPath(), registration.getViewController()); } SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); handlerMapping.setUrlMap(urlMap); handlerMapping.setOrder(this.order); return handlerMapping; }
@Test public void addRedirectViewControllerWithCustomSettings() throws Exception { this.registry.addRedirectViewController("/path", "/redirectTo") .setContextRelative(false) .setKeepQueryParams(true) .setStatusCode(HttpStatus.PERMANENT_REDIRECT); RedirectView redirectView = getRedirectView("/path"); this.request.setQueryString("a=b"); this.request.setContextPath("/context"); redirectView.render(Collections.emptyMap(), this.request, this.response); assertEquals(308, this.response.getStatus()); assertEquals("/redirectTo?a=b", response.getRedirectedUrl()); assertNotNull(redirectView.getApplicationContext()); }
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/path").setViewName("view"); registry.addRedirectViewController("/old", "/new").setStatusCode(HttpStatus.PERMANENT_REDIRECT); registry.addStatusController("/bad", HttpStatus.NOT_FOUND); }
/** * Map a view controller to the given URL path (or pattern) in order to redirect * to another URL. By default the redirect URL is expected to be relative to * the current ServletContext, i.e. as relative to the web application root. * @since 4.1 */ public RedirectViewControllerRegistration addRedirectViewController(String urlPath, String redirectUrl) { RedirectViewControllerRegistration registration = new RedirectViewControllerRegistration(urlPath, redirectUrl); registration.setApplicationContext(this.applicationContext); this.redirectRegistrations.add(registration); return registration; }
/** * Return the {@code HandlerMapping} that contains the registered view * controller mappings, or {@code null} for no registrations. * @since 4.3.12 */ @Nullable protected SimpleUrlHandlerMapping buildHandlerMapping() { if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) { return null; } Map<String, Object> urlMap = new LinkedHashMap<>(); for (ViewControllerRegistration registration : this.registrations) { urlMap.put(registration.getUrlPath(), registration.getViewController()); } for (RedirectViewControllerRegistration registration : this.redirectRegistrations) { urlMap.put(registration.getUrlPath(), registration.getViewController()); } SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); handlerMapping.setUrlMap(urlMap); handlerMapping.setOrder(this.order); return handlerMapping; }
/** * Map a view controller to the given URL path (or pattern) in order to redirect * to another URL. By default the redirect URL is expected to be relative to * the current ServletContext, i.e. as relative to the web application root. * @since 4.1 */ public RedirectViewControllerRegistration addRedirectViewController(String urlPath, String redirectUrl) { RedirectViewControllerRegistration registration = new RedirectViewControllerRegistration(urlPath, redirectUrl); registration.setApplicationContext(this.applicationContext); this.redirectRegistrations.add(registration); return registration; }
/** * Return the {@code HandlerMapping} that contains the registered view * controller mappings, or {@code null} for no registrations. * @since 4.3.12 */ protected SimpleUrlHandlerMapping buildHandlerMapping() { if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) { return null; } Map<String, Object> urlMap = new LinkedHashMap<String, Object>(); for (ViewControllerRegistration registration : this.registrations) { urlMap.put(registration.getUrlPath(), registration.getViewController()); } for (RedirectViewControllerRegistration registration : this.redirectRegistrations) { urlMap.put(registration.getUrlPath(), registration.getViewController()); } SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); handlerMapping.setUrlMap(urlMap); handlerMapping.setOrder(this.order); return handlerMapping; }