AuthenticationInterceptor extends AbstractInterceptor

    <package name="main" extends="struts-default" >
<!-- 
        <global-results>
            <result name="login">/login.jsp</result>
            <result name="loginfail">/loginfail.jsp</result>
        </global-results>
 -->
         <interceptors>
            <interceptor name="authentication"
                class="com.helloweenvsfei.struts2.interceptor.AuthenticationInterceptor">
            </interceptor>
        </interceptors>

        <global-results>
            <result name="login">/login.jsp</result>
        </global-results>
        
         <action name="Hello"
            class="com.skex.struts2.action.HelloAction">
            <interceptor-ref name="authentication"></interceptor-ref>
            <result>/Hello.jsp</result>
        </action>
        
    </package>
package com.helloweenvsfei.struts2.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthenticationInterceptor extends AbstractInterceptor {

    private static final long serialVersionUID = -4433771430728214868L;

    @Override
    @SuppressWarnings("all")
    public String intercept(ActionInvocation invocation) throws Exception {

        Map<String, Object> sessionValues = invocation.getInvocationContext()
                .getSession();

        String account = (String) sessionValues.get("account");

        if (account == null) {
            return Action.LOGIN;
        }

        return invocation.invoke();
    }

}

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    This is my JSP page. <br>
    <%
    session.setAttribute("account", "JimGreen");
     %>
     <a href="Hello.action">Hello.action</a>
  </body>
</html>

 

posted @ 2017-05-22 19:31  sky20080101  阅读(210)  评论(0)    收藏  举报