added modified HandlerMapping code

This commit is contained in:
Yasser Afifi 2017-01-01 18:43:25 +00:00
parent f8edea40ae
commit 31b74a996c
12 changed files with 246 additions and 98 deletions

View File

@ -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));

View File

@ -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;
}
}

View File

@ -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;
} }

View File

@ -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;
} }
} }

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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,6 +6,8 @@ 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 {
@ -29,10 +31,4 @@ public class WebAppConfiguration_ControllerClassNameHandlerMapping {
return welcome; return welcome;
} }
@Bean
public ExampleTwoController exampleTwo() {
ExampleTwoController exampleTwo = new ExampleTwoController();
return exampleTwo;
}
} }

View File

@ -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,6 +9,9 @@ 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 {
@ -25,7 +28,6 @@ public class WebAppConfiguration_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;
} }
@ -36,10 +38,4 @@ public class WebAppConfiguration_SimpleUrlHandlerMapping {
return welcome; return welcome;
} }
@Bean
public ExampleTwoController exampleTwo() {
ExampleTwoController exampleTwo = new ExampleTwoController();
return exampleTwo;
}
} }

View File

@ -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" />

View File

@ -28,7 +28,6 @@
<!-- register beans --> <!-- register beans -->
<bean class="com.baeldung.WelcomeController" /> <bean class="com.baeldung.WelcomeController" />
<bean class="com.baeldung.ExampleTwoController" />

View File

@ -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>

View File

@ -5,6 +5,7 @@
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
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc
@ -13,6 +14,7 @@
http://www.springframework.org/schema/cache/spring-cache.xsd"> http://www.springframework.org/schema/cache/spring-cache.xsd">
<bean <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" /> <property name="prefix" value="/" />
@ -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>