@Before public void setUp() { this.translator = new DefaultRequestToViewNameTranslator(); this.request = new MockHttpServletRequest(); this.request.setContextPath(CONTEXT_PATH); }
/** * Translates the request URI of the incoming {@link HttpServletRequest} * into the view name based on the configured parameters. * @see org.springframework.web.util.UrlPathHelper#getLookupPathForRequest * @see #transformPath */ @Override public String getViewName(HttpServletRequest request) { String lookupPath = this.urlPathHelper.getLookupPathForRequest(request); return (this.prefix + transformPath(lookupPath) + this.suffix); }
private void assertViewName(String expectedViewName) { String actualViewName = this.translator.getViewName(this.request); assertNotNull(actualViewName); assertEquals("Did not get the expected viewName from the DefaultRequestToViewNameTranslator.getViewName(..)", expectedViewName, actualViewName); }
@Test public void testGetViewNameWithPrefix() { final String prefix = "fiona_"; request.setRequestURI(CONTEXT_PATH + VIEW_NAME); this.translator.setPrefix(prefix); assertViewName(prefix + VIEW_NAME); }
@Test public void testGetViewNameLeavesExtensionIfSoConfigured() { request.setRequestURI(CONTEXT_PATH + "/" + VIEW_NAME + EXTENSION); this.translator.setStripExtension(false); assertViewName(VIEW_NAME + EXTENSION); }
@Test public void testGetViewNameWithCustomSeparator() { request.setRequestURI(CONTEXT_PATH + VIEW_NAME + "/fiona" + EXTENSION); this.translator.setSeparator("_"); assertViewName(VIEW_NAME + "_fiona"); }
@Test public void testGetViewNameWithNullPrefix() { request.setRequestURI(CONTEXT_PATH + VIEW_NAME); this.translator.setPrefix(null); assertViewName(VIEW_NAME); }
private void registerMvcSingletons(StubWebApplicationContext wac) { StandaloneConfiguration config = new StandaloneConfiguration(); config.setApplicationContext(wac); ServletContext sc = wac.getServletContext(); wac.addBeans(this.controllers); wac.addBeans(this.controllerAdvice); RequestMappingHandlerMapping hm = config.getHandlerMapping(); if (sc != null) { hm.setServletContext(sc); } hm.setApplicationContext(wac); hm.afterPropertiesSet(); wac.addBean("requestMappingHandlerMapping", hm); RequestMappingHandlerAdapter ha = config.requestMappingHandlerAdapter(); if (sc != null) { ha.setServletContext(sc); } ha.setApplicationContext(wac); ha.afterPropertiesSet(); wac.addBean("requestMappingHandlerAdapter", ha); wac.addBean("handlerExceptionResolver", config.handlerExceptionResolver()); wac.addBeans(initViewResolvers(wac)); wac.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver); wac.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver()); wac.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator()); this.flashMapManager = new SessionFlashMapManager(); wac.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager); extendMvcSingletons(sc).forEach(wac::addBean); }
/** * Translates the request URI of the incoming {@link HttpServletRequest} * into the view name based on the configured parameters. * @see org.springframework.web.util.UrlPathHelper#getLookupPathForRequest * @see #transformPath */ @Override public String getViewName(HttpServletRequest request) { String lookupPath = this.urlPathHelper.getLookupPathForRequest(request); return (this.prefix + transformPath(lookupPath) + this.suffix); }
public ViewResolverHelper(ListableBeanFactory beanFactory) { Map<String, ViewResolver> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors( beanFactory, ViewResolver.class, true, false); if (!matchingBeans.isEmpty()) { this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.values()); Collections.sort(this.viewResolvers, new OrderComparator()); } try { this.viewNameTranslator = beanFactory.getBean( DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, RequestToViewNameTranslator.class); } catch (NoSuchBeanDefinitionException ex) { this.viewNameTranslator = new DefaultRequestToViewNameTranslator(); } }
/** * Translates the request URI of the incoming {@link HttpServletRequest} * into the view name based on the configured parameters. * @see org.springframework.web.util.UrlPathHelper#getLookupPathForRequest * @see #transformPath */ @Override public String getViewName(HttpServletRequest request) { String lookupPath = this.urlPathHelper.getLookupPathForRequest(request); return (this.prefix + transformPath(lookupPath) + this.suffix); }
private void registerMvcSingletons(MockWebApplicationContext cxt) { StandaloneConfiguration configuration = new StandaloneConfiguration(); RequestMappingHandlerMapping handlerMapping = configuration.requestMappingHandlerMapping(); handlerMapping.setServletContext(cxt.getServletContext()); handlerMapping.setApplicationContext(cxt); cxt.addBean("requestMappingHandlerMapping", handlerMapping); RequestMappingHandlerAdapter handlerAdapter = configuration.requestMappingHandlerAdapter(); handlerAdapter.setServletContext(cxt.getServletContext()); handlerAdapter.setApplicationContext(cxt); handlerAdapter.afterPropertiesSet(); cxt.addBean("requestMappingHandlerAdapter", handlerAdapter); cxt.addBean("handlerExceptionResolver", configuration.handlerExceptionResolver()); cxt.addBeans(initViewResolvers(cxt)); cxt.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver); cxt.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver()); cxt.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator()); initFlashMapManager(); cxt.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager); }
private void registerMvcSingletons(StubWebApplicationContext wac) { StandaloneConfiguration config = new StandaloneConfiguration(); config.setApplicationContext(wac); ServletContext sc = wac.getServletContext(); wac.addBeans(this.controllers); wac.addBeans(this.controllerAdvice); RequestMappingHandlerMapping hm = config.getHandlerMapping(); if (sc != null) { hm.setServletContext(sc); } hm.setApplicationContext(wac); hm.afterPropertiesSet(); wac.addBean("requestMappingHandlerMapping", hm); RequestMappingHandlerAdapter ha = config.requestMappingHandlerAdapter(); if (sc != null) { ha.setServletContext(sc); } ha.setApplicationContext(wac); ha.afterPropertiesSet(); wac.addBean("requestMappingHandlerAdapter", ha); wac.addBean("handlerExceptionResolver", config.handlerExceptionResolver()); wac.addBeans(initViewResolvers(wac)); wac.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver); wac.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver()); wac.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator()); this.flashMapManager = new SessionFlashMapManager(); wac.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager); }
private void registerMvcSingletons(StubWebApplicationContext wac) { StandaloneConfiguration config = new StandaloneConfiguration(); config.setApplicationContext(wac); wac.addBeans(this.controllerAdvice); StaticRequestMappingHandlerMapping hm = config.getHandlerMapping(); hm.setServletContext(wac.getServletContext()); hm.setApplicationContext(wac); hm.afterPropertiesSet(); hm.registerHandlers(this.controllers); wac.addBean("requestMappingHandlerMapping", hm); RequestMappingHandlerAdapter handlerAdapter = config.requestMappingHandlerAdapter(); handlerAdapter.setServletContext(wac.getServletContext()); handlerAdapter.setApplicationContext(wac); handlerAdapter.afterPropertiesSet(); wac.addBean("requestMappingHandlerAdapter", handlerAdapter); wac.addBean("handlerExceptionResolver", config.handlerExceptionResolver()); wac.addBeans(initViewResolvers(wac)); wac.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver); wac.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver()); wac.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator()); this.flashMapManager = new SessionFlashMapManager(); wac.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager); }