• Struts2 拦截器 及如何获得 servlet 请求对象 以及Struts 基本配置 &&Session 超时设置


    在拦截器中可以三种实现

    一:继承 AbstractInterceptor 类
    二:继承 MethodFilterInterceptor类
    三:实现 Interceptor 接口

    在实现Interceptor接口时,会多出init() 和 destroy() 方法

    拦截器的主体方法是:
    public String intercept(ActionInvocation arg0) throws Exception {
    // TODO Auto-generated method stub

    HttpServletRequest request = ServletActionContext.getRequest();
    if(request.getParameter("username") == null){//输入的用户名为空
    return "logFail";
    }
    logInterceptor.invoke();
    return null;


    }

    在拦截器完成任务后-->记得 arg0.invoke()  
    激活action,让程序得以继续执行

    另外:
    <form action="logAction_login.action" method="post">
    在struts.xml中<action name="logAction_*" class="my.test.Control" method="{1}"></action>
    指定跳转的处理类的处理方法

    付一张必须jar包图:

     通过非IOC 方式 获取 http请求参数
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletRespons respons = ServletActionContext.getRespons();
    HttpSession session  = ServletActionContext.getRequest().getSession();//servlet 中的底层session
    Map sessionMap = ActionContext.getContext().getSession(); //struts2 中封装的Map session

    ------------------------------------------------------------------------------------------>

    <struts>
    <constant name="struts.ui.theme" value="simple" />
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.objectFactory.spring.autoWire" value="name" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.custom.i18n.resources" value="messages,department,serch,device,login,user,role,ywtj,param,notice,hmd,jrs,jwjk"/>
    <include file="struts-login.xml" />
    </struts>

    ----------------------------------------------------------------------------------------> 

     判断Session 是否超时:

    public class SessionInterceptor extends BaseAction implements Interceptor    {


    private static final long serialVersionUID = 1L;
    public String intercept(ActionInvocation actionInvocation) throws Exception {
    HttpSession session  = ServletActionContext.getRequest().getSession();
    Action action = (Action) actionInvocation.getAction();
    if (action instanceof LoginAction) {
    return actionInvocation.invoke();
    }else{
    SysUser user = UserSessionUtil.getUserInfo(session);
    if (user == null) {
    return "login";
    } else {
    return actionInvocation.invoke();
    }
    }
    }
    public void destroy() {
    // TODO Auto-generated method stub
    }
    public void init() {
    // TODO Auto-generated method stub
    }
    }

     ---------------------------------------------------struts.xml 配置:

    <package name="global" namespace="/" extends="struts-default">
         <!-- struts2 拦截器Session 超时 -->
       <interceptors>
    <interceptor name="sessionInterceptor" class="com.egintra.common.base.SessionInterceptor" />
    <!-- 拦截器栈 -->
    <interceptor-stack name="myStack">
    <interceptor-ref name="sessionInterceptor" />
    <interceptor-ref name="defaultStack" />
    </interceptor-stack>
    </interceptors>
        <default-interceptor-ref name="myStack" />
        
            <global-results>
                <result name="exception">/jsp/error.jsp</result>   
                <result name="login">/jsp/system/login/login.jsp</result>      
            </global-results>
            <global-exception-mappings>
                <exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
            </global-exception-mappings>                
        </package>
  • 相关阅读:
    ubuntu16.04上安装深度学习环境
    QQ音乐付费音乐下载的demo
    python列表按比例或长度拆分
    docker 使用gpu启动及tf限额
    python apscheduler was missed by
    k8s中使用gpu的部署方案
    从字符串中找出最大的数字串,字符串中可以带小数点。--python实现
    linux中批量删除带空格的文件
    tornado中使用python3原生事件循环asyncio
    mysql DATE_FORMAT FROM_UNIXTIME 的区别
  • 原文地址:https://www.cnblogs.com/leonkobe/p/2939128.html
Copyright © 2020-2023  润新知