发布于 2017-11-22 05:27:49 | 143 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java函数式编程,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


这篇文章主要为大家详细介绍了JavaWeb中Struts2拦截器的功能,感兴趣的小伙伴们可以参考一下

一、struts2中的拦截器(框架功能核心)

1、过滤器VS拦截器

过滤器VS拦截器功能是一回事。过滤器是Servlet规范中的技术,可以对请求和响应进行过滤。

拦截器是Struts2框架中的技术,实现AOP(面向切面)的编程思想,是可插拔的, 可以对访问某个 Action 方法之前或之后实施拦截。

拦截器栈(Interceptor Stack): 将拦截器按一定的顺序联结成一条链. 在访问被拦截的方法时, Struts2拦截器链中的拦截器就会按其之前定义的顺序被依次调用

Struts2执行原理 - 底层分析

2、自定义拦截器

struts2定义了一个拦截器接口Interceptor接口。
Interceptor接口里面有三个抽象方法

•init: 该方法将在拦截器被创建后立即被调用, 它在拦截器的生命周期内只被调用一次. 可以在该方法中对相关资源进行必要的初始化
 •interecept: 每拦截一个动作请求, 该方法就会被调用一次.
•destroy: 该方法将在拦截器被销毁之前被调用, 它在拦截器的生命周期内也只被调用一次.
Struts 会依次调用程序员为某个 Action 而注册的每一个拦截器的 interecept 方法.每次调用 interecept 方法时, Struts 会传递一个 ActionInvocation 接口的实例.

ActionInvocation: 代表一个给定动作的执行状态, 拦截器可以从该类的对象里获得与该动作相关联的 Action 对象和 Result 对象. 在完成拦截器自己的任务之后, 拦截器将调用 ActionInvocation 对象的 invoke 方法前进到 Action 处理流程的下一个环节.

还可以调用 ActionInvocation 对象的 addPreResultListener 方法给 ActionInvocation 对象 “挂” 上一个或多个 PreResultListener 监听器. 该监听器对象可以在动作执行完毕之后, 开始执行动作结果之前做些事情

自定义拦截器步骤:

a、编写一个类,实现com.opensymphony.xwork2.interceptor.Interceptor接口,或者继承
com.opensymphony.xwork2.interceptor.AbstractInterceptor类。(适配器模式),一般都选择继承AbstractInterceptor(拦截器会驻留内存)。因为AbstractInterceptor 类实现了 Interceptor 接口. 并为 init, destroy 提供了一个空白的实现

编写两个拦截器InterceptorDemo1 ,和InterceptorDemo2


package com.itheima.interceptor;

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

public class InterceptorDemo1 extends AbstractInterceptor {
 //动作的每次访问都会调用该方法
 public String intercept(ActionInvocation invocation) throws Exception {
  System.out.println("拦截前Demo1");
  String rtvalue = invocation.invoke();//放行,这里为什么返回string?
因为最终的结果返回的Action的Result,而action的结果是string类型
  System.out.println("拦截后Demo1");
  return rtvalue;
 }

}

package com.itheima.interceptor;

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

public class InterceptorDemo2 extends AbstractInterceptor {
 //动作的每次访问都会调用该方法
 public String intercept(ActionInvocation invocation) throws Exception {

//  invocation.addPreResultListener(new PreResultListener() {
//   
//   public void beforeResult(ActionInvocation invocation, String resultCode) {
//    System.out.println("结果显示前");
//   }
//  });

  System.out.println("拦截前Demo2");
  String rtvalue = invocation.invoke();//放行
  System.out.println("拦截后Demo2");
  return rtvalue;
 }

}

b、需要在struts.xml中进行定义,定义拦截器,先定义在使用。


<package name="p1" extends="struts-default">
 <!-- 定义拦截器:只对当前包有效 -->
 <interceptors>
  <interceptor name="interceprotDemo1" class="com.itheima.interceptor.InterceptorDemo1"></interceptor>
  <interceptor name="interceprotDemo2" class="com.itheima.interceptor.InterceptorDemo2"></interceptor>
 </interceptors>

</package>

c、在动作配置中就可以使用了


<action name="action1" class="com.itheima.action.Demo1Action" method="execute">
  <!-- 使用定义的拦截器。如过没有指定任何的拦截器,默认使用default-stack栈中的所有拦截器;
   一旦指定了任何一个拦截器,默认的就无效了
   -->
   <interceptor-ref name="interceprotDemo1"></interceptor-ref>
   <interceptor-ref name="interceprotDemo2"></interceptor-ref>
   <result>/success.jsp</result>
</action>

实现动作类Demo1Action


package com.itheima.action;

import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action extends ActionSupport {

 @Override
 public String execute() throws Exception {
  System.out.println("execute执行了");
  return SUCCESS;
 }

}

运行结果

因为struts2中如文件上传,数据验证,封装请求参数到action等功能都是由系统默认的defaultStack中的拦截器实现的,所以我们定义的拦截器需要引用系统默认的defaultStack,这样应用才可以使用struts2框架提供的众多功能。

如过没有指定任何的拦截器,默认使用default-stack栈中的所有拦截器;一旦指定了任何一个拦截器,默认的就无效了除了要使用自定义的拦截器之外,还要使用defaultStack,可以这么办

方法一:(自己使用),只需在action中配置自定义的和defaultStack默认的就可以了。

方法二:(大家都用的时候),如果希望包下的所有action都使用自定义的拦截器, 要使用拦截器栈 interceptor-stack,定义一个interceptor-stack,然后在action中可以通过<default-interceptor-ref name=“mydefaultStack”/>把拦截器定义为默认拦截器,mydefaultStack名字可以自己取。


<interceptors>
   <interceptor name="interceprotDemo1" class="com.itheima.interceptor.InterceptorDemo1"></interceptor>
   <interceptor name="interceprotDemo2" class="com.itheima.interceptor.InterceptorDemo2"></interceptor>
   <interceptor-stack name="mydefaultStack">
    <interceptor-ref name="defaultStack"></interceptor-ref>
    <interceptor-ref name="interceprotDemo1"></interceptor-ref>
    <interceptor-ref name="interceprotDemo2"></interceptor-ref>
   </interceptor-stack>
</interceptors>
<action name="action3" class="com.itheima.action.LoginAction" method="login">
   <interceptor-ref name="mydefaultStack"></interceptor-ref>
   <result>/success.jsp</result>
</action>

3、Struts2 自带的拦截器

案例1:检查用户是否登录

1、 编写页面login.jsp


 <body>
 <form action="${pageContext.request.contextPath}/login.action" method="post">
  <input type="text" name="username"/><br/>
  <input type="text" name="password"/><br/>
  <input type="submit" value="登录"/>
 </form>
 </body>

2、编写登录校验的拦截器LoginCheckInterceptor 类


package com.itheima.interceptor;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

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

public class LoginCheckInterceptor extends AbstractInterceptor {

 public String intercept(ActionInvocation invocation) throws Exception {
  HttpSession session = ServletActionContext.getRequest().getSession();//通过ServletActionContext对象获得session对象
  Object user = session.getAttribute("user");
  if(user==null){
   //没有登录
   return "login";//返回到某个逻辑视图
  }
  return invocation.invoke();//放行
 }

}

3、编写配置文件struts.xml


<package name="p2" extends="struts-default">
  <interceptors>
   <interceptor name="loginCheckInterceptor" class="com.itheima.interceptor.LoginCheckInterceptor"></interceptor>
   <interceptor-stack name="mydefaultStack">
    <interceptor-ref name="defaultStack"></interceptor-ref>
    <interceptor-ref name="loginCheckInterceptor"></interceptor-ref>
   </interceptor-stack>
  </interceptors>
  <action name="login" class="com.itheima.action.CustomerAction" method="login">
   <result>/login.jsp</result>
  </action>
 </package>

4、编写动作类CustomerAction


package com.itheima.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport {
 public String login(){
  System.out.println("登录");
  ServletActionContext.getRequest().getSession().setAttribute("user", "ppp");
  return SUCCESS;
 }
}

案例2:监测动作方法的执行效率

编写时间监测过滤器TimerInterceptor


package com.itheima.interceptor;

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

public class TimerInterceptor extends AbstractInterceptor {

 public String intercept(ActionInvocation invocation) throws Exception {
  long time = System.nanoTime();
  String rtvalue = invocation.invoke();
  System.out.println(rtvalue+"执行耗时:"+(System.nanoTime()-time)+"纳秒");
  return rtvalue;
 }

}

编写配置文件


<package name="p2" extends="struts-default">
  <interceptors>
   <interceptor name="loginCheckInterceptor" class="com.itheima.interceptor.LoginCheckInterceptor"></interceptor>
   <interceptor name="timerInterceptor" class="com.itheima.interceptor.TimerInterceptor"></interceptor>
   <interceptor-stack name="mydefaultStack">
    <interceptor-ref name="defaultStack"></interceptor-ref>
    <interceptor-ref name="loginCheckInterceptor"></interceptor-ref>
    <interceptor-ref name="timerInterceptor"></interceptor-ref>
   </interceptor-stack>
  </interceptors>
   <result name="login">/login.jsp</result>
  </action>
 </package>

从上面可以看出,在一个action 中可以配置多个过滤器。

4、自定义拦截器:能够指定拦截的方法或不拦截的方法

能够指定拦截的方法或不拦截的方法,编写过滤器时,可以实现类MethodFilterInterceptor,里面有两个字段,通过注入参数就可以指定那些不拦截,两个参数只要用一个即可,当拦截较少是,可以用includeMethods ,当拦截较多是,可以用排除的方法excludeMethods 。

excludeMethods = Collections.emptySet();//排除那些
includeMethods = Collections.emptySet();//包括那些

案例:再续登录校验的例子。

1、编写过滤器LoginCheckInterceptor


package com.itheima.interceptor;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

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

public class LoginCheckInterceptor extends MethodFilterInterceptor {
 protected String doIntercept(ActionInvocation invocation) throws Exception {
  HttpSession session = ServletActionContext.getRequest().getSession();
  Object user = session.getAttribute("user");
  if(user==null){
   //没有登录
   return "login";//返回到某个逻辑视图
  }
  return invocation.invoke();//放行
 }

}

2、编写配置文件

3、编写动作类CustomerAction


package com.itheima.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport {
 public String add(){
  System.out.println("调用add的service方法");
  return SUCCESS;
 }
 public String edit(){
  System.out.println("调用edit的service方法");
  return SUCCESS;
 }
 public String login(){
  System.out.println("登录");
  ServletActionContext.getRequest().getSession().setAttribute("user", "ppp");
  return SUCCESS;
 }
}

4、编写页面
addCustomer.jsp


 <body>
 添加客户
 </body>

editCustomer.jsp


 <body>
 修改客户
 </body>

login.jsp


 <body>
 <form action="${pageContext.request.contextPath}/login.action" method="post">
  <input type="text" name="username"/><br/>
  <input type="text" name="password"/><br/>
  <input type="submit" value="登录"/>
 </form>
 </body>

success.jsp


 <body>
 oyeah
 </body>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务