Search in sources :

Example 1 with SessionFlashMapManager

use of org.springframework.web.servlet.support.SessionFlashMapManager in project motech by motech.

the class FlashMapInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    Object flashMapObj = RequestContextUtils.getOutputFlashMap(request);
    if (flashMapObj != null && !(flashMapObj instanceof FlashMap)) {
        // we need to create a FlashMap using the webapp classLoader
        FlashMap flashMapCopy = new FlashMap();
        flashMapCopy.putAll((Map<? extends String, ?>) flashMapObj);
        // then put it in the request
        SessionFlashMapManager sessionFlashMapManager = new SessionFlashMapManager();
        sessionFlashMapManager.saveOutputFlashMap(flashMapCopy, request, response);
    }
    return true;
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager)

Example 2 with SessionFlashMapManager

use of org.springframework.web.servlet.support.SessionFlashMapManager in project spring-framework by spring-projects.

the class MockHttpServletRequestBuilderTests method flashAttribute.

@Test
void flashAttribute() {
    this.builder.flashAttr("foo", "bar");
    MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
    FlashMap flashMap = new SessionFlashMapManager().retrieveAndUpdate(request, null);
    assertThat((Object) flashMap).isNotNull();
    assertThat(flashMap.get("foo")).isEqualTo("bar");
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) Test(org.junit.jupiter.api.Test)

Example 3 with SessionFlashMapManager

use of org.springframework.web.servlet.support.SessionFlashMapManager in project spring-framework by spring-projects.

the class StandaloneMockMvcBuilder method registerMvcSingletons.

private void registerMvcSingletons(StubWebApplicationContext wac) {
    StandaloneConfiguration config = new StandaloneConfiguration();
    config.setApplicationContext(wac);
    ServletContext sc = wac.getServletContext();
    wac.addBeans(this.controllers);
    wac.addBeans(this.controllerAdvice);
    FormattingConversionService mvcConversionService = config.mvcConversionService();
    wac.addBean("mvcConversionService", mvcConversionService);
    ResourceUrlProvider resourceUrlProvider = config.mvcResourceUrlProvider();
    wac.addBean("mvcResourceUrlProvider", resourceUrlProvider);
    ContentNegotiationManager mvcContentNegotiationManager = config.mvcContentNegotiationManager();
    wac.addBean("mvcContentNegotiationManager", mvcContentNegotiationManager);
    Validator mvcValidator = config.mvcValidator();
    wac.addBean("mvcValidator", mvcValidator);
    RequestMappingHandlerMapping hm = config.getHandlerMapping(mvcConversionService, resourceUrlProvider);
    if (sc != null) {
        hm.setServletContext(sc);
    }
    hm.setApplicationContext(wac);
    hm.afterPropertiesSet();
    wac.addBean("requestMappingHandlerMapping", hm);
    RequestMappingHandlerAdapter ha = config.requestMappingHandlerAdapter(mvcContentNegotiationManager, mvcConversionService, mvcValidator);
    if (sc != null) {
        ha.setServletContext(sc);
    }
    ha.setApplicationContext(wac);
    ha.afterPropertiesSet();
    wac.addBean("requestMappingHandlerAdapter", ha);
    wac.addBean("handlerExceptionResolver", config.handlerExceptionResolver(mvcContentNegotiationManager));
    wac.addBeans(initViewResolvers(wac));
    wac.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver);
    wac.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver());
    wac.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator());
    this.flashMapManager = new SessionFlashMapManager();
    wac.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager);
    extendMvcSingletons(sc).forEach(wac::addBean);
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) DefaultRequestToViewNameTranslator(org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Validator(org.springframework.validation.Validator) ResourceUrlProvider(org.springframework.web.servlet.resource.ResourceUrlProvider)

Example 4 with SessionFlashMapManager

use of org.springframework.web.servlet.support.SessionFlashMapManager in project spring-framework by spring-projects.

the class RedirectViewTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    this.request = new MockHttpServletRequest();
    this.request.setContextPath("/context");
    this.request.setCharacterEncoding(WebUtils.DEFAULT_CHARACTER_ENCODING);
    this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
    this.request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
    this.response = new MockHttpServletResponse();
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with SessionFlashMapManager

use of org.springframework.web.servlet.support.SessionFlashMapManager in project spring-framework by spring-projects.

the class RedirectViewUriTemplateTests method setUp.

@BeforeEach
public void setUp() {
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
    this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
    this.request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

SessionFlashMapManager (org.springframework.web.servlet.support.SessionFlashMapManager)6 FlashMap (org.springframework.web.servlet.FlashMap)4 BeforeEach (org.junit.jupiter.api.BeforeEach)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2 ServletContext (jakarta.servlet.ServletContext)1 ServletContext (javax.servlet.ServletContext)1 Test (org.junit.jupiter.api.Test)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)1 FormattingConversionService (org.springframework.format.support.FormattingConversionService)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockServletContext (org.springframework.mock.web.MockServletContext)1 Validator (org.springframework.validation.Validator)1 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1 FlashMapManager (org.springframework.web.servlet.FlashMapManager)1 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)1 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)1 ResourceUrlProvider (org.springframework.web.servlet.resource.ResourceUrlProvider)1