strut2+Inteceptor实现简单的登录拦截

很久没有用过strtus2了,今天算作复习,一般项目中会使用shiro框架来做身份验证和授权,有机会会继续在此项目的基础上进行版本升级,使用shiro框架

 

这部分代码可以作为struts2的默认文件直接复制即可

只需要将其余各自用户的struts文件引入进来即可

<?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>
    <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    
    <!-- 设置动态调用方法 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    
    <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理。    
        如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
    <constant name="struts.action.extension" value="action"/>
    
    <!-- 开发阶段设置 -->
    <!-- 设置浏览器是否缓存静态内容,默认值为true,开发阶段最好false -->
    <constant name="struts.serve.static.browserCache " value="false"/>
    
    <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,
        默认值为false,开发阶段最好true -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    
    <!-- 开发模式下设为true,这样可以打印出更详细的错误信息 -->
    <constant name="struts.devMode" value="true"/>
    
    <!-- 拦截器 -->
    <include file="struts-interceptor.xml"/> 
    <!-- 用户 -->
    <include file="struts-user.xml" />
    
</struts>


这是用户登录的struts文件

成功,进入主页;失败,返回到登录页

这里要注意的是form表单提交数据的时候 action地址 :action="${pageContext.request.contextPath }/user/userAction.action"

注意:extends="all" 继承的是 拦截器的 packageName;        哪些访问需要使用拦截器,直接继承拦截器的名字即可

<?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>
   <package name="user" namespace="/user" extends="all">
        <action name="userAction" method="execute"
            class="com.genius.action.UserAction">
            <!-- 默认返回值
                method="execute"
                成功:success;
                失败:input
             -->
            <result name="success">/index.jsp</result>
            <result name="input">/jsp/error.jsp</result>
        </action>
   </package>
</struts>


登录页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head>
<title>登录页面</title>
</head>
<body>
    <!-- 
        注意form的提交路径:绝对路径/nameSpace/actionName.action
     -->
    <form action="${pageContext.request.contextPath }/user/userAction.action" method="post">
    
         <div align="center" style="margin: 300px">
            UserName: <input name="username" type="text"><br>
            <br>
            PassWord: <input name="password" type="text"><br>
            <br>
            <input type="submit" value="提交"><input type="reset" value="重置">
        </div> 
        
    </form>
</body>
</html>


定义拦截器

以下两种方式都可以,当然推荐使用第一种:被注释的那段

package com.genius.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

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

/**
 *<p>Description: 登录拦截器</p>
 * @author Chenfangbo
 * @date 2016-4-6
 * 
 */
public class LoginInterceptor extends AbstractInterceptor{

    private static final long serialVersionUID = 1L;

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        
        //取得请求相关的ActionContext
        /*ActionContext actionContext  =  invocation.getInvocationContext();
        Map session = actionContext.getSession();
        String username = (String) session.get("username");
        System.out.println("username : " + username);
        if(username != null && !username.equals("")){
            return invocation.invoke();
        }*/
        
         HttpServletRequest request = ServletActionContext.getRequest();  
         HttpSession session = request.getSession(true);  
         String username = (String) session.getAttribute("username");  
         
         if(username != null && !username.equals("")){
                return invocation.invoke();
        }
        return "login";
    }

}


拦截器的status文件

注意:package name="all" 哪个文件要使用该拦截器,就继承这个名字

<?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>
    <package name="all" extends="struts-default">
        <!-- 配置拦截器 -->
        <interceptors>
            <!-- 添加登录拦截器 -->
            <interceptor name="loginInterceptor" class="com.genius.interceptor.LoginInterceptor" />
            <!-- 拦截器栈 
                一般把默认的和自定义的都加上
            -->
            <interceptor-stack name="loginStack">
                <interceptor-ref name="loginInterceptor"></interceptor-ref>
                <!-- 一般把默认的拦截器加上 -->
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>
        </interceptors>        
        <!-- 修改默认拦截器 -->
        <default-interceptor-ref name="loginStack"></default-interceptor-ref>    
        <!-- 将result设为全局的,这样就不用在每个package中添加了 -->
    
        <!-- 全局异常处理 -->
        <global-results>
           <result name="login" type="redirect">/index.jsp</result>  
        </global-results>
    </package>
</struts>    

 

posted @ 2016-04-06 16:54  江湖一笑  阅读(175)  评论(0编辑  收藏  举报