SessionFlashMapManager.java 2.0 KB
Newer Older
1
/*
J
Juergen Hoeller 已提交
2
 * Copyright 2002-2014 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.servlet.support;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.FlashMap;

/**
27
 * Store and retrieve {@link FlashMap} instances to and from the HTTP session.
28 29 30 31
 *
 * @author Rossen Stoyanchev
 * @since 3.1.1
 */
J
Juergen Hoeller 已提交
32
public class SessionFlashMapManager extends AbstractFlashMapManager {
33 34 35

	private static final String FLASH_MAPS_SESSION_ATTRIBUTE = SessionFlashMapManager.class.getName() + ".FLASH_MAPS";

J
Juergen Hoeller 已提交
36

37
	/**
38 39 40 41
	 * Retrieve saved FlashMap instances from the HTTP Session.
	 * <p>Does not cause an HTTP session to be created but may update it if a
	 * FlashMap matching the current request is found or there are expired
	 * FlashMap to be removed.
42
	 */
43
	@Override
44 45 46
	@SuppressWarnings("unchecked")
	protected List<FlashMap> retrieveFlashMaps(HttpServletRequest request) {
		HttpSession session = request.getSession(false);
J
Juergen Hoeller 已提交
47
		return (session != null ? (List<FlashMap>) session.getAttribute(FLASH_MAPS_SESSION_ATTRIBUTE) : null);
48 49 50
	}

	/**
51
	 * Save the given FlashMap instance, if not empty, in the HTTP session.
52
	 */
53
	@Override
54 55 56 57 58
	protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest request, HttpServletResponse response) {
		request.getSession().setAttribute(FLASH_MAPS_SESSION_ATTRIBUTE, flashMaps);
	}

}