Java 类org.springframework.web.servlet.support.SessionFlashMapManager 实例源码

项目:class-guard    文件:StandaloneMockMvcBuilder.java   
private void registerMvcSingletons(StubWebApplicationContext cxt) {

        StandaloneConfiguration configuration = new StandaloneConfiguration();

        RequestMappingHandlerMapping handlerMapping = configuration.requestMappingHandlerMapping();
        handlerMapping.setServletContext(cxt.getServletContext());
        handlerMapping.setApplicationContext(cxt);
        cxt.addBean("requestMappingHandlerMapping", handlerMapping);

        RequestMappingHandlerAdapter handlerAdapter = configuration.requestMappingHandlerAdapter();
        handlerAdapter.setServletContext(cxt.getServletContext());
        handlerAdapter.setApplicationContext(cxt);
        handlerAdapter.afterPropertiesSet();
        cxt.addBean("requestMappingHandlerAdapter", handlerAdapter);

        cxt.addBean("handlerExceptionResolver", configuration.handlerExceptionResolver());

        cxt.addBeans(initViewResolvers(cxt));
        cxt.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver);
        cxt.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver());
        cxt.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator());

        this.flashMapManager = new SessionFlashMapManager();
        cxt.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager);
    }
项目:spring4-understanding    文件:RedirectViewUriTemplateTests.java   
@Before
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());
}
项目:spring4-understanding    文件:RedirectViewTests.java   
@Test
public void http11() throws Exception {
    RedirectView rv = new RedirectView();
    rv.setUrl("http://url.somewhere.com");
    rv.setHttp10Compatible(false);
    MockHttpServletRequest request = createRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
    request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
    rv.render(new HashMap<String, Object>(), request, response);
    assertEquals(303, response.getStatus());
    assertEquals("http://url.somewhere.com", response.getHeader("Location"));
}
项目:spring4-understanding    文件:StandaloneMockMvcBuilder.java   
private void registerMvcSingletons(StubWebApplicationContext wac) {
    StandaloneConfiguration config = new StandaloneConfiguration();
    config.setApplicationContext(wac);

    wac.addBeans(this.controllerAdvice);

    StaticRequestMappingHandlerMapping hm = config.getHandlerMapping();
    hm.setServletContext(wac.getServletContext());
    hm.setApplicationContext(wac);
    hm.afterPropertiesSet();
    hm.registerHandlers(this.controllers);
    wac.addBean("requestMappingHandlerMapping", hm);

    RequestMappingHandlerAdapter handlerAdapter = config.requestMappingHandlerAdapter();
    handlerAdapter.setServletContext(wac.getServletContext());
    handlerAdapter.setApplicationContext(wac);
    handlerAdapter.afterPropertiesSet();
    wac.addBean("requestMappingHandlerAdapter", handlerAdapter);

    wac.addBean("handlerExceptionResolver", config.handlerExceptionResolver());

    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);
}
项目:spring4-understanding    文件:MockHttpServletRequestBuilderTests.java   
@Test
public void flashAttribute() {
    this.builder.flashAttr("foo", "bar");
    MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);

    FlashMap flashMap = new SessionFlashMapManager().retrieveAndUpdate(request, null);
    assertNotNull(flashMap);
    assertEquals("bar", flashMap.get("foo"));
}
项目:motech    文件:FlashMapInterceptor.java   
@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;
}
项目:class-guard    文件:MockHttpServletRequestBuilderTests.java   
@Test
public void flashAttribute() throws Exception {
    this.builder.flashAttr("foo", "bar");
    MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);

    FlashMap flashMap = new SessionFlashMapManager().retrieveAndUpdate(request, null);
    assertNotNull(flashMap);
    assertEquals("bar", flashMap.get("foo"));
}
项目:class-guard    文件:RedirectViewUriTemplateTests.java   
@Before
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());
}
项目:class-guard    文件:RedirectViewTests.java   
@Test
public void http11() throws Exception {
    RedirectView rv = new RedirectView();
    rv.setUrl("http://url.somewhere.com");
    rv.setHttp10Compatible(false);
    MockHttpServletRequest request = createRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
    request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
    rv.render(new HashMap<String, Object>(), request, response);
    assertEquals(303, response.getStatus());
    assertEquals("http://url.somewhere.com", response.getHeader("Location"));
}
项目:spring4-understanding    文件:RedirectViewTests.java   
private MockHttpServletRequest createRequest() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
    request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
    return request;
}
项目:spring4-understanding    文件:RedirectViewTests.java   
private void doTest(final Map<String, ?> map, final String url, final boolean contextRelative,
        final boolean exposeModelAttributes, String expectedUrlForEncoding) throws Exception {

    class TestRedirectView extends RedirectView {

        public boolean queryPropertiesCalled = false;

        /**
         * Test whether this callback method is called with correct args
         */
        @Override
        protected Map<String, Object> queryProperties(Map<String, Object> model) {
            // They may not be the same model instance, but they're still equal
            assertTrue("Map and model must be equal.", map.equals(model));
            this.queryPropertiesCalled = true;
            return super.queryProperties(model);
        }
    }

    TestRedirectView rv = new TestRedirectView();
    rv.setUrl(url);
    rv.setContextRelative(contextRelative);
    rv.setExposeModelAttributes(exposeModelAttributes);

    HttpServletRequest request = mock(HttpServletRequest.class, "request");
    if (exposeModelAttributes) {
        given(request.getCharacterEncoding()).willReturn(WebUtils.DEFAULT_CHARACTER_ENCODING);
    }
    if (contextRelative) {
        expectedUrlForEncoding = "/context" + expectedUrlForEncoding;
        given(request.getContextPath()).willReturn("/context");
    }

    given(request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE)).willReturn(new FlashMap());

    FlashMapManager flashMapManager = new SessionFlashMapManager();
    given(request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE)).willReturn(flashMapManager);

    HttpServletResponse response = mock(HttpServletResponse.class, "response");
    given(response.encodeRedirectURL(expectedUrlForEncoding)).willReturn(expectedUrlForEncoding);
    response.sendRedirect(expectedUrlForEncoding);

    rv.render(map, request, response);
    if (exposeModelAttributes) {
        assertTrue("queryProperties() should have been called.", rv.queryPropertiesCalled);
    }
}
项目:class-guard    文件:RedirectViewTests.java   
private MockHttpServletRequest createRequest() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
    request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
    return request;
}
项目:class-guard    文件:RedirectViewTests.java   
private void doTest(final Map<String, ?> map, final String url, final boolean contextRelative,
        final boolean exposeModelAttributes, String expectedUrlForEncoding) throws Exception {

    class TestRedirectView extends RedirectView {

        public boolean queryPropertiesCalled = false;

        /**
         * Test whether this callback method is called with correct args
         */
        @Override
        protected Map<String, Object> queryProperties(Map<String, Object> model) {
            // They may not be the same model instance, but they're still equal
            assertTrue("Map and model must be equal.", map.equals(model));
            this.queryPropertiesCalled = true;
            return super.queryProperties(model);
        }
    }

    TestRedirectView rv = new TestRedirectView();
    rv.setUrl(url);
    rv.setContextRelative(contextRelative);
    rv.setExposeModelAttributes(exposeModelAttributes);

    HttpServletRequest request = mock(HttpServletRequest.class, "request");
    if (exposeModelAttributes) {
        given(request.getCharacterEncoding()).willReturn(WebUtils.DEFAULT_CHARACTER_ENCODING);
    }
    if (contextRelative) {
        expectedUrlForEncoding = "/context" + expectedUrlForEncoding;
        given(request.getContextPath()).willReturn("/context");
    }

    given(request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE)).willReturn(new FlashMap());

    FlashMapManager flashMapManager = new SessionFlashMapManager();
    given(request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE)).willReturn(flashMapManager);

    HttpServletResponse response = mock(HttpServletResponse.class, "response");
    given(response.encodeRedirectURL(expectedUrlForEncoding)).willReturn(expectedUrlForEncoding);
    response.sendRedirect(expectedUrlForEncoding);

    rv.render(map, request, response);
    if (exposeModelAttributes) {
        assertTrue("queryProperties() should have been called.", rv.queryPropertiesCalled);
    }
}