BAEL-97 - adding BeanNameMappingHandler

This commit is contained in:
slavisa-baeldung 2016-12-29 17:11:28 +01:00
parent de3d64b86d
commit b7b5463309
4 changed files with 86 additions and 4 deletions

View File

@ -0,0 +1,41 @@
package com.baeldung.spring.web.config;
import com.baeldung.web.controller.handlermapping.ExampleTwoController;
import com.baeldung.web.controller.handlermapping.WelcomeController;
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.mvc.support.ControllerClassNameHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
public class BeanNameHandlerMappingConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public BeanNameUrlHandlerMapping controllerClassNameHandlerMapping() {
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
return beanNameUrlHandlerMapping;
}
@Bean(name="/welcomeBean")
public WelcomeController welcomeBean() {
WelcomeController welcome = new WelcomeController();
return welcome;
}
@Bean(name="/ex")
public ExampleTwoController exampleTwo() {
ExampleTwoController exampleTwo = new ExampleTwoController();
return exampleTwo;
}
}

View File

@ -1,7 +1,7 @@
package com.baeldung.spring.web.config;
import com.baeldung.ExampleTwoController;
import com.baeldung.WelcomeController;
import com.baeldung.web.controller.handlermapping.ExampleTwoController;
import com.baeldung.web.controller.handlermapping.WelcomeController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

View File

@ -1,7 +1,7 @@
package com.baeldung.spring.web.config;
import com.baeldung.ExampleTwoController;
import com.baeldung.WelcomeController;
import com.baeldung.web.controller.handlermapping.ExampleTwoController;
import com.baeldung.web.controller.handlermapping.WelcomeController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;

View File

@ -0,0 +1,41 @@
package com.baeldung.web.controller;
import com.baeldung.spring.web.config.BeanNameHandlerMappingConfig;
import com.baeldung.spring.web.config.SimpleUrlHandlerMappingConfig;
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 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
@ContextConfiguration(classes = BeanNameHandlerMappingConfig.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("/welcomeBean")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print());
}
}