BAEL-97 - simplifying code

This commit is contained in:
slavisa-baeldung 2017-01-08 16:46:47 +01:00
parent e36d928219
commit adccc13c32
9 changed files with 120 additions and 37 deletions

View File

@ -0,0 +1,43 @@
package com.baeldung.spring.web.config;
import com.baeldung.web.controller.handlermapping.BeanNameHandlerMappingController;
import com.baeldung.web.controller.handlermapping.SimpleUrlMappingController;
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 java.util.HashMap;
import java.util.Map;
@Configuration
public class HandlerMappingCustomPrioritiesConfig {
@Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
beanNameUrlHandlerMapping.setOrder(0);
return beanNameUrlHandlerMapping;
}
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
Map<String, Object> urlMap = new HashMap<>();
urlMap.put("/welcome", simpleUrlMapping());
simpleUrlHandlerMapping.setUrlMap(urlMap);
simpleUrlHandlerMapping.setOrder(1);
return simpleUrlHandlerMapping;
}
@Bean
public SimpleUrlMappingController simpleUrlMapping() {
return new SimpleUrlMappingController();
}
@Bean("/welcome")
public BeanNameHandlerMappingController beanNameHandlerMapping() {
return new BeanNameHandlerMappingController();
}
}

View File

@ -3,14 +3,13 @@ package com.baeldung.spring.web.config;
import java.util.HashMap;
import java.util.Map;
import com.baeldung.web.controller.handlermapping.BaeldungController;
import com.baeldung.web.controller.handlermapping.TestController;
import com.baeldung.web.controller.handlermapping.SimpleUrlMappingController;
import com.baeldung.web.controller.handlermapping.BeanNameHandlerMappingController;
import com.baeldung.web.controller.handlermapping.WelcomeController;
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;
@Configuration
@ -19,7 +18,7 @@ public class HandlerMappingPrioritiesConfig {
@Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
beanNameUrlHandlerMapping.setOrder(2);
beanNameUrlHandlerMapping.setOrder(0);
return beanNameUrlHandlerMapping;
}
@ -27,31 +26,18 @@ public class HandlerMappingPrioritiesConfig {
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
Map<String, Object> urlMap = new HashMap<>();
urlMap.put("/welcome", test());
urlMap.put("/welcome", simpleUrlMapping());
simpleUrlHandlerMapping.setUrlMap(urlMap);
simpleUrlHandlerMapping.setOrder(0);
return simpleUrlHandlerMapping;
}
@Bean
public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() {
ControllerClassNameHandlerMapping bean = new ControllerClassNameHandlerMapping();
bean.setOrder(1);
return bean;
public SimpleUrlMappingController simpleUrlMapping() {
return new SimpleUrlMappingController();
}
@Bean("/welcome")
public BaeldungController welcomeBaeldungController() {
return new BaeldungController();
}
@Bean
public TestController test() {
return new TestController();
}
@Bean
public WelcomeController welcomeController() {
return new WelcomeController();
public BeanNameHandlerMappingController beanNameHandlerMapping() {
return new BeanNameHandlerMappingController();
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.spring.web.config;
import com.baeldung.web.controller.handlermapping.SimpleUrlMappingController;
import com.baeldung.web.controller.handlermapping.BeanNameHandlerMappingController;
import com.baeldung.web.controller.handlermapping.WelcomeController;
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 java.util.HashMap;
import java.util.Map;
@Configuration
public class HandlerMappingPrioritiesConfigNoOrder {
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
Map<String, Object> urlMap = new HashMap<>();
urlMap.put("/welcome", test());
simpleUrlHandlerMapping.setUrlMap(urlMap);
return simpleUrlHandlerMapping;
}
@Bean
public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() {
ControllerClassNameHandlerMapping bean = new ControllerClassNameHandlerMapping();
return bean;
}
@Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
return beanNameUrlHandlerMapping;
}
@Bean("/welcome")
public SimpleUrlMappingController welcomeBaeldungController() {
return new SimpleUrlMappingController();
}
@Bean
public BeanNameHandlerMappingController test() {
return new BeanNameHandlerMappingController();
}
@Bean
public WelcomeController welcomeController() {
return new WelcomeController();
}
}

View File

@ -6,10 +6,10 @@ import org.springframework.web.servlet.mvc.AbstractController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestController extends AbstractController {
public class BeanNameHandlerMappingController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("test");
ModelAndView model = new ModelAndView("bean-name-handler-mapping");
return model;
}

View File

@ -6,10 +6,10 @@ import org.springframework.web.servlet.mvc.AbstractController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BaeldungController extends AbstractController {
public class SimpleUrlMappingController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("baeldung");
ModelAndView model = new ModelAndView("simple-url-handler-mapping");
return model;
}

View File

@ -1,4 +1,4 @@
package com.baeldung.handlermapping;
package com.baeldung.handlermappings;
import com.baeldung.spring.web.config.BeanNameUrlHandlerMappingConfig;
import org.junit.Before;

View File

@ -1,4 +1,4 @@
package com.baeldung.handlermapping;
package com.baeldung.handlermappings;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

View File

@ -1,10 +1,7 @@
package com.baeldung.handlermapping;
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;
package com.baeldung.handlermappings;
import com.baeldung.spring.web.config.HandlerMappingCustomPrioritiesConfig;
import com.baeldung.spring.web.config.HandlerMappingPrioritiesConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -17,7 +14,10 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.spring.web.config.HandlerMappingPrioritiesConfig;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ -36,6 +36,6 @@ public class HandlerMappingPriorityConfigTest {
@Test
public void whenConfiguringPriorities_thenMappedOK() throws Exception {
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print());
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("bean-name-handler-mapping")).andDo(print());
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.handlermapping;
package com.baeldung.handlermappings;
import com.baeldung.spring.web.config.SimpleUrlHandlerMappingConfig;
import org.junit.Before;