private ViewResolver getViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver() { @Override protected AbstractUrlBasedView buildView(String viewName) throws Exception { String suffix = getSuffix(); if (suffix != null && suffix.length() > 0 && viewName.endsWith(suffix)) { viewName = viewName.substring(0, viewName.length() - suffix.length()); } return super.buildView(viewName); } }; viewResolver.setPrefix("/"); viewResolver.setSuffix(""); return viewResolver; }
/** * Register a {@link ViewResolverComposite} that contains a chain of view resolvers * to use for view resolution. * By default this resolver is ordered at 0 unless content negotiation view * resolution is used in which case the order is raised to * {@link org.springframework.core.Ordered#HIGHEST_PRECEDENCE * Ordered.HIGHEST_PRECEDENCE}. * <p>If no other resolvers are configured, * {@link ViewResolverComposite#resolveViewName(String, Locale)} returns null in order * to allow other potential {@link ViewResolver} beans to resolve views. * @since 4.1 */ @Bean public ViewResolver mvcViewResolver() { ViewResolverRegistry registry = new ViewResolverRegistry(); registry.setContentNegotiationManager(mvcContentNegotiationManager()); registry.setApplicationContext(this.applicationContext); configureViewResolvers(registry); if (registry.getViewResolvers().isEmpty()) { String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this.applicationContext, ViewResolver.class, true, false); if (names.length == 1) { registry.getViewResolvers().add(new InternalResourceViewResolver()); } } ViewResolverComposite composite = new ViewResolverComposite(); composite.setOrder(registry.getOrder()); composite.setViewResolvers(registry.getViewResolvers()); composite.setApplicationContext(this.applicationContext); composite.setServletContext(this.servletContext); return composite; }
@Test public void emptyParameterListHandleMethod() throws Exception { @SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(EmptyParameterListHandlerMethodController.class)); RootBeanDefinition vrDef = new RootBeanDefinition(InternalResourceViewResolver.class); vrDef.getPropertyValues().add("suffix", ".jsp"); wac.registerBeanDefinition("viewResolver", vrDef); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/emptyParameterListHandler"); MockHttpServletResponse response = new MockHttpServletResponse(); EmptyParameterListHandlerMethodController.called = false; servlet.service(request, response); assertTrue(EmptyParameterListHandlerMethodController.called); assertEquals("", response.getContentAsString()); }
@Test public void emptyParameterListHandleMethod() throws Exception { initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { @Override public void initialize(GenericWebApplicationContext context) { RootBeanDefinition vrDef = new RootBeanDefinition(InternalResourceViewResolver.class); vrDef.getPropertyValues().add("suffix", ".jsp"); context.registerBeanDefinition("viewResolver", vrDef); } }, EmptyParameterListHandlerMethodController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/emptyParameterListHandler"); MockHttpServletResponse response = new MockHttpServletResponse(); EmptyParameterListHandlerMethodController.called = false; getServlet().service(request, response); assertTrue(EmptyParameterListHandlerMethodController.called); assertEquals("", response.getContentAsString()); }
@Test public void withNoViewAndSamePath() throws Exception { InternalResourceViewResolver vr = (InternalResourceViewResolver) complexDispatcherServlet .getWebApplicationContext().getBean("viewResolver2"); vr.setSuffix(""); MockServletContext servletContext = new MockServletContext(); MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/noview"); MockHttpServletResponse response = new MockHttpServletResponse(); try { complexDispatcherServlet.service(request, response); fail("Should have thrown ServletException"); } catch (ServletException ex) { ex.printStackTrace(); } }
@Bean public ViewResolver viewResolver() { ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver(); List<View> defaultViews = new ArrayList<View>(); defaultViews.add(new MappingJackson2JsonView()); resolver.setDefaultViews(defaultViews); resolver.setOrder(0); List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>(); InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); internalResourceViewResolver.setPrefix("/WEB-INF/view/"); internalResourceViewResolver.setSuffix(".jsp"); internalResourceViewResolver.setOrder(1); viewResolvers.add(internalResourceViewResolver); resolver.setViewResolvers(viewResolvers); return resolver; }
@Test public void deviceDelegatingThymeleafViewResolverEnabled() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.mobile.devicedelegatingviewresolver.enabled:true"); this.context.register(Config.class, WebMvcAutoConfiguration.class, ThymeleafAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DeviceDelegatingViewResolverConfiguration.class); this.context.refresh(); ThymeleafViewResolver thymeleafViewResolver = this.context .getBean(ThymeleafViewResolver.class); AbstractDeviceDelegatingViewResolver deviceDelegatingViewResolver = this.context .getBean("deviceDelegatingViewResolver", AbstractDeviceDelegatingViewResolver.class); assertThat(thymeleafViewResolver).isNotNull(); assertThat(deviceDelegatingViewResolver).isNotNull(); assertThat(deviceDelegatingViewResolver.getViewResolver()) .isInstanceOf(ThymeleafViewResolver.class); assertThat(this.context.getBean(InternalResourceViewResolver.class)).isNotNull(); assertThat(this.context.getBean(ThymeleafViewResolver.class)).isNotNull(); assertThat(deviceDelegatingViewResolver.getOrder()) .isEqualTo(thymeleafViewResolver.getOrder() - 1); }
@Test(expected = NoSuchBeanDefinitionException.class) public void deviceDelegatingThymeleafViewResolverDisabled() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.mobile.devicedelegatingviewresolver.enabled:false"); this.context.register(Config.class, WebMvcAutoConfiguration.class, ThymeleafAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DeviceDelegatingViewResolverConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(InternalResourceViewResolver.class)).isNotNull(); assertThat(this.context.getBean(ThymeleafViewResolver.class)).isNotNull(); this.context.getBean("deviceDelegatingViewResolver", AbstractDeviceDelegatingViewResolver.class); }
@Test(expected = NoSuchBeanDefinitionException.class) public void deviceDelegatingInternalResourceViewResolverDisabled() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.mobile.devicedelegatingviewresolver.enabled:false"); this.context.register(Config.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DeviceDelegatingViewResolverConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(InternalResourceViewResolver.class)); try { this.context.getBean(ThymeleafViewResolver.class); } catch (NoSuchBeanDefinitionException ex) { // expected. ThymeleafViewResolver shouldn't be defined. } this.context.getBean("deviceDelegatingViewResolver", AbstractDeviceDelegatingViewResolver.class); }
@Test public void deviceDelegatingThymeleafViewResolverEnabled() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.mobile.devicedelegatingviewresolver.enabled:true"); this.context.register(Config.class, WebMvcAutoConfiguration.class, ThymeleafAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DeviceDelegatingViewResolverConfiguration.class); this.context.refresh(); ThymeleafViewResolver thymeleafViewResolver = this.context .getBean(ThymeleafViewResolver.class); AbstractDeviceDelegatingViewResolver deviceDelegatingViewResolver = this.context .getBean("deviceDelegatingViewResolver", AbstractDeviceDelegatingViewResolver.class); assertNotNull(thymeleafViewResolver); assertNotNull(deviceDelegatingViewResolver); assertTrue(deviceDelegatingViewResolver .getViewResolver() instanceof ThymeleafViewResolver); assertNotNull(this.context.getBean(InternalResourceViewResolver.class)); assertNotNull(this.context.getBean(ThymeleafViewResolver.class)); assertTrue(deviceDelegatingViewResolver .getOrder() == thymeleafViewResolver.getOrder() - 1); }
@Test(expected = NoSuchBeanDefinitionException.class) public void deviceDelegatingThymeleafViewResolverDisabled() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.mobile.devicedelegatingviewresolver.enabled:false"); this.context.register(Config.class, WebMvcAutoConfiguration.class, ThymeleafAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DeviceDelegatingViewResolverConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(InternalResourceViewResolver.class)); assertNotNull(this.context.getBean(ThymeleafViewResolver.class)); this.context.getBean("deviceDelegatingViewResolver", AbstractDeviceDelegatingViewResolver.class); }
public void testWithNoViewAndSamePath() throws Exception { InternalResourceViewResolver vr = (InternalResourceViewResolver) complexDispatcherServlet .getWebApplicationContext().getBean("viewResolver2"); vr.setSuffix(""); MockServletContext servletContext = new MockServletContext(); MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/noview"); MockHttpServletResponse response = new MockHttpServletResponse(); try { complexDispatcherServlet.service(request, response); fail("Should have thrown ServletException"); } catch (ServletException ex) { ex.printStackTrace(); } }
@Override public void refresh() throws BeansException { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("commandClass", "org.springframework.tests.sample.beans.TestBean"); pvs.add("formView", "form"); registerSingleton("/form.do", SimpleFormController.class, pvs); registerSingleton("/locale.do", LocaleChecker.class); addMessage("test", Locale.ENGLISH, "test message"); addMessage("test", Locale.CANADA, "Canadian & test message"); addMessage("testArgs", Locale.ENGLISH, "test {0} message {1}"); addMessage("testArgsFormat", Locale.ENGLISH, "test {0} message {1,number,#.##} X"); registerSingleton(UiApplicationContextUtils.THEME_SOURCE_BEAN_NAME, DummyThemeSource.class); registerSingleton("handlerMapping", BeanNameUrlHandlerMapping.class); registerSingleton("viewResolver", InternalResourceViewResolver.class); pvs = new MutablePropertyValues(); pvs.add("location", "org/springframework/web/context/WEB-INF/sessionContext.xml"); registerSingleton("viewResolver2", XmlViewResolver.class, pvs); super.refresh(); }
@Bean public InternalResourceViewResolver jspViewResolver() { InternalResourceViewResolver viewResolverA = new InternalResourceViewResolver(); viewResolverA.setPrefix("/WEB-INF/jsp/"); viewResolverA.setSuffix(".jsp"); viewResolverA.setOrder(1); return viewResolverA; }
@Bean public InternalResourceViewResolver getViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/"); resolver.setSuffix(".html"); resolver.setOrder(3); return resolver; }
@Bean public InternalResourceViewResolver jspViewResolver() { InternalResourceViewResolver viewResolverA = new InternalResourceViewResolver(); viewResolverA.setPrefix("/WEB-INF/jsp/"); viewResolverA.setSuffix(".jsp"); viewResolverA.setOrder(3); return viewResolverA; }
@RequestMapping("/logout") public String logout(HttpSession session){ String currentUser = (String)session.getAttribute("currentUser"); System.out.println("用户[" + currentUser + "]准备登出"); SecurityUtils.getSubject().logout(); System.out.println("用户[" + currentUser + "]已登出"); return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/"; }
/** * Basic setup for JSP views. */ @Bean public InternalResourceViewResolver configureInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); return resolver; }
/** * Configure ViewResolvers to deliver preferred views. */ @Override public void configureViewResolvers(ViewResolverRegistry registry) { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); registry.viewResolver(viewResolver); }
@Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; }
@Bean public InternalResourceViewResolver setupViewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; }
@Bean public InternalResourceViewResolver setupViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/"); resolver.setSuffix(".jsp"); resolver.setContentType("text/html; charset-utf-8"); resolver.setViewClass(JstlView.class); return resolver; }
@Bean public ViewResolver getViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; }
@Override public void configureViewResolvers(ViewResolverRegistry registry) { super.configureViewResolvers(registry); InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/pages/"); viewResolver.setSuffix(".jsp"); registry.viewResolver(viewResolver); }