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.ServletException;
@ -8,12 +8,14 @@ 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));

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.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("test");
ModelAndView model = new ModelAndView("welcome");
return model;
}

View File

@ -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("exampleTwo");
ModelAndView model = new ModelAndView("welcome");
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.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;
}
}

View File

@ -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<String, Object> 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<String, Object> 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;
}
}

View File

@ -25,8 +25,6 @@
<bean name="welcome" class="com.baeldung.WelcomeController" />
<bean name="/exampleTwo" class="com.baeldung.ExampleTwoController" />

View File

@ -25,10 +25,9 @@
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
</bean>
<!-- register beans -->
<!-- register beans -->
<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

@ -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">
<bean
@ -23,16 +25,14 @@
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/test=firstController
/*/test=firstController
/exampleTwo=exampleTwoController
/test=welcome
/*/test=welcome
</value>
</property>
</bean>
<bean id="welcome" class="com.baeldung.WelcomeController" />
<bean id="exampleTwo" class="com.baeldung.ExampleTwoController" />
</beans>