Moved ViewResolver article related code from spring-mvc-java to spring-mvc-basics
This commit is contained in:
parent
ee4953b7ef
commit
ed7630c2d0
|
@ -9,3 +9,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Spring MVC Tutorial](https://www.baeldung.com/spring-mvc-tutorial)
|
||||
- [The Spring @Controller and @RestController Annotations](http://www.baeldung.com/spring-controller-vs-restcontroller)
|
||||
- [Using Spring ResponseEntity to Manipulate the HTTP Response](http://www.baeldung.com/spring-response-entity)
|
||||
- [A Guide to the ViewResolver in Spring MVC](http://www.baeldung.com/spring-mvc-view-resolver-tutorial)
|
|
@ -56,6 +56,18 @@
|
|||
<scope>test</scope>
|
||||
<version>${jayway.json-path.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -72,6 +84,8 @@
|
|||
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
|
||||
<javax.jsp-api.version>2.3.3</javax.jsp-api.version>
|
||||
<jayway.json-path.version>2.4.0</jayway.json-path.version>
|
||||
<rest-assured.version>4.0.0</rest-assured.version>
|
||||
<hamcrest.version>1.3</hamcrest.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -5,9 +5,20 @@ import org.springframework.web.bind.annotation.GetMapping;
|
|||
|
||||
@Controller
|
||||
public class SampleController {
|
||||
|
||||
@GetMapping("/sample")
|
||||
public String showForm() {
|
||||
return "sample";
|
||||
}
|
||||
|
||||
@GetMapping("/sample2")
|
||||
public String showForm2() {
|
||||
return "sample2";
|
||||
}
|
||||
|
||||
@GetMapping("/sample3")
|
||||
public String showForm3() {
|
||||
return "sample3";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
sample2.(class)=org.springframework.web.servlet.view.JstlView
|
||||
sample2.url=/WEB-INF/view2/sample2.jsp
|
||||
|
|
@ -3,8 +3,8 @@
|
|||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
|
||||
|
||||
<bean id="sample" class="org.springframework.web.servlet.view.JstlView">
|
||||
<property name="url" value="/WEB-INF/view/sample.jsp" />
|
||||
<bean id="sample3" class="org.springframework.web.servlet.view.JstlView">
|
||||
<property name="url" value="/WEB-INF/view3/sample3.jsp" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<head></head>
|
||||
|
||||
<body>
|
||||
<h1>This is the body of the sample2 view</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<head></head>
|
||||
|
||||
<body>
|
||||
<h1>This is the body of the sample3 view</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -9,7 +9,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Spring Bean Annotations](http://www.baeldung.com/spring-bean-annotations)
|
||||
- [Introduction to Pointcut Expressions in Spring](http://www.baeldung.com/spring-aop-pointcut-tutorial)
|
||||
- [Introduction to Advice Types in Spring](http://www.baeldung.com/spring-aop-advice-tutorial)
|
||||
- [A Guide to the ViewResolver in Spring MVC](http://www.baeldung.com/spring-mvc-view-resolver-tutorial)
|
||||
- [Integration Testing in Spring](http://www.baeldung.com/integration-testing-in-spring)
|
||||
- [A Quick Guide to Spring MVC Matrix Variables](http://www.baeldung.com/spring-mvc-matrix-variables)
|
||||
- [Intro to WebSockets with Spring](http://www.baeldung.com/websockets-spring)
|
||||
|
|
|
@ -12,7 +12,6 @@ import org.springframework.context.annotation.ComponentScan;
|
|||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Description;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
|
@ -26,8 +25,6 @@ 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;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
|
@ -111,22 +108,6 @@ public class WebConfig implements WebMvcConfigurer {
|
|||
return multipartResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver xmlViewResolver() {
|
||||
final XmlViewResolver bean = new XmlViewResolver();
|
||||
bean.setLocation(new ClassPathResource("views.xml"));
|
||||
bean.setOrder(1);
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver resourceBundleViewResolver() {
|
||||
final ResourceBundleViewResolver bean = new ResourceBundleViewResolver();
|
||||
bean.setBasename("views");
|
||||
bean.setOrder(0);
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void extendMessageConverters(final List<HttpMessageConverter<?>> converters) {
|
||||
converters.add(byteArrayHttpMessageConverter());
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
sample.(class)=org.springframework.web.servlet.view.JstlView
|
||||
sample.url=/WEB-INF/view/sample.jsp
|
||||
|
Loading…
Reference in New Issue