BAEL-97 - adding handler mapping examples
This commit is contained in:
parent
4a96076ecf
commit
de3d64b86d
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.spring.web.config;
|
||||||
|
|
||||||
|
import com.baeldung.ExampleTwoController;
|
||||||
|
import com.baeldung.WelcomeController;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping;
|
||||||
|
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class ControllerClassNameHandlerMappingConfig {
|
||||||
|
|
||||||
|
@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 WelcomeController welcome() {
|
||||||
|
WelcomeController welcome = new WelcomeController();
|
||||||
|
return welcome;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ExampleTwoController exampleTwo() {
|
||||||
|
ExampleTwoController exampleTwo = new ExampleTwoController();
|
||||||
|
return exampleTwo;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.baeldung.spring.web.config;
|
||||||
|
|
||||||
|
import com.baeldung.ExampleTwoController;
|
||||||
|
import com.baeldung.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.SimpleUrlHandlerMapping;
|
||||||
|
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class SimpleUrlHandlerMappingConfig {
|
||||||
|
|
||||||
|
@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("/simpleUrlWelcome", welcome());
|
||||||
|
urlMap.put("/exampleTwo", exampleTwo());
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.web.controller.handlermapping;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
import org.springframework.web.servlet.mvc.AbstractController;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class ExampleTwoController extends AbstractController
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected ModelAndView handleRequestInternal(HttpServletRequest request,
|
||||||
|
HttpServletResponse response) throws Exception {
|
||||||
|
System.out.println("Inside ExampleTwo Controller");
|
||||||
|
|
||||||
|
ModelAndView model = new ModelAndView("exampleTwo");
|
||||||
|
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.web.controller.handlermapping;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
import org.springframework.web.servlet.mvc.AbstractController;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class WelcomeController extends AbstractController
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected ModelAndView handleRequestInternal(HttpServletRequest request,
|
||||||
|
HttpServletResponse response) throws Exception {
|
||||||
|
System.out.println("Inside BeanNameMappingExampleOne Controller");
|
||||||
|
|
||||||
|
ModelAndView model = new ModelAndView("test");
|
||||||
|
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
|
import com.baeldung.spring.web.config.ControllerClassNameHandlerMappingConfig;
|
||||||
|
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.*;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
@ContextConfiguration(classes = ControllerClassNameHandlerMappingConfig.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("/welcome")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
|
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 = SimpleUrlHandlerMappingConfig.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("test")).andDo(print());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue