zoukankan      html  css  js  c++  java
  • struts1拦截器

    Struts2已经发布一段时间了,这个版本较struts1.x版本有了很大变化,其中一个就是增加了拦截器功能。这是个非常有用的功能,可是struts1.x却没有。     其实,struts1.x可以配合插件,实现拦截器的功能。     SAIF(Struts Action Invocation Framework)是一个开源组件,它让Struts框架具备Action拦截器与IOC的功能,这样你的1.x框架也就有了拦截器的功能。       1.将saif.jar包放入你的lib中。       2.创建Interceptor类。比如我在这里创建一个类:

    package interceptor;
    import java.io.IOException;
    import javax.servlet.ServletException;
       import javax.servlet.http.HttpServletRequest;
       import javax.servlet.http.HttpServletResponse; 
    import org.apache.struts.action.Action;
       import org.apache.struts.action.ActionForm;
       import org.apache.struts.action.ActionForward;
       import org.apache.struts.action.ActionMapping;
    import net.sf.struts.saif.ActionHaveForwardInterceptor;
    public class Display Interceptor implements ActionHaveForwardInterceptor{     
            public ActionForward afterAction(Action arg0, ActionMapping arg1,             
                 ActionForm arg2, HttpServletRequest arg3, HttpServletResponse arg4)            
                     throws IOException, ServletException{         
                        // TODO Auto-generated method stub         
                                returnnull;     
                            }     
                   public ActionForward beforeAction(Action action, ActionMapping mapping,              ActionForm form, HttpServletRequest request, HttpServletResponse response)             throws IOException, ServletException{         
                // TODO Auto-generated method stub         
                      System.out.println("Inteceptor...");        
                                if (!"fred".equals(request.getParameter("user_name"))){             
                                    return mapping.findForward("noPermission");         
                                          }         
                                     returnnull;     
                                       } 
    }

    3.写interceptor配置文件:interceptor-config.xml。这个配置文件中指定了interceptor类和要被拦截的action

    <?xml version="1.0" encoding="UTF-8"?>
    <interceptor-config>   
    <interceptorname="displayInterceptor" type="interceptor.DisplayInterceptor"/>        
    <actiontype="/display">         
    <interceptorname="displayInterceptor"/>   
    </action>      </interceptor-config>

    4.在struts-config.xml中指定加载interceptor-config.xml

    <plug-in className="net.sf.struts.saif.SAIFSpringPlugin">     
    <set-propertyproperty="interceptor-config" value="/WEB-INF/interceptor-config.xml"/>   
    </plug-in>

    ok,配置完后,启动服务器,然后输入.../display.do?user_name=fred,回车,这时候,这个请求就会被拦截来,

    进入beforeAction中,进行验证,若验证成功,return null,就会转到action的forward指向的页面,若不成功,

    就会转向另一个页面。

  • 相关阅读:
    [译文] 实体与值对象到底是不是一回事?
    实现 WebApi 自托管服务宿主于 WinForms 及其交互
    [译文] C# 8 已成旧闻, 向前, 抵达 C# 9!
    [译文] 为什么你在 C# 里总是应该使用 "var" 关键字
    通过设置iis在局域网中访问网页
    windows 10 安装使用kafka
    ASP.NET Core 2.1 中的 HttpClientFactory (Part 4) 整合Polly实现瞬时故障处理
    ASP.NET Core 2.1 中的 HttpClientFactory (Part 3) 使用Handler实现传出请求中间件
    ASP.NET Core 2.1 中的 HttpClientFactory (Part 2) 定义命名化和类型化的客户端
    Asp.net Core 2.0 OpenId Connect Handler缺失Claims?
  • 原文地址:https://www.cnblogs.com/hkxd-1/p/4871183.html
Copyright © 2011-2022 走看看