package com.tocken.listener;
import java.util.Map;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpSession;
public class TockenPhaseListener implements PhaseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void afterPhase(PhaseEvent pe) {
if(pe.getPhaseId()==PhaseId.INVOKE_APPLICATION ){
System.out.println("action逻辑处理之后要做的事");
FacesContext facesContext = pe.getFacesContext();
ExternalContext context =facesContext.getExternalContext();
HttpSession session = (HttpSession) context.getSession(true);
Map map = context.getRequestParameterMap();
String tockenName=map.get("tockenName").toString();
session.removeAttribute(tockenName);
}
}
@Override
public void beforePhase(PhaseEvent pe) {
if(pe.getPhaseId()==PhaseId.APPLY_REQUEST_VALUES ){
System.out.println("action逻辑处理之前要做的事");
//把session里的值取出来比较
FacesContext facesContext = pe.getFacesContext();
ExternalContext context =facesContext.getExternalContext();
HttpSession session = (HttpSession) context.getSession(true);
Map map = context.getRequestParameterMap();
String tockenName=map.get("tockenName").toString();
String tockenValue= map.get("tockenValue").toString();
if(session.getAttribute(tockenName)==null){
session.setAttribute(tockenName, tockenValue);
}
else{
String tockenVar = session.getAttribute(tockenName).toString();
if(tockenValue.equals(tockenVar)){
System.out.println("你被拦截了");
facesContext.renderResponse();
}
}
}
}
@Override
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
}
写的通过PhaseListener 的重复提交拦截器,可惜效果不好,当作纪念吧