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 File: ContentNegotiatingViewResolverTests.java    From spring4-understanding with 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 #2
Source File: DispatcherServletConfiguration.java    From activiti6-boot2 with 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 #3
Source File: ContentNegotiatingViewResolver.java    From java-technology-stack with 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 #4
Source File: WebMvcConfigurationSupport.java    From java-technology-stack with 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 #5
Source File: ContentNegotiatingViewResolverTests.java    From spring-analysis-note with 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 #6
Source File: WidgetConfiguration.java    From catnap with 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 #7
Source File: ContentNegotiatingViewResolverTests.java    From java-technology-stack with 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 #8
Source File: ContentNegotiatingViewResolverTests.java    From spring-analysis-note with 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 #9
Source File: ContentNegotiatingViewResolverTests.java    From spring4-understanding with 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 #10
Source File: MvcNamespaceTests.java    From spring4-understanding with 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 #11
Source File: DelegatingCatnapWebMvcConfiguration.java    From catnap with 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 #12
Source File: ContentNegotiatingViewResolverTests.java    From java-technology-stack with 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 File: MvcNamespaceTests.java    From java-technology-stack with 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 File: ScooldServer.java    From scoold with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
	VelocityViewResolver viewr = new VelocityViewResolver();
	viewr.setRedirectHttp10Compatible(false);
	viewr.setSuffix(".vm");
	viewr.setOrder(Ordered.HIGHEST_PRECEDENCE);
	return viewr;
}
 
Example #15
Source File: MVCConf.java    From codenjoy with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ViewResolver internalResourceViewResolver() {
    return new InternalResourceViewResolver(){{
        setPrefix("/view/");
        setSuffix(".jsp");
        setRedirectHttp10Compatible(false);
    }};
}
 
Example #16
Source File: ContentNegotiatingViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveViewNameAcceptHeaderDefaultView() throws Exception {
	request.addHeader("Accept", "application/json");

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

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

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

	viewResolver.afterPropertiesSet();

	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");
	given(viewMock3.getContentType()).willReturn("application/json");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock3, result);
}
 
Example #17
Source File: WebConfig.java    From pizzeria with MIT License 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    return resolver;
}
 
Example #18
Source File: ContentNegotiatingViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveViewNameFilenameDefaultView() throws Exception {
	request.setRequestURI("/test.json");

	Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
	PathExtensionContentNegotiationStrategy pathStrategy = new PathExtensionContentNegotiationStrategy(mapping);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(pathStrategy));

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

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

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

	viewResolver.afterPropertiesSet();

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

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

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock3, result);
}
 
Example #19
Source File: ApplicationConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver viewResolver(ContentNegotiationManager cnManager) {
    ContentNegotiatingViewResolver cnvResolver = new ContentNegotiatingViewResolver();
    cnvResolver.setContentNegotiationManager(cnManager);
    List<ViewResolver> resolvers = new ArrayList<>();

    InternalResourceViewResolver bean = new InternalResourceViewResolver("/WEB-INF/views/",".jsp");
    ArticleRssFeedViewResolver articleRssFeedViewResolver = new ArticleRssFeedViewResolver();

    resolvers.add(bean);
    resolvers.add(articleRssFeedViewResolver);

    cnvResolver.setViewResolvers(resolvers);
    return cnvResolver;
}
 
Example #20
Source File: UiEnvSpringModuleConfig.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a JSP view resolver.
 *
 * @return the JSP view resolver.
 */
@Bean
public ViewResolver jspViewResolver()
{
    // The JSP view resolver which means the "view" in our UI controller's "ModelAndView" return object will map to a JSP file
    // (e.g. a view name of "test" would map to "test.jsp" in the "/WEB-INF/jsp" directory.
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    return resolver;
}
 
Example #21
Source File: MvcConfig.java    From tutorials with 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 #22
Source File: WebMvcConfig.java    From spring-comparing-template-engines with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver ickenhamViewResolver() {
	IckenhamViewResolver viewResolver = new IckenhamViewResolver();
	viewResolver.setPrefix("/WEB-INF/ickenham/");
	viewResolver.setSuffix(".hbs");
	viewResolver.setViewNames("*-ickenham");
	viewResolver.setRequestContextAttribute("rc");
	viewResolver.setCache(false);
	viewResolver.setContentType("text/html;charset=UTF-8");

	return viewResolver;
}
 
Example #23
Source File: WebConfig.java    From Books-Management-System with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}
 
Example #24
Source File: WebConfig.java    From Project with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
	InternalResourceViewResolver resolver = new InternalResourceViewResolver();
	resolver.setPrefix("/WEB-INF/views/");
	resolver.setSuffix(".jsp");
	return resolver;
}
 
Example #25
Source File: ViewResolverComposite.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Set the list of view viewResolvers to delegate to.
 */
public void setViewResolvers(List<ViewResolver> viewResolvers) {
	this.viewResolvers.clear();
	if (!CollectionUtils.isEmpty(viewResolvers)) {
		this.viewResolvers.addAll(viewResolvers);
	}
}
 
Example #26
Source File: ContentNegotiatingViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveViewNameAcceptHeaderDefaultView() throws Exception {
	request.addHeader("Accept", "application/json");

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

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

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

	viewResolver.afterPropertiesSet();

	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");
	given(viewMock3.getContentType()).willReturn("application/json");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock3, result);
}
 
Example #27
Source File: WebMVCConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ViewResolver javascriptViewResolver() {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(templateEngine(javascriptTemplateResolver()));
    resolver.setContentType("application/javascript");
    resolver.setCharacterEncoding("UTF-8");
    resolver.setViewNames(ArrayUtil.array("*.js"));
    return resolver;
}
 
Example #28
Source File: WebConfig.java    From Project with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
	InternalResourceViewResolver resolver = new InternalResourceViewResolver();
	resolver.setPrefix("/WEB-INF/views/");
	resolver.setSuffix(".jsp");
	return resolver;
}
 
Example #29
Source File: WebMvcConfig.java    From spring-comparing-template-engines with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver httlViewResolver() {
	HttlViewResolver viewResolver = new HttlViewResolver();
	viewResolver.setContentType("text/html;charset=UTF-8");
	viewResolver.setViewNames("*-httl");
	viewResolver.setCache(false);

	return viewResolver;
}
 
Example #30
Source File: MultiViewResolver.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 获得属性后加工为方便查询的map缓存起来,
 *
 * @param viewResolverMap the view resolver map
 */
public void setViewResolverMap(Map<Set<String>, ViewResolver> viewResolverMap) {
    if (CollectionUtil.isNotEmpty(viewResolverMap)) {
        //将内容导入成一对一的Map,以便快速地查询
        this.viewResolverMap = new HashMap<String, ViewResolver>();
        for (Entry<Set<String>, ViewResolver> entry : viewResolverMap.entrySet()) {
            ViewResolver viewResolver = entry.getValue();
            Set<String> suffixs = entry.getKey();
            for (String suffix : suffixs) {
                this.viewResolverMap.put(suffix, viewResolver);
            }
        }
    }
}