• jsf学习笔记拦截器


    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 的重复提交拦截器,可惜效果不好,当作纪念吧

  • 相关阅读:
    解决windows上安装TortoiseSVN后不能使用命令行问题
    Python里Pure paths、PurePosixPath、PureWindowsPath的区别
    PHP数组运算符
    global,local,static的区别
    echo和print的区别
    PHP中foreach循环传值问题
    Matlab入门学习(文件读写)
    Matlab入门学习(程序设计)
    IDEA中使用Maven下载依赖时报错:unable to find valid certification path to requested target
    全国县市区编码表
  • 原文地址:https://www.cnblogs.com/cxyzl/p/2625532.html
Copyright © 2020-2023  润新知