• 面向切面编程(Aspect-Oriented Programming,AOP)设计思想的实现       

      这里说一下面向切面编程:AOP设计思想是处理对象之间的横向关系,它将OOP(纵向)中混在一起的业务处理代码和功能代码分离出来,这样既减少了代码的重复,又降低模块之间的耦合,还增强了系统的可维护性和可读性

          struts拦截器---工作原理、作用、应用           struts拦截器---工作原理、作用、应用

                    (OOP)                                                       (AOP)

 

  • 工作流程

  •       struts拦截器---工作原理、作用、应用1
  1. 客户端发送请求 (客户端初始化一个指向Servlet容器(如Tomcat)的请求)
  2. 请求经过一系列过滤器(如ActionContextCleanUp、SiteMesh等),ActionContextCleanUp-->FilterDispatcher
  3. FilterDispatcher通过ActionMapper来决定这个Request需要调用哪个Action
  4. 如果ActionMapper决定调用某个Action,FilterDispatcher把请求的处理交给ActionProxy,
  5. ActionProxy通过ConfigurationManager询问Struts配置文件(Struts.xml),找到需要调用的Action类。
  6. ActionProxy创建一个ActionInvocation的实例
  7. ActionInvocation调用真正的Action,当然这涉及到相关拦截器的调用(interceptor)
  8. Action执行完毕,ActionInvocation创建Result并返回
  • 作用

     1.过滤器和拦截器非常相似,过滤器过滤的是所有的请求,拦截器只过滤action。他们都是Java类。 Struts2框架在调用action处理用户请求的前后,通过调用拦截器来完成某些操作。在struts2中,拦截器是单列的,所有action共享相同的拦截器,所以在拦截器中定义常量时要注意线程安全的问题。

      2.Struts2框架中的拦截器都是可插拔的,这种可插拔性是通过配置文件来完成的。如果需要使用某项功能的时候,只需在配置文件中配置对应的拦截器即可;如果不需要某项功能的时候,只需从配置文件中去除相应的拦截器配置即可。

  • 内置拦截器

  • 自定义拦截器的步骤

       (1)创建拦截器类(两种实现方式:a)实现Interceptor接口    b)继承AbstractIntercepter类);

       (2)注册拦截器;

       (3)使用拦截器

     eg:自定义拦截器实例1

//1.创建Action类
import com.opensymphony.xwork2.ActionSupport;

public class InterAction extends ActionSupport{
	private String message;
	
public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

@Override
public String execute() throws Exception {
	// TODO Auto-generated method stub
	addActionMessage("InterAction.execute():message is"+message);
	System.out.println("InterAction.execute():message is"+message);
	return super.execute();
}
}

//2.创建拦截器
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class MyInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		// TODO Auto-generated method stub
		InterAction ia=(InterAction) actionInvocation.getAction();
		ia.addActionMessage("Interception1");
		System.out.println("Interception1");
		ia.addActionMessage("Interception1-1:ActionName is "+ia.getClass().getName());
		System.out.println("Interception1-1:ActionName is "+ia.getClass().getName());
		String result=actionInvocation.invoke();
		ia.addActionMessage("Interception1-2:The result is "+result);
		System.out.println("Interception1-2:The result is "+result);
		
		return result;
	}

}
//3.注册拦截器
<interceptor name="myinterceptor" class="intercepteraction.MyInterceptor"></interceptor>
	</interceptors>
		<action name="ljq" class="intercepteraction.InterAction">
			<!--绝对路径,根路径 -->
			<result >/login.jsp</result>
<!--这句默认的,如果不写会被自定义的覆盖,为了不覆盖掉,最好写上-->
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="myinterceptor"></interceptor-ref>
		</action>
		
//4.使用拦截器
<s:actionmessage/>
<s:form action="ljq">
<s:textfield name="message" label="message"></s:textfield>
<s:submit value="submit" ></s:submit>
<s:reset value="reset" ></s:reset>
</s:form>

结果:

struts拦截器---工作原理、作用、应用

(可以看到控制台与页面显示不一致,原因是在执行完invoke后即action完成后输出页面后,才执行后面两句)

  • 应用

    避免表单重复提交、数据类型转换、数据校验、文件上传、国际化及权限管理等。

相关文章: