From 6a203ae91bcad889998d2ff579b74eefbec90ad9 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 2 Feb 2016 17:37:39 +0100 Subject: [PATCH] Add multiple ViewResolver configuration example --- .../baeldung/spring/web/config/WebConfig.java | 49 +++++++++++++++++-- .../src/main/resources/views.properties | 3 ++ spring-mvc-java/src/main/resources/views.xml | 10 ++++ 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 spring-mvc-java/src/main/resources/views.properties create mode 100644 spring-mvc-java/src/main/resources/views.xml diff --git a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java index d60bcfe127..58438d2976 100644 --- a/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/org/baeldung/spring/web/config/WebConfig.java @@ -1,19 +1,60 @@ package org.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.WebMvcConfigurerAdapter; +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; @Configuration @EnableWebMvc @ComponentScan("org.baeldung.web") public class WebConfig extends WebMvcConfigurerAdapter { - public WebConfig() { - super(); - } + public WebConfig() { + super(); + } - // API + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + registry.addViewController("/sample.html"); + } + + @Bean + public ViewResolver internalResourceViewResolver() { + + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + bean.setOrder(2); + return bean; + } + + @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; + } } \ No newline at end of file diff --git a/spring-mvc-java/src/main/resources/views.properties b/spring-mvc-java/src/main/resources/views.properties new file mode 100644 index 0000000000..95687cb62a --- /dev/null +++ b/spring-mvc-java/src/main/resources/views.properties @@ -0,0 +1,3 @@ +sample.(class)=org.springframework.web.servlet.view.JstlView +sample.url=/WEB-INF/view/sample.jsp + diff --git a/spring-mvc-java/src/main/resources/views.xml b/spring-mvc-java/src/main/resources/views.xml new file mode 100644 index 0000000000..83bca5293d --- /dev/null +++ b/spring-mvc-java/src/main/resources/views.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file