Moved ViewResolver article related code from spring-mvc-java to spring-mvc-basics

This commit is contained in:
Gerardo Roza
2019-05-26 19:08:28 -03:00
parent ee4953b7ef
commit ed7630c2d0
12 changed files with 108 additions and 36 deletions

View File

@@ -3,12 +3,15 @@ package com.baeldung.spring.web.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
import org.springframework.web.servlet.view.XmlViewResolver;
@EnableWebMvc
@Configuration
@@ -27,7 +30,23 @@ public class WebConfig implements WebMvcConfigurer {
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
bean.setOrder(2);
return bean;
}
@Bean
public ViewResolver resourceBundleViewResolver() {
final ResourceBundleViewResolver bean = new ResourceBundleViewResolver();
bean.setBasename("views");
bean.setOrder(0);
return bean;
}
@Bean
public ViewResolver xmlViewResolver() {
final XmlViewResolver bean = new XmlViewResolver();
bean.setLocation(new ClassPathResource("views.xml"));
bean.setOrder(1);
return bean;
}
}

View File

@@ -5,9 +5,20 @@ import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class SampleController {
@GetMapping("/sample")
@GetMapping("/sample")
public String showForm() {
return "sample";
}
@GetMapping("/sample2")
public String showForm2() {
return "sample2";
}
@GetMapping("/sample3")
public String showForm3() {
return "sample3";
}
}

View File

@@ -0,0 +1,3 @@
sample2.(class)=org.springframework.web.servlet.view.JstlView
sample2.url=/WEB-INF/view2/sample2.jsp

View File

@@ -0,0 +1,10 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<bean id="sample3" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/WEB-INF/view3/sample3.jsp" />
</bean>
</beans>

View File

@@ -0,0 +1,7 @@
<html>
<head></head>
<body>
<h1>This is the body of the sample2 view</h1>
</body>
</html>

View File

@@ -0,0 +1,7 @@
<html>
<head></head>
<body>
<h1>This is the body of the sample3 view</h1>
</body>
</html>

View File

@@ -0,0 +1,33 @@
package com.baeldung.web.controller;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import io.restassured.RestAssured;
public class SampleControllerLiveTest {
private static final String SERVICE_BASE_URL = "/spring-mvc-basics";
@Test
public void whenSampleEndpointCalled_thenOkResponseObtained() throws Exception {
RestAssured.get(SERVICE_BASE_URL + "/sample")
.then()
.statusCode(HttpStatus.OK.value());
}
@Test
public void whenSample2EndpointCalled_thenOkResponseObtained() throws Exception {
RestAssured.get(SERVICE_BASE_URL + "/sample2")
.then()
.statusCode(HttpStatus.OK.value());
}
@Test
public void whenSample3EndpointCalled_thenOkResponseObtained() throws Exception {
RestAssured.get(SERVICE_BASE_URL + "/sample3")
.then()
.statusCode(HttpStatus.OK.value());
}
}