fixed handlerMapping code and added tests

This commit is contained in:
Yasser Afifi 2017-01-04 13:10:02 +00:00
parent 31b74a996c
commit f723114128
12 changed files with 190 additions and 25 deletions

View File

@ -6,12 +6,12 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class WelcomeBaeldungController extends AbstractController {
public class BaeldungController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Inside Welcome Baeldung Controller");
ModelAndView model = new ModelAndView("welcome");
ModelAndView model = new ModelAndView("baeldung");
return model;
}

View File

@ -6,12 +6,12 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class WelcomeTwoController extends AbstractController {
public class TestController extends AbstractController {
@Override
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("test");
return model;
}

View File

@ -25,7 +25,7 @@ public class WebAppConfiguration_BeanNameUrlHandlerMapping {
return bean;
}
@Bean("/welcome")
@Bean("/beanNameUrl")
public WelcomeController welcome() {
WelcomeController welcome = new WelcomeController();
return welcome;

View File

@ -9,9 +9,9 @@ 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.BaeldungController;
import com.baeldung.controller.WelcomeController;
import com.baeldung.controller.WelcomeTwoController;
import com.baeldung.controller.TestController;
@Configuration
public class WebAppConfiguration_ConfiguringPriorities {
@ -24,32 +24,32 @@ public class WebAppConfiguration_ConfiguringPriorities {
}
@Bean("/welcome")
public WelcomeBaeldungController welcomeBaeldungController() {
WelcomeBaeldungController welcomeBaeldungController = new WelcomeBaeldungController();
return welcomeBaeldungController;
public BaeldungController welcomeBaeldungController() {
BaeldungController baeldungController = new BaeldungController();
return baeldungController;
}
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
Map<String, Object> urlMap = new HashMap<>();
urlMap.put("/welcome", welcome());
urlMap.put("/welcome", test());
simpleUrlHandlerMapping.setUrlMap(urlMap);
simpleUrlHandlerMapping.setOrder(0);
return simpleUrlHandlerMapping;
}
@Bean
public WelcomeTwoController welcome() {
WelcomeTwoController bean = new WelcomeTwoController();
return bean;
public TestController test() {
TestController test = new TestController();
return test;
}
@Bean
public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() {
ControllerClassNameHandlerMapping controllerClassNameHandlerMapping = new ControllerClassNameHandlerMapping();
controllerClassNameHandlerMapping.setOrder(1);
return controllerClassNameHandlerMapping;
ControllerClassNameHandlerMapping bean = new ControllerClassNameHandlerMapping();
bean.setOrder(1);
return bean;
}
@Bean

View File

@ -27,7 +27,7 @@ public class WebAppConfiguration_SimpleUrlHandlerMapping {
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
Map<String, Object> urlMap = new HashMap<>();
urlMap.put("/welcome", welcome());
urlMap.put("/simpleUrlWelcome", welcome());
simpleUrlHandlerMapping.setUrlMap(urlMap);
return simpleUrlHandlerMapping;
}

View File

@ -23,7 +23,7 @@
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean>
<bean name="welcome" class="com.baeldung.WelcomeController" />
<bean name="beanNameUrl" class="com.baeldung.WelcomeController" />

View File

@ -17,19 +17,19 @@
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="2" />
</bean>
<bean name="/welcome" class="com.baeldung.WelcomeController" />
<bean name="/welcome" class="com.baeldung.BaeldungController" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/welcome=welcome
/*/welcome=welcome
/welcome=test
/*/welcome=test
</value>
</property>
<property name="order" value="0" />
</bean>
<bean id="welcome" class="com.baeldung.WelcomeTwoController" />
<bean id="test" class="com.baeldung.TestController" />
<bean

View File

@ -25,8 +25,8 @@
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/test=welcome
/*/test=welcome
/simpleUrlWelcome=welcome
/*/simpleUrlWelcome=welcome
</value>
</property>
</bean>

View File

@ -0,0 +1,42 @@
package come.baeldung.test;
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;
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 com.baeldung.config.WebAppConfiguration_BeanNameUrlHandlerMapping;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = WebAppConfiguration_BeanNameUrlHandlerMapping.class)
public class BeanNameMappingConfigTest {
@Autowired
private WebApplicationContext webAppContext;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
}
@Test
public void whenBeanNameMapping_thenMappedOK() throws Exception {
mockMvc.perform(get("/beanNameUrl")).andExpect(status().isOk()).andExpect(view().name("welcome")).andDo(print());
}
}

View File

@ -0,0 +1,41 @@
package come.baeldung.test;
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;
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 com.baeldung.config.WebAppConfiguration_ControllerClassNameHandlerMapping;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = WebAppConfiguration_ControllerClassNameHandlerMapping.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("/welcometest")).andExpect(status().isOk()).andExpect(view().name("welcome")).andDo(print());
}
}

View File

@ -0,0 +1,41 @@
package come.baeldung.test;
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;
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 com.baeldung.config.WebAppConfiguration_ConfiguringPriorities;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = WebAppConfiguration_ConfiguringPriorities.class)
public class HandlerMappingPriorityConfigTest {
@Autowired
private WebApplicationContext webAppContext;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
}
@Test
public void whenBeanNameMapping_thenMappedOK() throws Exception {
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print());
}
}

View File

@ -0,0 +1,41 @@
package come.baeldung.test;
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;
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 com.baeldung.config.WebAppConfiguration_SimpleUrlHandlerMapping;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = WebAppConfiguration_SimpleUrlHandlerMapping.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("welcome")).andDo(print());
}
}