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 new file mode 100644 index 0000000000..8e060f72f6 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ControllerClassNameHandlerMappingConfig.java @@ -0,0 +1,42 @@ +package com.baeldung.spring.web.config; + +import com.baeldung.ExampleTwoController; +import com.baeldung.WelcomeController; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +public class ControllerClassNameHandlerMappingConfig { + + @Bean + public ViewResolver viewResolver() { + InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); + viewResolver.setPrefix("/"); + viewResolver.setSuffix(".jsp"); + return viewResolver; + } + + @Bean + public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() { + ControllerClassNameHandlerMapping controllerClassNameHandlerMapping = new ControllerClassNameHandlerMapping(); + return controllerClassNameHandlerMapping; + } + + @Bean + public WelcomeController welcome() { + WelcomeController welcome = new WelcomeController(); + return welcome; + } + + @Bean + public ExampleTwoController exampleTwo() { + ExampleTwoController exampleTwo = new ExampleTwoController(); + return exampleTwo; + } + +} 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 new file mode 100644 index 0000000000..beb4e870d8 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/SimpleUrlHandlerMappingConfig.java @@ -0,0 +1,47 @@ +package com.baeldung.spring.web.config; + +import com.baeldung.ExampleTwoController; +import com.baeldung.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.SimpleUrlHandlerMapping; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +import java.util.HashMap; +import java.util.Map; + +@Configuration +public class SimpleUrlHandlerMappingConfig { + + @Bean + public ViewResolver viewResolver() { + InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); + viewResolver.setPrefix("/"); + viewResolver.setSuffix(".jsp"); + return viewResolver; + } + + @Bean + public SimpleUrlHandlerMapping simpleUrlHandlerMapping() { + SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping(); + Map urlMap = new HashMap<>(); + urlMap.put("/simpleUrlWelcome", welcome()); + urlMap.put("/exampleTwo", exampleTwo()); + simpleUrlHandlerMapping.setUrlMap(urlMap); + return simpleUrlHandlerMapping; + } + + @Bean + public WelcomeController welcome() { + WelcomeController welcome = new WelcomeController(); + return welcome; + } + + @Bean + public ExampleTwoController exampleTwo() { + ExampleTwoController exampleTwo = new ExampleTwoController(); + return exampleTwo; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/handlermapping/ExampleTwoController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/handlermapping/ExampleTwoController.java new file mode 100644 index 0000000000..efdec6408c --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/handlermapping/ExampleTwoController.java @@ -0,0 +1,22 @@ +package com.baeldung.web.controller.handlermapping; + +import org.springframework.stereotype.Controller; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.AbstractController; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Controller +public class ExampleTwoController extends AbstractController +{ + @Override + protected ModelAndView handleRequestInternal(HttpServletRequest request, + HttpServletResponse response) throws Exception { + System.out.println("Inside ExampleTwo Controller"); + + ModelAndView model = new ModelAndView("exampleTwo"); + + return model; + } +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/handlermapping/WelcomeController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/handlermapping/WelcomeController.java new file mode 100644 index 0000000000..396244f01c --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/handlermapping/WelcomeController.java @@ -0,0 +1,22 @@ +package com.baeldung.web.controller.handlermapping; + +import org.springframework.stereotype.Controller; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.AbstractController; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Controller +public class WelcomeController extends AbstractController +{ + @Override + protected ModelAndView handleRequestInternal(HttpServletRequest request, + HttpServletResponse response) throws Exception { + System.out.println("Inside BeanNameMappingExampleOne Controller"); + + ModelAndView model = new ModelAndView("test"); + + return model; + } +} \ No newline at end of file diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/ControllerClassNameHandlerMappingTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/ControllerClassNameHandlerMappingTest.java new file mode 100644 index 0000000000..ea6243a1eb --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/ControllerClassNameHandlerMappingTest.java @@ -0,0 +1,39 @@ +package com.baeldung.web.controller; + +import com.baeldung.spring.web.config.ControllerClassNameHandlerMappingConfig; +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.*; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = ControllerClassNameHandlerMappingConfig.class) +public class ControllerClassNameHandlerMappingTest { + + @Autowired + private WebApplicationContext webAppContext; + private MockMvc mockMvc; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build(); + } + + @Test + public void whenControllerClassNameMapping_thenMappedOK() throws Exception { + mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print()); + } +} diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleUrlMappingConfigTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleUrlMappingConfigTest.java new file mode 100644 index 0000000000..0f83244738 --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleUrlMappingConfigTest.java @@ -0,0 +1,40 @@ +package com.baeldung.web.controller; + +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 = SimpleUrlHandlerMappingConfig.class) +public class SimpleUrlMappingConfigTest { + + @Autowired + private WebApplicationContext webAppContext; + private MockMvc mockMvc; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build(); + } + + @Test + public void whenSimpleUrlMapping_thenMappedOK() throws Exception { + mockMvc.perform(get("/simpleUrlWelcome")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print()); + } +}