Created
September 24, 2014 03:05
-
-
Save fankay/ecb3c0316493e113fc0c to your computer and use it in GitHub Desktop.
使用 Filter实现 FlashAttribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kaishengit.web.filter; | |
import java.io.IOException; | |
import java.util.Enumeration; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import javax.servlet.Filter; | |
import javax.servlet.FilterChain; | |
import javax.servlet.FilterConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletRequest; | |
import javax.servlet.ServletResponse; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpSession; | |
public class FlashScopeFilter implements Filter { | |
private static final String FLASH_SESSION_SCOPE = "flash_session_scope"; | |
@Override | |
public void destroy() { | |
} | |
@Override | |
public void doFilter(ServletRequest req, ServletResponse resp, | |
FilterChain chain) throws IOException, ServletException { | |
HttpServletRequest request = (HttpServletRequest) req; | |
HttpSession session = request.getSession(); | |
System.out.println("before"); | |
@SuppressWarnings("unchecked") | |
Map<String, Object> sessionParams = (Map<String, Object>) session.getAttribute(FLASH_SESSION_SCOPE); | |
if(sessionParams != null) { | |
for(Entry<String, Object> entry : sessionParams.entrySet()) { | |
request.setAttribute(entry.getKey(), entry.getValue()); | |
} | |
session.removeAttribute(FLASH_SESSION_SCOPE); | |
} | |
chain.doFilter(req, resp); | |
System.out.println("after"); | |
Map<String, Object> flashParams = new HashMap<String, Object>(); | |
Enumeration<String> attrNames = request.getAttributeNames(); | |
while(attrNames.hasMoreElements()) { | |
String attrName = attrNames.nextElement(); | |
if(attrName.startsWith("flash.")) { | |
flashParams.put(attrName.substring(attrName.indexOf(".")+1), request.getAttribute(attrName)); | |
} | |
} | |
if(!flashParams.isEmpty()) { | |
session.setAttribute(FLASH_SESSION_SCOPE, flashParams); | |
} | |
} | |
@Override | |
public void init(FilterConfig arg0) throws ServletException { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=UTF-8" | |
pageEncoding="UTF-8"%> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>Insert title here</title> | |
</head> | |
<body> | |
提示消息:${message} | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@WebServlet("/save.do") | |
public class SaveServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
request.getRequestDispatcher("/WEB-INF/views/save.jsp").forward(request, response); | |
} | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
String money = request.getParameter("money"); | |
//以 flash 开头的都会被传递 | |
request.setAttribute("flash.message", "支付成功"); | |
response.sendRedirect("/suc.do"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment