zoukankan      html  css  js  c++  java
  • struts2登陆拦截器 (FIX)

    struts2.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    	"http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    	<!-- 覆盖掉Struts2一些常量的默认值 -->
        <constant name="struts.enable.DynamicMethodInvocation" value="true" />
        <constant name="struts.devMode" value="true" />
        <constant name="struts.action.extension" value="action,,do"></constant>
        <constant name="struts.configuration.xml.reload" value="true"></constant>
        <constant name="struts.locale" value="zh_CN"></constant>
        <package name="mydefaultpackage" extends="struts-default">
        	<interceptors><!-- 只是定义拦截器 -->
    			<interceptor name="timeCost" class="com.itheima.interceptor.TimeCostInterceptor"></interceptor>
    			<interceptor name="loginCheck" class="com.itheima.interceptor.LoginCheckInterceptor">
    				<param name="excludeMethods">sayHello1</param><!-- 排除在拦截之外 -->
    			</interceptor>
    			<!-- 定义小组 -->
    			<interceptor-stack name="mydefaultStack">
    				<interceptor-ref name="annotationWorkflow"></interceptor-ref>
    				<!-- <interceptor-ref name="loginCheck"></interceptor-ref> -->
    				<interceptor-ref name="timeCost"></interceptor-ref>
    				<interceptor-ref name="defaultStack"></interceptor-ref>
    			</interceptor-stack>
    		</interceptors>
    		<default-interceptor-ref name="mydefaultStack"></default-interceptor-ref>
        </package>
    	<package name="p1" extends="mydefaultpackage">
    		<action name="test1" class="com.itheima.actions.ActionDemo1">
    			<result>/index.jsp</result>
    			<result name="loginCheckFailure">/failure.jsp</result>
    		</action>
    		<action name="test2" class="com.itheima.actions.ActionDemo2">
    			<result>/index.jsp</result>
    		</action>
    	</package>
    </struts>
    

    TimeCostInterceptor

    package com.itheima.interceptor;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    //统计动作方法执行效率的拦截器
    public class TimeCostInterceptor extends AbstractInterceptor {
    	//核心拦截方法
    	public String intercept(ActionInvocation invocation) throws Exception {
    		long startTime = System.nanoTime();//纳秒: 1s=1000000000纳秒
    		String result = invocation.invoke();//让下一个执行
    		long endTime = System.nanoTime();
    		System.out.println(invocation.getInvocationContext().getName()+" cost time :"+(endTime-startTime)+" ns");
    		return result;
    	}
    
    }
    

    LoginCheckInterceptor

    package com.itheima.interceptor;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
    
    public class LoginCheckInterceptor extends MethodFilterInterceptor {
    
    	protected String doIntercept(ActionInvocation invocation) throws Exception {
    		String user = (String)ServletActionContext.getRequest().getSession().getAttribute("user");
    		String result = "loginCheckFailure";
    		if(user!=null){
    			result = invocation.invoke();
    		}
    		return result;
    	}
    
    }
    

    failure.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        
        <title></title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	
    
      </head>
      
      <body>
        对不起!您没有登陆<a href="${pageContext.request.contextPath}/login.jsp?username=wzhting">登陆</a>
      </body>
    </html>
    

    login.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        
        <title></title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	
    
      </head>
      
      <body>
        <%
        String username = request.getParameter("username");
        if(username!=null){
        	session.setAttribute("user", username);
        }else{
        	session.invalidate();//相当于注销
        }
        %>
      </body>
    </html>
    

      

  • 相关阅读:
    oracle 使用正则表达式获取字符串中包含的数字
    oracle 将逗号分隔的字符串转成多行记录
    sqlplus下 查看oracle 执行计划
    使用connect by 虚拟生成100条记录
    Lomsat gelral
    Legacy
    慢慢变小的序列(好题)
    Walls(扫描线)
    线段树求面积并,面积交,周长
    最小圆覆盖(随机增量||模拟退火)
  • 原文地址:https://www.cnblogs.com/vaer/p/4691677.html
Copyright © 2011-2022 走看看