use of org.springframework.web.servlet.view.InternalResourceViewResolver in project spring-framework by spring-projects.
the class ViewResolutionTests method jspOnly.
@Test
void jspOnly() throws Exception {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver("/WEB-INF/", ".jsp");
standaloneSetup(new PersonController()).setViewResolvers(viewResolver).build().perform(get("/person/Corea")).andExpect(status().isOk()).andExpect(model().size(1)).andExpect(model().attributeExists("person")).andExpect(forwardedUrl("/WEB-INF/person/show.jsp"));
}
use of org.springframework.web.servlet.view.InternalResourceViewResolver in project spring-framework by spring-projects.
the class ViewResolutionTests method contentNegotiation.
@Test
void contentNegotiation() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(Person.class);
List<View> viewList = new ArrayList<>();
viewList.add(new MappingJackson2JsonView());
viewList.add(new MarshallingView(marshaller));
ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
cnViewResolver.setDefaultViews(viewList);
cnViewResolver.setContentNegotiationManager(manager);
cnViewResolver.afterPropertiesSet();
MockMvc mockMvc = standaloneSetup(new PersonController()).setViewResolvers(cnViewResolver, new InternalResourceViewResolver()).build();
mockMvc.perform(get("/person/Corea")).andExpect(status().isOk()).andExpect(model().size(1)).andExpect(model().attributeExists("person")).andExpect(forwardedUrl("person/show"));
mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.person.name").value("Corea"));
mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_XML)).andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
use of org.springframework.web.servlet.view.InternalResourceViewResolver in project spring-framework by spring-projects.
the class ViewResolutionTests method jspOnly.
@Test
void jspOnly() throws Exception {
WebTestClient testClient = MockMvcWebTestClient.bindToController(new PersonController()).viewResolvers(new InternalResourceViewResolver("/WEB-INF/", ".jsp")).build();
EntityExchangeResult<Void> result = testClient.get().uri("/person/Corea").exchange().expectStatus().isOk().expectBody().isEmpty();
// Further assertions on the server response
MockMvcWebTestClient.resultActionsFor(result).andExpect(status().isOk()).andExpect(model().size(1)).andExpect(model().attributeExists("person")).andExpect(forwardedUrl("/WEB-INF/person/show.jsp"));
}
use of org.springframework.web.servlet.view.InternalResourceViewResolver in project spring-framework by spring-projects.
the class DispatcherServletTests method withNoViewAndSamePath.
@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();
assertThatExceptionOfType(ServletException.class).isThrownBy(() -> complexDispatcherServlet.service(request, response));
}
use of org.springframework.web.servlet.view.InternalResourceViewResolver in project spring-boot by spring-projects.
the class WebConfig method viewResolver.
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
Aggregations