本文整理匯總了Java中org.springframework.web.servlet.ViewResolver類的典型用法代碼示例。如果您正苦於以下問題:Java ViewResolver類的具體用法?Java ViewResolver怎麽用?Java ViewResolver使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ViewResolver類屬於org.springframework.web.servlet包,在下文中一共展示了ViewResolver類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: renderNonWebAppTemplate
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@Test
public void renderNonWebAppTemplate() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
assertThat(context.getBeanNamesForType(ViewResolver.class).length).isEqualTo(0);
try {
TemplateEngine engine = context.getBean(TemplateEngine.class);
Context attrs = new Context(Locale.UK,
Collections.singletonMap("greeting", "Hello World"));
String result = engine.process("message", attrs);
assertThat(result).contains("Hello World");
}
finally {
context.close();
}
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:18,代碼來源:ThymeleafAutoConfigurationTests.java
示例2: contentNegotiatingViewResolver
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@Bean
public ViewResolver contentNegotiatingViewResolver() {
logger.debug("Configuring the ContentNegotiatingViewResolver");
ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
urlBasedViewResolver.setViewClass(JstlView.class);
urlBasedViewResolver.setSuffix(".html");
viewResolvers.add(urlBasedViewResolver);
viewResolver.setViewResolvers(viewResolvers);
List<View> defaultViews = new ArrayList<View>();
defaultViews.add(new MappingJackson2JsonView());
viewResolver.setDefaultViews(defaultViews);
return viewResolver;
}
示例3: getViewResolver
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
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;
}
示例4: getCandidateViews
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes)
throws Exception {
List<View> candidateViews = new ArrayList<View>();
for (ViewResolver viewResolver : this.viewResolvers) {
View view = viewResolver.resolveViewName(viewName, locale);
if (view != null) {
candidateViews.add(view);
}
for (MediaType requestedMediaType : requestedMediaTypes) {
List<String> extensions = this.contentNegotiationManager.resolveFileExtensions(requestedMediaType);
for (String extension : extensions) {
String viewNameWithExtension = viewName + "." + extension;
view = viewResolver.resolveViewName(viewNameWithExtension, locale);
if (view != null) {
candidateViews.add(view);
}
}
}
}
if (!CollectionUtils.isEmpty(this.defaultViews)) {
candidateViews.addAll(this.defaultViews);
}
return candidateViews;
}
示例5: mvcViewResolver
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
/**
* 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;
}
示例6: resolveViewNameWithPathExtension
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@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);
}
示例7: resolveViewNameWithAcceptHeader
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@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);
}
示例8: resolveViewNameWithRequestParameter
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@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);
}
示例9: resolveViewNameAcceptHeader
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@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);
}
示例10: resolveViewNameAcceptHeaderSortByQuality
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@Test
public void resolveViewNameAcceptHeaderSortByQuality() throws Exception {
request.addHeader("Accept", "text/plain;q=0.5, application/json");
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(new HeaderContentNegotiationStrategy()));
ViewResolver htmlViewResolver = mock(ViewResolver.class);
ViewResolver jsonViewResolver = mock(ViewResolver.class);
viewResolver.setViewResolvers(Arrays.asList(htmlViewResolver, jsonViewResolver));
View htmlView = mock(View.class, "text_html");
View jsonViewMock = mock(View.class, "application_json");
String viewName = "view";
Locale locale = Locale.ENGLISH;
given(htmlViewResolver.resolveViewName(viewName, locale)).willReturn(htmlView);
given(jsonViewResolver.resolveViewName(viewName, locale)).willReturn(jsonViewMock);
given(htmlView.getContentType()).willReturn("text/html");
given(jsonViewMock.getContentType()).willReturn("application/json");
View result = viewResolver.resolveViewName(viewName, locale);
assertSame("Invalid view", jsonViewMock, result);
}
示例11: resolveViewNameFilename
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@Test
public void resolveViewNameFilename() throws Exception {
request.setRequestURI("/test.html");
ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
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(viewResolverMock1.resolveViewName(viewName + ".html", locale)).willReturn(null);
given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(null);
given(viewResolverMock2.resolveViewName(viewName + ".html", 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);
}
示例12: resolveViewContentTypeNull
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@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);
}
示例13: resolveViewNoMatch
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@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);
}
示例14: resolveViewNoMatchUseUnacceptableStatus
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@Test
public void resolveViewNoMatchUseUnacceptableStatus() throws Exception {
viewResolver.setUseNotAcceptableStatusCode(true);
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);
assertNotNull("Invalid view", result);
MockHttpServletResponse response = new MockHttpServletResponse();
result.render(null, request, response);
assertEquals("Invalid status code set", 406, response.getStatus());
}
示例15: nestedViewResolverIsNotSpringBean
import org.springframework.web.servlet.ViewResolver; //導入依賴的package包/類
@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
webAppContext.setServletContext(new MockServletContext());
webAppContext.refresh();
InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
nestedResolver.setApplicationContext(webAppContext);
nestedResolver.setViewClass(InternalResourceView.class);
viewResolver.setViewResolvers(new ArrayList<ViewResolver>(Arrays.asList(nestedResolver)));
FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));
viewResolver.afterPropertiesSet();
String viewName = "view";
Locale locale = Locale.ENGLISH;
View result = viewResolver.resolveViewName(viewName, locale);
assertNotNull("Invalid view", result);
}