org.springframework.web.servlet.ViewResolver Java Examples

The following examples show how to use org.springframework.web.servlet.ViewResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: spring-analysis-note   Author: Vip-Augus   File: ContentNegotiatingViewResolverTests.java    License: MIT License 6 votes vote down vote up
@Test
public void resolveViewNameWithPathExtension() throws Exception {
	request.setRequestURI("/test.xls");

	ViewResolver viewResolverMock = mock(ViewResolver.class);
	viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
	viewResolver.afterPropertiesSet();

	View viewMock = mock(View.class, "application_xls");

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
	given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
	given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock, result);
}
 
Example #2
Source Project: catnap   Author: gregwhitaker   File: WidgetConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    List<View> defaultViews = new ArrayList<>(2);
    defaultViews.add(jsonCatnapSpringView());
    defaultViews.add(jsonpCatnapSpringView());

    List<CatnapWrappingView> catnapViews = new ArrayList<>(2);
    catnapViews.add(jsonCatnapSpringView());
    catnapViews.add(jsonpCatnapSpringView());

    CatnapViewResolver catnapViewResolver = new CatnapViewResolver();
    catnapViewResolver.setViews(catnapViews);

    List<ViewResolver> viewResolvers = new ArrayList<>(1);
    viewResolvers.add(catnapViewResolver);

    ContentNegotiatingViewResolver cnvr = new ContentNegotiatingViewResolver();
    cnvr.setContentNegotiationManager(mvcContentNegotiationManager());
    cnvr.setOrder(1);
    cnvr.setDefaultViews(defaultViews);
    cnvr.setViewResolvers(viewResolvers);

    return cnvr;
}
 
Example #3
Source Project: spring-analysis-note   Author: Vip-Augus   File: ContentNegotiatingViewResolverTests.java    License: MIT License 6 votes vote down vote up
@Test
public void resolveViewNameAcceptHeader() throws Exception {
	request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

	ViewResolver viewResolverMock1 = mock(ViewResolver.class);
	ViewResolver viewResolverMock2 = mock(ViewResolver.class);
	viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

	viewResolver.afterPropertiesSet();

	View viewMock1 = mock(View.class, "application_xml");
	View viewMock2 = mock(View.class, "text_html");

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
	given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
	given(viewMock1.getContentType()).willReturn("application/xml");
	given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock2, result);
}
 
Example #4
Source Project: activiti6-boot2   Author: dingziyang   File: DispatcherServletConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public ViewResolver contentNegotiatingViewResolver() {
    log.debug("Configuring the ContentNegotiatingViewResolver");
    ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
    List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();

    UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
    urlBasedViewResolver.setViewClass(JstlView.class);
    urlBasedViewResolver.setPrefix("/WEB-INF/pages/");
    urlBasedViewResolver.setSuffix(".jsp");
    viewResolvers.add(urlBasedViewResolver);

    viewResolver.setViewResolvers(viewResolvers);

    List<View> defaultViews = new ArrayList<View>();
    defaultViews.add(new MappingJackson2JsonView());
    viewResolver.setDefaultViews(defaultViews);

    return viewResolver;
}
 
Example #5
Source Project: catnap   Author: gregwhitaker   File: DelegatingCatnapWebMvcConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    List<View> defaultViews = new ArrayList<>(2);
    defaultViews.add(jsonCatnapSpringView());
    defaultViews.add(jsonpCatnapSpringView());

    List<CatnapWrappingView> catnapViews = new ArrayList<>(2);
    catnapViews.add(jsonCatnapSpringView());
    catnapViews.add(jsonpCatnapSpringView());

    CatnapViewResolver catnapViewResolver = new CatnapViewResolver();
    catnapViewResolver.setViews(catnapViews);

    List<ViewResolver> viewResolvers = new ArrayList<>(1);
    viewResolvers.add(catnapViewResolver);

    ContentNegotiatingViewResolver cnvr = new ContentNegotiatingViewResolver();
    cnvr.setContentNegotiationManager(mvcContentNegotiationManager());
    cnvr.setOrder(1);
    cnvr.setDefaultViews(defaultViews);
    cnvr.setViewResolvers(viewResolvers);

    return cnvr;
}
 
Example #6
Source Project: spring4-understanding   Author: langtianya   File: ContentNegotiatingViewResolverTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void resolveViewNameWithRequestParameter() throws Exception {
	request.addParameter("format", "xls");

	Map<String, MediaType> mapping = Collections.singletonMap("xls", MediaType.valueOf("application/vnd.ms-excel"));
	ParameterContentNegotiationStrategy paramStrategy = new ParameterContentNegotiationStrategy(mapping);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(paramStrategy));

	ViewResolver viewResolverMock = mock(ViewResolver.class);
	viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
	viewResolver.afterPropertiesSet();

	View viewMock = mock(View.class, "application_xls");

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
	given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
	given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock, result);
}
 
Example #7
Source Project: java-technology-stack   Author: codeEngraver   File: ContentNegotiatingViewResolver.java    License: MIT License 6 votes vote down vote up
@Override
protected void initServletContext(ServletContext servletContext) {
	Collection<ViewResolver> matchingBeans =
			BeanFactoryUtils.beansOfTypeIncludingAncestors(obtainApplicationContext(), ViewResolver.class).values();
	if (this.viewResolvers == null) {
		this.viewResolvers = new ArrayList<>(matchingBeans.size());
		for (ViewResolver viewResolver : matchingBeans) {
			if (this != viewResolver) {
				this.viewResolvers.add(viewResolver);
			}
		}
	}
	else {
		for (int i = 0; i < this.viewResolvers.size(); i++) {
			ViewResolver vr = this.viewResolvers.get(i);
			if (matchingBeans.contains(vr)) {
				continue;
			}
			String name = vr.getClass().getName() + i;
			obtainApplicationContext().getAutowireCapableBeanFactory().initializeBean(vr, name);
		}

	}
	AnnotationAwareOrderComparator.sort(this.viewResolvers);
	this.cnmFactoryBean.setServletContext(servletContext);
}
 
Example #8
Source Project: java-technology-stack   Author: codeEngraver   File: WebMvcConfigurationSupport.java    License: MIT License 6 votes vote down vote up
/**
 * 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(
			mvcContentNegotiationManager(), this.applicationContext);
	configureViewResolvers(registry);

	if (registry.getViewResolvers().isEmpty() && this.applicationContext != null) {
		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());
	if (this.applicationContext != null) {
		composite.setApplicationContext(this.applicationContext);
	}
	if (this.servletContext != null) {
		composite.setServletContext(this.servletContext);
	}
	return composite;
}
 
Example #9
Source Project: java-technology-stack   Author: codeEngraver   File: ContentNegotiatingViewResolverTests.java    License: MIT License 6 votes vote down vote up
@Test
public void resolveViewNameWithAcceptHeader() throws Exception {
	request.addHeader("Accept", "application/vnd.ms-excel");

	Map<String, MediaType> mapping = Collections.singletonMap("xls", MediaType.valueOf("application/vnd.ms-excel"));
	MappingMediaTypeFileExtensionResolver extensionsResolver = new MappingMediaTypeFileExtensionResolver(mapping);
	ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy());
	manager.addFileExtensionResolvers(extensionsResolver);
	viewResolver.setContentNegotiationManager(manager);

	ViewResolver viewResolverMock = mock(ViewResolver.class);
	viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

	View viewMock = mock(View.class, "application_xls");

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
	given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
	given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock, result);
}
 
Example #10
Source Project: spring4-understanding   Author: langtianya   File: ContentNegotiatingViewResolverTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void resolveViewContentTypeNull() throws Exception {
	request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

	ViewResolver viewResolverMock = mock(ViewResolver.class);
	viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

	viewResolver.afterPropertiesSet();

	View viewMock = mock(View.class, "application_xml");

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
	given(viewMock.getContentType()).willReturn(null);

	View result = viewResolver.resolveViewName(viewName, locale);
	assertNull("Invalid view", result);
}
 
Example #11
Source Project: spring4-understanding   Author: langtianya   File: MvcNamespaceTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testContentNegotiationManager() throws Exception {
	loadBeanDefinitions("mvc-config-content-negotiation-manager.xml", 15);

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	ContentNegotiationManager manager = mapping.getContentNegotiationManager();

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml");
	NativeWebRequest webRequest = new ServletWebRequest(request);
	assertEquals(Arrays.asList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest));

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());

	ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
	assertEquals(ContentNegotiatingViewResolver.class, resolver.getClass());
	ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
	assertSame(manager, cnvr.getContentNegotiationManager());
}
 
Example #12
Source Project: java-technology-stack   Author: codeEngraver   File: ContentNegotiatingViewResolverTests.java    License: MIT License 6 votes vote down vote up
@Test
public void resolveViewNoMatch() throws Exception {
	request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9");

	ViewResolver viewResolverMock = mock(ViewResolver.class);
	viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

	viewResolver.afterPropertiesSet();

	View viewMock = mock(View.class, "application_xml");

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
	given(viewMock.getContentType()).willReturn("application/pdf");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertNull("Invalid view", result);
}
 
Example #13
Source Project: java-technology-stack   Author: codeEngraver   File: MvcNamespaceTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testViewResolutionWithContentNegotiation() throws Exception {
	loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml");

	ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
	assertNotNull(compositeResolver);
	assertEquals(1, compositeResolver.getViewResolvers().size());
	assertEquals(Ordered.HIGHEST_PRECEDENCE, compositeResolver.getOrder());

	List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
	assertEquals(ContentNegotiatingViewResolver.class, resolvers.get(0).getClass());
	ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolvers.get(0);
	assertEquals(6, cnvr.getViewResolvers().size());
	assertEquals(1, cnvr.getDefaultViews().size());
	assertTrue(cnvr.isUseNotAcceptableStatusCode());

	String beanName = "contentNegotiationManager";
	DirectFieldAccessor accessor = new DirectFieldAccessor(cnvr);
	ContentNegotiationManager manager = (ContentNegotiationManager) accessor.getPropertyValue(beanName);
	assertNotNull(manager);
	assertSame(manager, this.appContext.getBean(ContentNegotiationManager.class));
	assertSame(manager, this.appContext.getBean("mvcContentNegotiationManager"));
}
 
Example #14
Source Project: spring-comparing-template-engines   Author: jreijn   File: WebMvcConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver chunkViewResolver() {
	InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
	viewResolver.setViewClass(ChunkTemplateView.class);
	viewResolver.setPrefix("/WEB-INF/chunk/");
	viewResolver.setViewNames("*-chunk");
	viewResolver.setSuffix(".chtml");
	viewResolver.setCache(false);
	viewResolver.setRequestContextAttribute("rc");
	viewResolver.setContentType("text/html;charset=UTF-8");

	return viewResolver;
}
 
Example #15
Source Project: tutorials   Author: eugenp   File: TestConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
    bean.setCache(true);
    bean.setPrefix("/WEB-INF/views/");
    bean.setLayoutUrl("/WEB-INF/layouts/layout.vm");
    bean.setSuffix(".vm");
    return bean;
}
 
Example #16
Source Project: spring-analysis-note   Author: Vip-Augus   File: ViewResolverComposite.java    License: MIT License 5 votes vote down vote up
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	for (ViewResolver viewResolver : this.viewResolvers) {
		if (viewResolver instanceof ApplicationContextAware) {
			((ApplicationContextAware)viewResolver).setApplicationContext(applicationContext);
		}
	}
}
 
Example #17
Source Project: spring-soy-view   Author: matiwinnetou   File: SpringSoyViewBaseConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver soyViewResolver(final CompiledTemplatesHolder compiledTemplatesHolder,
                                    final ModelAdjuster modelAdjuster,
                                    final TemplateRenderer templateRenderer,
                                    final LocaleProvider localeProvider,
                                    final GlobalRuntimeModelResolver globalRuntimeModelResolver,
                                    final ContentNegotiator contentNegotiator,
                                    final SoyMsgBundleResolver msgBundleResolver)
                                 throws Exception {
    final SoyTemplateViewResolver soyTemplateViewResolver = new SoyTemplateViewResolver();
    soyTemplateViewResolver.setSoyMsgBundleResolver(msgBundleResolver);
    soyTemplateViewResolver.setCompiledTemplatesHolder(compiledTemplatesHolder);
    soyTemplateViewResolver.setEncoding(encoding);
    soyTemplateViewResolver.setGlobalRuntimeModelResolver(globalRuntimeModelResolver);
    soyTemplateViewResolver.setHotReloadMode(hotReloadMode);
    soyTemplateViewResolver.setIndexView(indexView);
    soyTemplateViewResolver.setLocaleProvider(localeProvider);
    soyTemplateViewResolver.setModelAdjuster(modelAdjuster);
    soyTemplateViewResolver.setTemplateRenderer(templateRenderer);
    soyTemplateViewResolver.setPrefix(logicalPrefix);
    soyTemplateViewResolver.setOrder(order);
    soyTemplateViewResolver.setRedirectContextRelative(true);
    soyTemplateViewResolver.setRedirectHttp10Compatible(true);
    soyTemplateViewResolver.setContentNegotiator(contentNegotiator);

    return soyTemplateViewResolver;
}
 
Example #18
Source Project: spring-analysis-note   Author: Vip-Augus   File: ViewResolverComposite.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public View resolveViewName(String viewName, Locale locale) throws Exception {
	for (ViewResolver viewResolver : this.viewResolvers) {
		View view = viewResolver.resolveViewName(viewName, locale);
		if (view != null) {
			return view;
		}
	}
	return null;
}
 
Example #19
Source Project: Spring   Author: iooifun   File: WebConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    resolver.setViewClass(JstlView.class);
    return resolver;
}
 
Example #20
Source Project: lams   Author: lamsfoundation   File: ContentNegotiatingViewResolver.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void initServletContext(ServletContext servletContext) {
	Collection<ViewResolver> matchingBeans =
			BeanFactoryUtils.beansOfTypeIncludingAncestors(getApplicationContext(), ViewResolver.class).values();
	if (this.viewResolvers == null) {
		this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.size());
		for (ViewResolver viewResolver : matchingBeans) {
			if (this != viewResolver) {
				this.viewResolvers.add(viewResolver);
			}
		}
	}
	else {
		for (int i = 0; i < viewResolvers.size(); i++) {
			if (matchingBeans.contains(viewResolvers.get(i))) {
				continue;
			}
			String name = viewResolvers.get(i).getClass().getName() + i;
			getApplicationContext().getAutowireCapableBeanFactory().initializeBean(viewResolvers.get(i), name);
		}

	}
	if (this.viewResolvers.isEmpty()) {
		logger.warn("Did not find any ViewResolvers to delegate to; please configure them using the " +
				"'viewResolvers' property on the ContentNegotiatingViewResolver");
	}
	AnnotationAwareOrderComparator.sort(this.viewResolvers);
	this.cnmFactoryBean.setServletContext(servletContext);
}
 
Example #21
Source Project: PedestrianDetectionSystem   Author: zhangpengpengpeng   File: WebConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(name = "internalResourceViewResolver")
public ViewResolver initViewResolver(){
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/jsp/");
    viewResolver.setSuffix(".jsp");

    return viewResolver;
}
 
Example #22
Source Project: Spring   Author: iooifun   File: WebConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    resolver.setViewClass(JstlView.class);
    return resolver;
}
 
Example #23
Source Project: lams   Author: lamsfoundation   File: ViewResolverComposite.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
	for (ViewResolver viewResolver : this.viewResolvers) {
		View view = viewResolver.resolveViewName(viewName, locale);
		if (view != null) {
			return view;
		}
	}
	return null;
}
 
Example #24
Source Project: tutorials   Author: eugenp   File: MvcConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver bean = new InternalResourceViewResolver();

    bean.setViewClass(JstlView.class);
    bean.setPrefix("/WEB-INF/view/");
    bean.setSuffix(".jsp");

    return bean;
}
 
Example #25
Source Project: tutorials   Author: eugenp   File: MvcConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver bean = new InternalResourceViewResolver();
    bean.setViewClass(JstlView.class);
    bean.setPrefix("/WEB-INF/view/");
    bean.setSuffix(".jsp");
    return bean;
}
 
Example #26
Source Project: thymeleafexamples-layouts   Author: thymeleaf   File: WebMvcConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
    thymeleafViewResolver.setTemplateEngine(templateEngine());
    thymeleafViewResolver.setCharacterEncoding("UTF-8");
    return thymeleafViewResolver;
}
 
Example #27
Source Project: spring-comparing-template-engines   Author: jreijn   File: WebMvcConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver htmlFlowViewResolver() {
	HtmlFlowViewResolver viewResolver = new HtmlFlowViewResolver();
	viewResolver.setViewNames("*-htmlFlow");
	viewResolver.setCache(false);

	return viewResolver;
}
 
Example #28
Source Project: spring4-understanding   Author: langtianya   File: DispatcherPortlet.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the ViewResolvers used by this class.
 * <p>If no ViewResolver beans are defined in the BeanFactory
 * for this namespace, we default to InternalResourceViewResolver.
 */
private void initViewResolvers(ApplicationContext context) {
	this.viewResolvers = null;

	if (this.detectAllViewResolvers) {
		// Find all ViewResolvers in the ApplicationContext, including ancestor contexts.
		Map<String, ViewResolver> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
				context, ViewResolver.class, true, false);
		if (!matchingBeans.isEmpty()) {
			this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.values());
			// We keep ViewResolvers in sorted order.
			AnnotationAwareOrderComparator.sort(this.viewResolvers);
		}
	}
	else {
		try {
			ViewResolver vr = context.getBean(VIEW_RESOLVER_BEAN_NAME, ViewResolver.class);
			this.viewResolvers = Collections.singletonList(vr);
		}
		catch (NoSuchBeanDefinitionException ex) {
			// Ignore, we'll add a default ViewResolver later.
		}
	}

	// Ensure we have at least one ViewResolver, by registering
	// a default ViewResolver if no other resolvers are found.
	if (this.viewResolvers == null) {
		this.viewResolvers = getDefaultStrategies(context, ViewResolver.class);
		if (logger.isDebugEnabled()) {
			logger.debug("No ViewResolvers found in portlet '" + getPortletName() + "': using default");
		}
	}
}
 
Example #29
Source Project: tutorials   Author: eugenp   File: WebConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver bean = new InternalResourceViewResolver();
    bean.setViewClass(JstlView.class);
    bean.setPrefix("/WEB-INF/view/");
    bean.setSuffix(".jsp");
    bean.setOrder(0);
    return bean;
}
 
Example #30
Source Project: spring-comparing-template-engines   Author: jreijn   File: WebMvcConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver rockerViewResolver() {
	RockerViewResolver viewResolver = new RockerViewResolver();
	viewResolver.setViewNames("*-rocker");
	viewResolver.setCache(false);

	return viewResolver;
}