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.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController; import org.springframework.web.servlet.mvc.AbstractController;
public class WelcomeBaeldungController extends AbstractController { public class BaeldungController extends AbstractController {
@Override @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Inside Welcome Baeldung Controller"); System.out.println("Inside Welcome Baeldung Controller");
ModelAndView model = new ModelAndView("welcome"); ModelAndView model = new ModelAndView("baeldung");
return model; return model;
} }

View File

@ -6,12 +6,12 @@ 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 WelcomeTwoController extends AbstractController { public class TestController extends AbstractController {
@Override @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Inside Welcome two Controller"); System.out.println("Inside Welcome two Controller");
ModelAndView model = new ModelAndView("welcome"); ModelAndView model = new ModelAndView("test");
return model; return model;
} }

View File

@ -25,7 +25,7 @@ public class WebAppConfiguration_BeanNameUrlHandlerMapping {
return bean; return bean;
} }
@Bean("/welcome") @Bean("/beanNameUrl")
public WelcomeController welcome() { public WelcomeController welcome() {
WelcomeController welcome = new WelcomeController(); WelcomeController welcome = new WelcomeController();
return welcome; 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.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping; 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.WelcomeController;
import com.baeldung.controller.WelcomeTwoController; import com.baeldung.controller.TestController;
@Configuration @Configuration
public class WebAppConfiguration_ConfiguringPriorities { public class WebAppConfiguration_ConfiguringPriorities {
@ -24,32 +24,32 @@ public class WebAppConfiguration_ConfiguringPriorities {
} }
@Bean("/welcome") @Bean("/welcome")
public WelcomeBaeldungController welcomeBaeldungController() { public BaeldungController welcomeBaeldungController() {
WelcomeBaeldungController welcomeBaeldungController = new WelcomeBaeldungController(); BaeldungController baeldungController = new BaeldungController();
return welcomeBaeldungController; return baeldungController;
} }
@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", test());
simpleUrlHandlerMapping.setUrlMap(urlMap); simpleUrlHandlerMapping.setUrlMap(urlMap);
simpleUrlHandlerMapping.setOrder(0); simpleUrlHandlerMapping.setOrder(0);
return simpleUrlHandlerMapping; return simpleUrlHandlerMapping;
} }
@Bean @Bean
public WelcomeTwoController welcome() { public TestController test() {
WelcomeTwoController bean = new WelcomeTwoController(); TestController test = new TestController();
return bean; return test;
} }
@Bean @Bean
public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() { public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() {
ControllerClassNameHandlerMapping controllerClassNameHandlerMapping = new ControllerClassNameHandlerMapping(); ControllerClassNameHandlerMapping bean = new ControllerClassNameHandlerMapping();
controllerClassNameHandlerMapping.setOrder(1); bean.setOrder(1);
return controllerClassNameHandlerMapping; return bean;
} }
@Bean @Bean

View File

@ -27,7 +27,7 @@ public class WebAppConfiguration_SimpleUrlHandlerMapping {
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("/simpleUrlWelcome", welcome());
simpleUrlHandlerMapping.setUrlMap(urlMap); simpleUrlHandlerMapping.setUrlMap(urlMap);
return simpleUrlHandlerMapping; return simpleUrlHandlerMapping;
} }

View File

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

View File

@ -25,8 +25,8 @@
<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=welcome /simpleUrlWelcome=welcome
/*/test=welcome /*/simpleUrlWelcome=welcome
</value> </value>
</property> </property>
</bean> </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());
}
}