Sping,Hibernateでリクエストにまたがってメッセージを渡す


        url        redirect,           ,       : 


package com.founder.bcimp.core.filter;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.support.DelegatingMessageSource;
import org.springframework.web.servlet.support.RequestContextUtils;

public final class FlashScope {
	
	static final String FLASH_SCOPE_ATTRIBUTE = FlashScope.class.getName();
	
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getCurrent(HttpServletRequest request) {
		HttpSession session = request.getSession(); 
		Map flash = (Map) session.getAttribute(FLASH_SCOPE_ATTRIBUTE);
		if (flash == null) {
			flash = new HashMap();
			session.setAttribute(FLASH_SCOPE_ATTRIBUTE, flash);
		}
		return flash;
	}
	
	private FlashScope() {
	}
	
	/**
	 * Gets the international message
	 * 
	 * @param request HttpServletRequest
	 * @param messageSource DelegatingMessageSource
	 * @param key String
	 * @return the international message
	 * @author Jiang Haiying
	 */
	private static String getLocalMessage(HttpServletRequest request, DelegatingMessageSource messageSource, String key) {
		
		String message = messageSource.getMessage(key, null, RequestContextUtils.getLocale(request));
		return message;
	}

	/**
	 * Gets the international message
	 * 
	 * @param request HttpServletRequest
	 * @param messageSource DelegatingMessageSource
	 * @param key String
	 * @author Jiang Haiying
	 */
	public static void internationalMessage(HttpServletRequest request, DelegatingMessageSource messageSource, String message) {
		FlashScope.getCurrent(request).put("message", getLocalMessage(request, messageSource, message));
	}
	
}

package com.founder.bcimp.core.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.filter.OncePerRequestFilter;

public class FlashScopeFilter extends OncePerRequestFilter {

	@Override
	@SuppressWarnings("unchecked")
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {
		HttpSession session = request.getSession(false);
		if (session != null) {
			Map<String, ?> flash = (Map<String, ?>) session.getAttribute(FlashScope.FLASH_SCOPE_ATTRIBUTE);
			if (flash != null) {
				for (Map.Entry<String, ?> entry : flash.entrySet()) {
					Object currentValue = request.getAttribute(entry.getKey());
					if (currentValue == null) {
						request.setAttribute(entry.getKey(), entry.getValue());
					}					
				}
				session.removeAttribute(FlashScope.FLASH_SCOPE_ATTRIBUTE);
			}
		}
		filterChain.doFilter(request, response);
	}
}


は のとおりです.

@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String register(CRSCompound compound, Integer compoundSaltId, Integer notebookId,
			Integer unitsId, Integer projectId, Integer submitterId, Integer saltCoeff,
			Integer appearanceId, HttpServletRequest request, ModelMap model){
		
		String forward = "redirect:/crs/compound/";
		String message = "bcimp.save.success.message";
		
		if (compound.getId() != null) {
			message = "bcimp.edit.success.message";
		}
		
		try {
			
			
			if(compoundService.registerCompound(compound) == null && compound.getId() == null) {
				message = "bcimp.save.failure.message";
			}
			if(compoundService.registerCompound(compound) == null && compound.getId() != null) {
				message = "bcimp.edit.failure.message";
			}
			
		} catch (LogicException e) {
			logger.error(e.getMessage(), e);
		}
		if (compound.getId() != null) {
			forward = forward + compound.getId();
		} else {
			//TODO
			//registration failed
		}

		FlashScope.internationalMessage(request, messageSource, message);
		return forward;
	}