From b7b5463309253385ac2847dd20d745fdb84c8972 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Thu, 29 Dec 2016 17:11:28 +0100 Subject: [PATCH] BAEL-97 - adding BeanNameMappingHandler --- .../config/BeanNameHandlerMappingConfig.java | 41 +++++++++++++++++++ ...ntrollerClassNameHandlerMappingConfig.java | 4 +- .../config/SimpleUrlHandlerMappingConfig.java | 4 +- .../controller/BeanNameMappingConfigTest.java | 41 +++++++++++++++++++ 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 spring-mvc-java/src/main/java/com/baeldung/spring/web/config/BeanNameHandlerMappingConfig.java create mode 100644 spring-mvc-java/src/test/java/com/baeldung/web/controller/BeanNameMappingConfigTest.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/BeanNameHandlerMappingConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/BeanNameHandlerMappingConfig.java new file mode 100644 index 0000000000..fce1d50a00 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/BeanNameHandlerMappingConfig.java @@ -0,0 +1,41 @@ +package com.baeldung.spring.web.config; + +import com.baeldung.web.controller.handlermapping.ExampleTwoController; +import com.baeldung.web.controller.handlermapping.WelcomeController; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping; +import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +public class BeanNameHandlerMappingConfig { + + @Bean + public ViewResolver viewResolver() { + InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); + viewResolver.setPrefix("/"); + viewResolver.setSuffix(".jsp"); + return viewResolver; + } + + @Bean + public BeanNameUrlHandlerMapping controllerClassNameHandlerMapping() { + BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping(); + return beanNameUrlHandlerMapping; + } + + @Bean(name="/welcomeBean") + public WelcomeController welcomeBean() { + WelcomeController welcome = new WelcomeController(); + return welcome; + } + + @Bean(name="/ex") + public ExampleTwoController exampleTwo() { + ExampleTwoController exampleTwo = new ExampleTwoController(); + return exampleTwo; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ControllerClassNameHandlerMappingConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ControllerClassNameHandlerMappingConfig.java index 8e060f72f6..b03b781e5a 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ControllerClassNameHandlerMappingConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ControllerClassNameHandlerMappingConfig.java @@ -1,7 +1,7 @@ package com.baeldung.spring.web.config; -import com.baeldung.ExampleTwoController; -import com.baeldung.WelcomeController; +import com.baeldung.web.controller.handlermapping.ExampleTwoController; +import com.baeldung.web.controller.handlermapping.WelcomeController; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/SimpleUrlHandlerMappingConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/SimpleUrlHandlerMappingConfig.java index beb4e870d8..aef7750dc0 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/SimpleUrlHandlerMappingConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/SimpleUrlHandlerMappingConfig.java @@ -1,7 +1,7 @@ package com.baeldung.spring.web.config; -import com.baeldung.ExampleTwoController; -import com.baeldung.WelcomeController; +import com.baeldung.web.controller.handlermapping.ExampleTwoController; +import com.baeldung.web.controller.handlermapping.WelcomeController; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/BeanNameMappingConfigTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/BeanNameMappingConfigTest.java new file mode 100644 index 0000000000..dc6d861db2 --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/BeanNameMappingConfigTest.java @@ -0,0 +1,41 @@ +package com.baeldung.web.controller; + +import com.baeldung.spring.web.config.BeanNameHandlerMappingConfig; +import com.baeldung.spring.web.config.SimpleUrlHandlerMappingConfig; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = BeanNameHandlerMappingConfig.class) +public class BeanNameMappingConfigTest { + + @Autowired + private WebApplicationContext webAppContext; + private MockMvc mockMvc; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build(); + } + + @Test + public void whenBeanNameMapping_thenMappedOK() throws Exception { + mockMvc.perform(get("/welcomeBean")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print()); + } +}