拦截器代码: 

package mycrm.interceptor;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

import mycrm.domain.User;

public class PrivilegeInterceptor extends MethodFilterInterceptor {

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        //判断session是否有用户信息
        User existUser = (User) ServletActionContext.getRequest().getSession().getAttribute("existUser");
        if(existUser == null){
            //没有登陆,给出提示信息,回到登录页面
            //获取当前Action
             ActionSupport actionSupport = (ActionSupport) invocation.getAction();
             actionSupport.addActionError("没有权限,请登录后再访问!");
             //回到登录页面
             return actionSupport.LOGIN;
        }else{
            //已经登录,放行,执行下一个拦截器
            return invocation.invoke();
        }
    }

}

向配置文件添加拦截器:

<?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="action" extends="struts-default" namespace="/">

<!-- 定义拦截器 -->
<interceptors>
<interceptor name="privilegeInterceptor" class="mycrm.interceptor.PrivilegeInterceptor"/>
</interceptors>

<!-- 全局页面 -->
<global-results>
<result name="login">/login.jsp</result>
</global-results>

<!-- 配置Customer的Action -->
<action name="customer_*" class="mycrm.action.CustomerAction" method="{1}">

<result name="findAll">/jsp/customer/list.jsp</result>
<result  name="add">/jsp/customer/add.jsp</result>
<result name="addSuccess">/jsp/customer/list.jsp</result>

<!-- 添加拦截器,登录和注册不拦截 -->
<interceptor-ref name="privilegeInterceptor">
<param name="excludeMethods">login</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>

</action>

<!-- 配置User的Action -->
<action name="user_*" class="mycrm.action.UserAction" method="{1}">

<result name="login">/login.jsp</result>
<result type="redirect">/index.jsp</result>

<!-- 添加拦截器,登录和注册不拦截 -->
<interceptor-ref name="privilegeInterceptor">
<param name="excludeMethods">login</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>

</action>

</package>
</struts>

 设置表单target放置页面嵌套:

  <FORM id=form1 name=form1 action="${pageContext.request.contextPath }/user_login.action" method=post target="_parent">                        

最新文章

  1. JS常用属性
  2. RDIFramework.NET V2.7 Web版本升手风琴+树型目录(2级+)方法
  3. (C/C++) Interview in English - Class
  4. 第一个完整的cppunit单元测试程序
  5. Spring REST实践之Versioning,Paging和Sorting
  6. OC中类别、扩展、协议与托付
  7. 17.1.1.3 Creating a User for Replication
  8. 深入理解Tomcat系列之一:系统架构(转)
  9. linux下串口调试工具/串口终端推荐: picocom
  10. 附加被分离DB
  11. maven创建web工程Spring配置文件找不到问题解决方案
  12. Spring MVC工作原理
  13. 506. Relative Ranks
  14. [SDOI2011]计算器(exgcd&amp;BSGS)
  15. TensorFlow入门学习(让机器/算法帮助我们作出选择)
  16. Android动画之逐帧动画(FrameAnimation)详解
  17. Netty实践二(心跳检测)
  18. C++中 explicit的用法
  19. Openwrt TF Card Auto Mount&amp;Check (4)
  20. C#中使用TCP通信

热门文章

  1. docker 报错 docker: Error response from daemon: driver failed....iptables failed:
  2. 【PAT甲级】1109 Group Photo (25分)(模拟)
  3. web应用程序上传文件 超过了最大请求长度
  4. 创建Maven project 提示pom.xml 首行错误
  5. 【转载】巴塞尔问题(Basel Problem)的多种解法
  6. IS加载JSON 和 MP4文件 错误 404 提示 需要添加mime映射(默认IIS Express里没有映射)
  7. 不要在mutation回调函数之外,修改vuex仓库里属性的状态
  8. 「WC2013」糖果公园
  9. 「NOI2016」区间
  10. Python - 关于属性访问的优先级,属性访问顺序,python attributel lookup,类和实例访问属性的顺序