Sping,Hibernate中跨请求传递消息

[/code]在项目中使用注解url进行访问事用到了redirect,结果发现其信息就丢失了,其解决方案如下:
[code="java"]
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值