zoukankan      html  css  js  c++  java
  • 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 的重复提交拦截器,可惜效果不好,当作纪念吧

  • 相关阅读:
    naturalWidth、naturalHeight来获取图片的真实宽高
    网站访问量等数据统计
    电话号码中间四位用****代替
    前端通过url页面传值
    前端存取cookie
    SQL Server 硬件和软件要求
    sql server 2017安装
    Js小知识及一些常见易混淆的知识点
    前端学习资料汇总
    短信验证码
  • 原文地址:https://www.cnblogs.com/cxyzl/p/2625532.html
Copyright © 2011-2022 走看看