strut2 模拟拦截器

  需求:用户登录的情况下可以访问action的方法,用户没有登录时不允许访问action中的方法并提示'你没有权限访问"。

  设计思路:建立一个jsp页面,当请求该页面时,设置用户为登录状态。若没有先请求该jsp页面而去访问action中的方法则不允许。

  实现:

  1.    建立一个user.jsp页面,设置用户为登录状态:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
        request.getSession().setAttribute("user", "itcast");
    %>
    
     用户已经登录
  2. 自定义一个拦截器类,用来判断用户是否登录,如果没有则返回到全局视图页面,如果已经登录则允许执行action中的方法:
    package cn.itcast.interceptor;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;
    
    public class PermissionInterceptor implements Interceptor {
    
        public void destroy() {
        }
    
        public void init() {
        }
    
    //拦截器主要的方法
    public String intercept(ActionInvocation invocation) throws Exception { Object user = ActionContext.getContext().getSession().get("user"); if(user!=null) return invocation.invoke(); //如果user不为null,代表用户已经登录,允许执行action中的方法 ActionContext.getContext().put("message", "你没有权限执行该操作"); return "success"; } }
  3. action中的代码如下:
    package cn.itcast.action;
    
    public class HelloWorldAction {    
        private String message;
        
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public String addUI(){
            this.message = "addUI";
            return "success";
        }
    
        public String execute() throws Exception{
            this.message = "execute";
            return "success";
        }
    }
  4. 注册拦截器,在struts.xml中配置拦截器,代码如下:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
        <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
        <constant name="struts.action.extension" value="do,action"/>
        <constant name="struts.multipart.maxSize" value="10701096"/>
        
        <package name="employee" namespace="/control/employee" extends="struts-default">
            <interceptors>
                 <!--配置自定义的拦截器-->
                <interceptor name="permission" class="cn.itcast.interceptor.PermissionInterceptor"/>
                <!--配置一个拦截栈-->
                <interceptor-stack name="permissionStack">
                     <!--struts2的拦截栈不能丢,且放在自定义的拦截器前面-->
                    <interceptor-ref name="defaultStack"/>
                    <interceptor-ref name="permission" />
                </interceptor-stack>
            </interceptors>
            <global-results><!--配置全局视图-->
                <result name="success">/WEB-INF/page/message.jsp</result>
            </global-results>
            <action name="list_*" class="cn.itcast.action.HelloWorldAction" method="{1}">
                <interceptor-ref name="permissionStack" /><!--为action配置拦截器-->
            </action>
        </package>
    </struts>
  5. 测试:在浏览器中输入:http://localhost:8080/xxx/list_excute.action  因为用户是直接访问action中的方法,所以页面提示没有权限访问。
  6. 在浏览器中请求user.jsp页面,因为用户已经设为登录状态,所以可以正常访问。
原文地址:https://www.cnblogs.com/kailing-con/p/4196958.html