added modified HandlerMapping code
This commit is contained in:
parent
f8edea40ae
commit
31b74a996c
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung;
|
package com.baeldung.controller;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
@ -8,12 +8,14 @@ import org.springframework.web.WebApplicationInitializer;
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
import org.springframework.web.servlet.DispatcherServlet;
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
|
|
||||||
|
import com.baeldung.config.WebAppConfiguration_BeanNameUrlHandlerMapping;
|
||||||
|
|
||||||
public class WebAppInitializer implements WebApplicationInitializer {
|
public class WebAppInitializer implements WebApplicationInitializer {
|
||||||
|
|
||||||
public void onStartup(ServletContext container) throws ServletException {
|
public void onStartup(ServletContext container) throws ServletException {
|
||||||
|
|
||||||
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
|
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
|
||||||
ctx.register(WebAppConfiguration_ControllerClassNameHandlerMapping.class);
|
ctx.register(WebAppConfiguration_BeanNameUrlHandlerMapping.class);
|
||||||
ctx.setServletContext(container);
|
ctx.setServletContext(container);
|
||||||
|
|
||||||
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
|
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,18 +1,17 @@
|
||||||
package com.baeldung;
|
package com.baeldung.controller;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
import org.springframework.web.servlet.mvc.AbstractController;
|
import org.springframework.web.servlet.mvc.AbstractController;
|
||||||
|
|
||||||
public class WelcomeController extends AbstractController
|
public class WelcomeController extends AbstractController {
|
||||||
{
|
|
||||||
@Override
|
@Override
|
||||||
protected ModelAndView handleRequestInternal(HttpServletRequest request,
|
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||||
HttpServletResponse response) throws Exception {
|
System.out.println("Inside Welcome Controller");
|
||||||
System.out.println("Inside BeanNameMappingExampleOne Controller");
|
|
||||||
|
|
||||||
ModelAndView model = new ModelAndView("test");
|
ModelAndView model = new ModelAndView("welcome");
|
||||||
|
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
package com.baeldung;
|
package com.baeldung.controller;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
import org.springframework.web.servlet.mvc.AbstractController;
|
import org.springframework.web.servlet.mvc.AbstractController;
|
||||||
|
|
||||||
public class ExampleTwoController extends AbstractController
|
public class WelcomeTwoController extends AbstractController {
|
||||||
{
|
|
||||||
@Override
|
@Override
|
||||||
protected ModelAndView handleRequestInternal(HttpServletRequest request,
|
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||||
HttpServletResponse response) throws Exception {
|
System.out.println("Inside Welcome two Controller");
|
||||||
System.out.println("Inside ExampleTwo Controller");
|
|
||||||
|
|
||||||
ModelAndView model = new ModelAndView("exampleTwo");
|
ModelAndView model = new ModelAndView("welcome");
|
||||||
|
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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<String, Object> 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung;
|
package com.baeldung.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
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.mvc.support.ControllerClassNameHandlerMapping;
|
||||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||||
|
|
||||||
|
import com.baeldung.controller.WelcomeController;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebAppConfiguration_ControllerClassNameHandlerMapping {
|
public class WebAppConfiguration_ControllerClassNameHandlerMapping {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ViewResolver viewResolver() {
|
public ViewResolver viewResolver() {
|
||||||
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
||||||
viewResolver.setPrefix("/");
|
viewResolver.setPrefix("/");
|
||||||
viewResolver.setSuffix(".jsp");
|
viewResolver.setSuffix(".jsp");
|
||||||
return viewResolver;
|
return viewResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() {
|
public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() {
|
||||||
ControllerClassNameHandlerMapping controllerClassNameHandlerMapping = new ControllerClassNameHandlerMapping();
|
ControllerClassNameHandlerMapping controllerClassNameHandlerMapping = new ControllerClassNameHandlerMapping();
|
||||||
return controllerClassNameHandlerMapping;
|
return controllerClassNameHandlerMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public WelcomeController welcome() {
|
public WelcomeController welcome() {
|
||||||
WelcomeController welcome = new WelcomeController();
|
WelcomeController welcome = new WelcomeController();
|
||||||
return welcome;
|
return welcome;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ExampleTwoController exampleTwo() {
|
|
||||||
ExampleTwoController exampleTwo = new ExampleTwoController();
|
|
||||||
return exampleTwo;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung;
|
package com.baeldung.config;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
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.handler.SimpleUrlHandlerMapping;
|
||||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||||
|
|
||||||
|
import com.baeldung.controller.WelcomeController;
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebAppConfiguration_SimpleUrlHandlerMapping {
|
public class WebAppConfiguration_SimpleUrlHandlerMapping {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ViewResolver viewResolver() {
|
public ViewResolver viewResolver() {
|
||||||
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
||||||
viewResolver.setPrefix("/");
|
viewResolver.setPrefix("/");
|
||||||
viewResolver.setSuffix(".jsp");
|
viewResolver.setSuffix(".jsp");
|
||||||
return viewResolver;
|
return viewResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
|
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
|
||||||
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
|
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
|
||||||
Map<String, Object> urlMap = new HashMap<>();
|
Map<String, Object> urlMap = new HashMap<>();
|
||||||
urlMap.put("/welcome", welcome());
|
urlMap.put("/welcome", welcome());
|
||||||
urlMap.put("/exampleTwo", exampleTwo());
|
simpleUrlHandlerMapping.setUrlMap(urlMap);
|
||||||
simpleUrlHandlerMapping.setUrlMap(urlMap);
|
return simpleUrlHandlerMapping;
|
||||||
return simpleUrlHandlerMapping;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public WelcomeController welcome() {
|
public WelcomeController welcome() {
|
||||||
WelcomeController welcome = new WelcomeController();
|
WelcomeController welcome = new WelcomeController();
|
||||||
return welcome;
|
return welcome;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ExampleTwoController exampleTwo() {
|
|
||||||
ExampleTwoController exampleTwo = new ExampleTwoController();
|
|
||||||
return exampleTwo;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,8 +25,6 @@
|
||||||
|
|
||||||
<bean name="welcome" class="com.baeldung.WelcomeController" />
|
<bean name="welcome" class="com.baeldung.WelcomeController" />
|
||||||
|
|
||||||
<bean name="/exampleTwo" class="com.baeldung.ExampleTwoController" />
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -25,10 +25,9 @@
|
||||||
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
|
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<!-- register beans -->
|
<!-- register beans -->
|
||||||
<bean class="com.baeldung.WelcomeController" />
|
<bean class="com.baeldung.WelcomeController" />
|
||||||
|
|
||||||
<bean class="com.baeldung.ExampleTwoController" />
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
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">
|
||||||
|
|
||||||
|
|
||||||
|
<bean
|
||||||
|
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
|
||||||
|
<property name="order" value="2" />
|
||||||
|
</bean>
|
||||||
|
<bean name="/welcome" class="com.baeldung.WelcomeController" />
|
||||||
|
|
||||||
|
|
||||||
|
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
|
||||||
|
<property name="mappings">
|
||||||
|
<value>
|
||||||
|
/welcome=welcome
|
||||||
|
/*/welcome=welcome
|
||||||
|
</value>
|
||||||
|
</property>
|
||||||
|
<property name="order" value="0" />
|
||||||
|
</bean>
|
||||||
|
<bean id="welcome" class="com.baeldung.WelcomeTwoController" />
|
||||||
|
|
||||||
|
|
||||||
|
<bean
|
||||||
|
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
|
||||||
|
<property name="order" value="1" />
|
||||||
|
</bean>
|
||||||
|
<bean class="com.baeldung.WelcomeController" />
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</beans>
|
|
@ -3,14 +3,16 @@
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
|
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"
|
xmlns:cache="http://www.springframework.org/schema/cache"
|
||||||
xsi:schemaLocation="
|
xsi:schemaLocation="
|
||||||
http://www.springframework.org/schema/beans
|
http://www.springframework.org/schema/beans
|
||||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
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/context
|
||||||
http://www.springframework.org/schema/mvc
|
http://www.springframework.org/schema/context/spring-context.xsd
|
||||||
http://www.springframework.org/schema/mvc/spring-mvc.xsd
|
http://www.springframework.org/schema/mvc
|
||||||
http://www.springframework.org/schema/cache
|
http://www.springframework.org/schema/mvc/spring-mvc.xsd
|
||||||
http://www.springframework.org/schema/cache/spring-cache.xsd">
|
http://www.springframework.org/schema/cache
|
||||||
|
http://www.springframework.org/schema/cache/spring-cache.xsd">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<bean
|
<bean
|
||||||
|
@ -23,16 +25,14 @@
|
||||||
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
|
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
|
||||||
<property name="mappings">
|
<property name="mappings">
|
||||||
<value>
|
<value>
|
||||||
/test=firstController
|
/test=welcome
|
||||||
/*/test=firstController
|
/*/test=welcome
|
||||||
/exampleTwo=exampleTwoController
|
|
||||||
</value>
|
</value>
|
||||||
</property>
|
</property>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<bean id="welcome" class="com.baeldung.WelcomeController" />
|
<bean id="welcome" class="com.baeldung.WelcomeController" />
|
||||||
|
|
||||||
<bean id="exampleTwo" class="com.baeldung.ExampleTwoController" />
|
|
||||||
|
|
||||||
|
|
||||||
</beans>
|
</beans>
|
Loading…
Reference in New Issue