Search in sources :

Example 61 with InternalResourceViewResolver

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"));
}
Also used : InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 62 with InternalResourceViewResolver

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")));
}
Also used : ArrayList(java.util.ArrayList) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) View(org.springframework.web.servlet.View) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MockMvc(org.springframework.test.web.servlet.MockMvc) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 63 with InternalResourceViewResolver

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"));
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) MockMvcWebTestClient(org.springframework.test.web.servlet.client.MockMvcWebTestClient) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 64 with InternalResourceViewResolver

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));
}
Also used : ServletException(jakarta.servlet.ServletException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 65 with InternalResourceViewResolver

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;
}
Also used : InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Bean(org.springframework.context.annotation.Bean)

Aggregations

InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)65 Bean (org.springframework.context.annotation.Bean)47 Test (org.junit.jupiter.api.Test)8 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)5 ArrayList (java.util.ArrayList)4 MappingJackson2JsonView (org.springframework.web.servlet.view.json.MappingJackson2JsonView)4 Test (org.junit.Test)3 Jaxb2Marshaller (org.springframework.oxm.jaxb.Jaxb2Marshaller)3 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 FixedContentNegotiationStrategy (org.springframework.web.accept.FixedContentNegotiationStrategy)3 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)3 View (org.springframework.web.servlet.View)3 MarshallingView (org.springframework.web.servlet.view.xml.MarshallingView)3 Before (org.junit.Before)2 Order (org.springframework.core.annotation.Order)2 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)2 MockMvc (org.springframework.test.web.servlet.MockMvc)2 MockMvcWebTestClient (org.springframework.test.web.servlet.client.MockMvcWebTestClient)2 ContentNegotiationManagerFactoryBean (org.springframework.web.accept.ContentNegotiationManagerFactoryBean)2 ViewResolverComposite (org.springframework.web.servlet.view.ViewResolverComposite)2