`
jhyimu2005
  • 浏览: 181608 次
  • 性别: Icon_minigender_2
  • 来自: 苏州
社区版块
存档分类
最新评论

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;
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics