Struts2运行机制(MVC)的分析:
2013-06-22 22:02
375 查看
C:(controller)控制器 M:(model)模型处理 V:(view)视图
Struts 2 的运行过程:
核心控制器是FilterDispatcher会过滤所有的请求,如果请求以 action结尾,该请求会转入框架处理。当框架获取*action请求后,根据*action的前半部分决定调用哪个业务逻辑组件。最后根据业务逻辑组件的处理信息决定转发到哪个视图;
Struts2有三部分组成:核心控制器是FilterDispatcher,业务控制器和业务逻辑组件组件,其中核心控制器FilterDispatcher由Struts2提供,而业务逻辑控制器和业务逻辑组件要用户自己实现。
核心控制器FilterDispatcher:负责拦截所有用户的请求,如果用户的请求以action结尾,该请求就会转入Struts2框架处理。
业务控制器组件:实现Action类的实例(或者继承了ActionSupport的实例),该类经常能够返回一个字符串(逻辑视图<result>的execute方法,用来实现业务控制)。
业务逻辑组件:一般由javaBean或者EJB实现;
注意:在Struts2中处理用户请求的并不是业务逻辑,而是Action代理:过程是这样的,在Struts2框架中有一系列的拦截器,这些拦截器将HttpServletRequest请求中的参数分析出来,传入Action中,并回调execute()方法来处理用户请求。
当所有的请求被拦截器拦截时:执行流程
1,FilterDispatcher会将所有的请求转发给ActionProxy(Action代理),Action代理会根据配置文件struts.xml决定转发给那个Action;
2,在请求转发给Action的过程中,会经过一系列拦截器,这些拦截器负责将请求解析并转发给相应的Action。
3,经过相应的Action的execute()方法处理,会得到一个视图名的结果集,根据结果结合相应的模版产生相应的输出流。
4,输出流也可以经过一些列的拦截器后,传给浏览器。
![](http://img.blog.csdn.net/20130622220720156?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb21pbmcyNjIzNTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
对于源码根据图的理解:在Struts2的doFilter()方法中,通过execute.executeAction(request, response, mapping)执行进入了Dispatcher,生成了Dispatcher的对象,并调用了serviceAction()方法;接下里调用了create()方法生成ActionProxy的对象proxy ,通过proxy调用了Actioninvaction的的invoke()方法,接下里,Actioninvaction调用interceptor()方法,然后再
继续调用invoke()方法返回,判断是否interceptor()调用完成,直到所有的interceptor()方法调用完成,就执行*Action所对应的execute().
模拟Struts2 实现的全过程代码;
public class Main
{
public static void main(String[] args)
{
new ActionInvaction().invoke();
}
}
public interface Interceptor
{
public void interceptor(ActionInvaction invaction);
}
public class FristInterceptor implements Interceptor
{
public void interceptor(ActionInvaction invaction)
{
System.out.println(1);
invaction.invoke();
System.out.println(-1);
}
}
public class SecondInterceptor implements Interceptor
{
public void interceptor(ActionInvaction invaction)
{
System.out.println(2);
invaction.invoke();
System.out.println(-2);
}
}
public class ActionInvaction
{
List<Interceptor> interceptors = new ArrayList<Interceptor>();
int index = -1;
Action a = new Action();
public ActionInvaction()
{
this.interceptors.add(new FristInterceptor());
this.interceptors.add(new SecondInterceptor());
}
public void invoke()
{
index++;
if(index >= interceptors.size())
{
a.execute();
}
else
{
this.interceptors.get(index).interceptor(this);
}
}
}
public class Action
{
public void execute()
{
System.out.println("execute!!!");
}
}
Struts 2 的运行过程:
核心控制器是FilterDispatcher会过滤所有的请求,如果请求以 action结尾,该请求会转入框架处理。当框架获取*action请求后,根据*action的前半部分决定调用哪个业务逻辑组件。最后根据业务逻辑组件的处理信息决定转发到哪个视图;
Struts2有三部分组成:核心控制器是FilterDispatcher,业务控制器和业务逻辑组件组件,其中核心控制器FilterDispatcher由Struts2提供,而业务逻辑控制器和业务逻辑组件要用户自己实现。
核心控制器FilterDispatcher:负责拦截所有用户的请求,如果用户的请求以action结尾,该请求就会转入Struts2框架处理。
业务控制器组件:实现Action类的实例(或者继承了ActionSupport的实例),该类经常能够返回一个字符串(逻辑视图<result>的execute方法,用来实现业务控制)。
业务逻辑组件:一般由javaBean或者EJB实现;
注意:在Struts2中处理用户请求的并不是业务逻辑,而是Action代理:过程是这样的,在Struts2框架中有一系列的拦截器,这些拦截器将HttpServletRequest请求中的参数分析出来,传入Action中,并回调execute()方法来处理用户请求。
当所有的请求被拦截器拦截时:执行流程
1,FilterDispatcher会将所有的请求转发给ActionProxy(Action代理),Action代理会根据配置文件struts.xml决定转发给那个Action;
2,在请求转发给Action的过程中,会经过一系列拦截器,这些拦截器负责将请求解析并转发给相应的Action。
3,经过相应的Action的execute()方法处理,会得到一个视图名的结果集,根据结果结合相应的模版产生相应的输出流。
4,输出流也可以经过一些列的拦截器后,传给浏览器。
对于源码根据图的理解:在Struts2的doFilter()方法中,通过execute.executeAction(request, response, mapping)执行进入了Dispatcher,生成了Dispatcher的对象,并调用了serviceAction()方法;接下里调用了create()方法生成ActionProxy的对象proxy ,通过proxy调用了Actioninvaction的的invoke()方法,接下里,Actioninvaction调用interceptor()方法,然后再
继续调用invoke()方法返回,判断是否interceptor()调用完成,直到所有的interceptor()方法调用完成,就执行*Action所对应的execute().
模拟Struts2 实现的全过程代码;
public class Main
{
public static void main(String[] args)
{
new ActionInvaction().invoke();
}
}
public interface Interceptor
{
public void interceptor(ActionInvaction invaction);
}
public class FristInterceptor implements Interceptor
{
public void interceptor(ActionInvaction invaction)
{
System.out.println(1);
invaction.invoke();
System.out.println(-1);
}
}
public class SecondInterceptor implements Interceptor
{
public void interceptor(ActionInvaction invaction)
{
System.out.println(2);
invaction.invoke();
System.out.println(-2);
}
}
public class ActionInvaction
{
List<Interceptor> interceptors = new ArrayList<Interceptor>();
int index = -1;
Action a = new Action();
public ActionInvaction()
{
this.interceptors.add(new FristInterceptor());
this.interceptors.add(new SecondInterceptor());
}
public void invoke()
{
index++;
if(index >= interceptors.size())
{
a.execute();
}
else
{
this.interceptors.get(index).interceptor(this);
}
}
}
public class Action
{
public void execute()
{
System.out.println("execute!!!");
}
}
相关文章推荐
- Struts2运行机制(MVC)的分析:
- Struts2架构分析和运行机制
- ASP.NET MVC的运行机制--url的全局分析
- ASP.NET MVC的运行机制--url的全局分析
- ASP.NET MVC的运行机制--url的全局分析
- 探究Struts2运行机制:StrutsPrepareAndExecuteFilter 源码剖析
- Struts2-12 Struts2运行流程分析
- 从jvm运行机制来分析String对象
- hadoop源代码分析(一)从wordCount开始,剖析mapreduce的运行机制
- 第二章 Struts2的工作机制及分析
- Erlang运行时源码分析之——线程进度机制
- Erlang运行时源码分析之——线程进度机制
- Asp.net mvc 深读mvc运行机制
- Ecmall二次开发指南手册,ECMALL模板解析机制.MVC架构分析及文件目录说明.
- MVC运行机制[转]
- Struts2 Hibernate Spring 运行流程与部分机制
- Java.MVC概念(Struts2 MVC SpringMvc分析)
- Spring的MVC模型运行序列分析
- Struts2运行流程分析
- Struts2的工作机制及分析