diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppInitializer.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WebAppInitializer.java similarity index 77% rename from spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppInitializer.java rename to spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WebAppInitializer.java index 19fc09d6a8..9813077aab 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppInitializer.java +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WebAppInitializer.java @@ -1,25 +1,27 @@ -package com.baeldung; +package com.baeldung.controller; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; - + import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; +import com.baeldung.config.WebAppConfiguration_BeanNameUrlHandlerMapping; + public class WebAppInitializer implements WebApplicationInitializer { - + public void onStartup(ServletContext container) throws ServletException { - + AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); - ctx.register(WebAppConfiguration_ControllerClassNameHandlerMapping.class); + ctx.register(WebAppConfiguration_BeanNameUrlHandlerMapping.class); ctx.setServletContext(container); - + ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx)); - + servlet.setLoadOnStartup(1); - servlet.addMapping("/"); - + servlet.addMapping("/"); + } } diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeBaeldungController.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeBaeldungController.java new file mode 100644 index 0000000000..c8769c5607 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeBaeldungController.java @@ -0,0 +1,19 @@ +package com.baeldung.controller; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.AbstractController; + +public class WelcomeBaeldungController extends AbstractController { + @Override + protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { + System.out.println("Inside Welcome Baeldung Controller"); + + ModelAndView model = new ModelAndView("welcome"); + + return model; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeController.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeController.java index fb1f8676f3..baf9dd9ece 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeController.java @@ -1,19 +1,18 @@ -package com.baeldung; +package com.baeldung.controller; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; -public class WelcomeController extends AbstractController -{ +public class WelcomeController extends AbstractController { @Override - protected ModelAndView handleRequestInternal(HttpServletRequest request, - HttpServletResponse response) throws Exception { - System.out.println("Inside BeanNameMappingExampleOne Controller"); + protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { + System.out.println("Inside Welcome Controller"); + + ModelAndView model = new ModelAndView("welcome"); - ModelAndView model = new ModelAndView("test"); - return model; } } \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/ExampleTwoController.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeTwoController.java similarity index 52% rename from spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/ExampleTwoController.java rename to spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeTwoController.java index dceeca779f..2a63e57fe4 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/ExampleTwoController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/Controller/WelcomeTwoController.java @@ -1,19 +1,19 @@ -package com.baeldung; +package com.baeldung.controller; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; -public class ExampleTwoController extends AbstractController -{ +public class WelcomeTwoController extends AbstractController { @Override - protected ModelAndView handleRequestInternal(HttpServletRequest request, - HttpServletResponse response) throws Exception { - System.out.println("Inside ExampleTwo Controller"); + protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { + System.out.println("Inside Welcome two Controller"); + + ModelAndView model = new ModelAndView("welcome"); - ModelAndView model = new ModelAndView("exampleTwo"); - return model; } -} \ No newline at end of file + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_BeanNameUrlHandlerMapping.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_BeanNameUrlHandlerMapping.java new file mode 100644 index 0000000000..9078444c51 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_BeanNameUrlHandlerMapping.java @@ -0,0 +1,34 @@ +package com.baeldung.config; + +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.view.InternalResourceViewResolver; + +import com.baeldung.controller.WelcomeController; + +@Configuration +public class WebAppConfiguration_BeanNameUrlHandlerMapping { + + @Bean + public ViewResolver viewResolver() { + InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); + viewResolver.setPrefix("/"); + viewResolver.setSuffix(".jsp"); + return viewResolver; + } + + @Bean + BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() { + BeanNameUrlHandlerMapping bean = new BeanNameUrlHandlerMapping(); + return bean; + } + + @Bean("/welcome") + public WelcomeController welcome() { + WelcomeController welcome = new WelcomeController(); + return welcome; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ConfiguringPriorities.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ConfiguringPriorities.java new file mode 100644 index 0000000000..ba9968caca --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ConfiguringPriorities.java @@ -0,0 +1,60 @@ +package com.baeldung.config; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping; +import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; +import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping; + +import com.baeldung.controller.WelcomeBaeldungController; +import com.baeldung.controller.WelcomeController; +import com.baeldung.controller.WelcomeTwoController; + +@Configuration +public class WebAppConfiguration_ConfiguringPriorities { + + @Bean + BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() { + BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping(); + beanNameUrlHandlerMapping.setOrder(2); + return beanNameUrlHandlerMapping; + } + + @Bean("/welcome") + public WelcomeBaeldungController welcomeBaeldungController() { + WelcomeBaeldungController welcomeBaeldungController = new WelcomeBaeldungController(); + return welcomeBaeldungController; + } + + @Bean + public SimpleUrlHandlerMapping simpleUrlHandlerMapping() { + SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping(); + Map urlMap = new HashMap<>(); + urlMap.put("/welcome", welcome()); + simpleUrlHandlerMapping.setUrlMap(urlMap); + simpleUrlHandlerMapping.setOrder(0); + return simpleUrlHandlerMapping; + } + + @Bean + public WelcomeTwoController welcome() { + WelcomeTwoController bean = new WelcomeTwoController(); + return bean; + } + + @Bean + public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() { + ControllerClassNameHandlerMapping controllerClassNameHandlerMapping = new ControllerClassNameHandlerMapping(); + controllerClassNameHandlerMapping.setOrder(1); + return controllerClassNameHandlerMapping; + } + + @Bean + public WelcomeController welcomeController() { + WelcomeController bean = new WelcomeController(); + return bean; + } +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ControllerClassNameHandlerMapping.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ControllerClassNameHandlerMapping.java index c6ad821616..ac822dbe5a 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ControllerClassNameHandlerMapping.java +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_ControllerClassNameHandlerMapping.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -6,33 +6,29 @@ import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping; import org.springframework.web.servlet.view.InternalResourceViewResolver; +import com.baeldung.controller.WelcomeController; + @Configuration public class WebAppConfiguration_ControllerClassNameHandlerMapping { - @Bean - public ViewResolver viewResolver() { - InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); - viewResolver.setPrefix("/"); - viewResolver.setSuffix(".jsp"); - return viewResolver; - } + @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 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; - } + @Bean + public WelcomeController welcome() { + WelcomeController welcome = new WelcomeController(); + return welcome; + } } diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_SimpleUrlHandlerMapping.java b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_SimpleUrlHandlerMapping.java index 33cc81a4b8..48504ed76d 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_SimpleUrlHandlerMapping.java +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/JavaConfig/WebAppConfiguration_SimpleUrlHandlerMapping.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.config; import java.util.HashMap; import java.util.Map; @@ -9,37 +9,33 @@ import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.servlet.view.InternalResourceViewResolver; +import com.baeldung.controller.WelcomeController; + + @Configuration public class WebAppConfiguration_SimpleUrlHandlerMapping { - @Bean - public ViewResolver viewResolver() { - InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); - viewResolver.setPrefix("/"); - viewResolver.setSuffix(".jsp"); - return viewResolver; - } + @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("/welcome", welcome()); - urlMap.put("/exampleTwo", exampleTwo()); - simpleUrlHandlerMapping.setUrlMap(urlMap); - return simpleUrlHandlerMapping; - } + @Bean + public SimpleUrlHandlerMapping simpleUrlHandlerMapping() { + SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping(); + Map urlMap = new HashMap<>(); + urlMap.put("/welcome", welcome()); + 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; - } + @Bean + public WelcomeController welcome() { + WelcomeController welcome = new WelcomeController(); + return welcome; + } } diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/BeanNameUrlHandlerMapping.xml b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/BeanNameUrlHandlerMappingConfig.xml similarity index 93% rename from spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/BeanNameUrlHandlerMapping.xml rename to spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/BeanNameUrlHandlerMappingConfig.xml index 867311f53c..c8b68b99aa 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/BeanNameUrlHandlerMapping.xml +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/BeanNameUrlHandlerMappingConfig.xml @@ -22,10 +22,8 @@ - + - - diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/ControllerClassNameHandlerMappingConfig.xml b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/ControllerClassNameHandlerMappingConfig.xml index aacffb6b96..26eaa97595 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/ControllerClassNameHandlerMappingConfig.xml +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/ControllerClassNameHandlerMappingConfig.xml @@ -24,11 +24,10 @@ - - + + - diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/HandlerMappingConfiguringPriorities.xml b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/HandlerMappingConfiguringPriorities.xml new file mode 100644 index 0000000000..5666d25748 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/HandlerMappingConfiguringPriorities.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + /welcome=welcome + /*/welcome=welcome + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/SimpleUrlHandlerMappingConfig.xml b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/SimpleUrlHandlerMappingConfig.xml index 68fe422268..e69ab2afab 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/SimpleUrlHandlerMappingConfig.xml +++ b/spring-mvc-java/src/main/java/com/baeldung/HandlerMapping/config/XML/SimpleUrlHandlerMappingConfig.xml @@ -3,14 +3,16 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" - http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context.xsd - http://www.springframework.org/schema/mvc - http://www.springframework.org/schema/mvc/spring-mvc.xsd - http://www.springframework.org/schema/cache - http://www.springframework.org/schema/cache/spring-cache.xsd"> + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd + + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context.xsd + http://www.springframework.org/schema/mvc + http://www.springframework.org/schema/mvc/spring-mvc.xsd + http://www.springframework.org/schema/cache + http://www.springframework.org/schema/cache/spring-cache.xsd"> + - /test=firstController - /*/test=firstController - /exampleTwo=exampleTwoController + /test=welcome + /*/test=welcome - + - - + \ No newline at end of file