1、tomcat启动,初始化Fillter过滤器,public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter{}
StrutsPrepareAndExecuteFilter调用init方法->初始化FilterHostConfig config = new FilterHostConfig(filterConfig);
->dispatcher = init.initDispatcher(config);->在initDispatcher方法中调用dispatcher.init();->加载所有的资源文件
init_DefaultProperties(); // [1]
init_TraditionalXmlConfigurations(); // [2]
init_LegacyStrutsProperties(); // [3]
init_CustomConfigurationProviders(); // [5]
init_FilterInitParameters() ; // [6]
init_AliasStandardObjects() ; // [7]
Container container = init_PreloadConfiguration();
container.inject(this);
init_CheckWebLogicWorkaround(container);
2、StrutsPrepareAndExecuteFilter,通过doFilter方法过滤所有客户端的请求(整个过程就在StrutsPrepareAndExecuteFilter的doFilter()中完成各种调用)
封装request,response
HttpServletRequest request = (HttpServletRequest) req; request = prepare.wrapRequest(request);
HttpServletResponse response = (HttpServletResponse) res;
prepare.setEncodingAndLocale(request, response);//设置编码
prepare.createActionContext(request, response);//创建actionContext
调用createActionContext
{
createAcitonContext(){
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();//利用容器创建值栈
stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));//构建值栈的结构
ActionContext.setContext(ctx);//把整个actionContext放入到了当前线程中,因为actionContext中有valueStack,所以valueStack也在当前线程中,这样就保证了数据的安全性
}
说明:
1、创建actionContext对象
2、创建ValueStack
3、把整个的actionContext放入到了ThreadLocal中
}
prepare.assignDispatcherToThread();//整个的过程都在当前线程中执行
execute.executeAction(request, response, mapping);
调用ExecuteOperations类中executeAction()
{
在这Dispatcher类中调用dispatcher.serviceAction(request, response, servletContext, mapping);
}
Dispatcher中serviceAction方法
{
serviceAction(){
ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
/**
* 因为在struts2容器中有太多的参数
request,response,valueStack,session,application,paramters
所以struts2容器对当前的请求中用到所有的数据封装在了ActionContext中的map中
*/
extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
//创建actionProxy
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false);
// 在DefaultActionProxyFactory的createActionProxy方法中执行了如下的内容:
{
重点:
执行了DefaultActionInvocation中的init方法
createAction(contextMap); 创建了action
stack.push(action); 把action放入到了对象的栈顶
List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());
interceptors = interceptorList.iterator();
获取所有的拦截器,并且返回了迭代器的形式
}
proxy.execute()方法
//通过DefaultActionProxy的execute方法调用 ActionContext.setContext(invocation.getInvocationContext());
再通过invocation.getInvocationContext()方法调用类DefaultActionInvocation的invoke方法
{
invoke(){
//调用了拦截器
if (interceptors.hasNext()) {
final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
String interceptorMsg = "interceptor: " + interceptor.getName();
UtilTimerStack.push(interceptorMsg);
try {
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
}
finally {
UtilTimerStack.pop(interceptorMsg);
}
}
resultCode = invokeActionOnly();//执行action
//在执行结果集之前执行PreResultListener
if (preResultListeners != null) {
for (Object preResultListener : preResultListeners) {
PreResultListener listener = (PreResultListener) preResultListener;
String _profileKey = "preResultListener: ";
try {
UtilTimerStack.push(_profileKey);
listener.beforeResult(this, resultCode);
}
finally {
UtilTimerStack.pop(_profileKey);
}
}
}
//执行结果集
executeResult();
}
}
prepare.cleanupRequest(request);//把struts2过程中的数据全部清空了
}
}
StrutsPrepareAndExecuteFilter调用init方法->初始化FilterHostConfig config = new FilterHostConfig(filterConfig);
->dispatcher = init.initDispatcher(config);->在initDispatcher方法中调用dispatcher.init();->加载所有的资源文件
init_DefaultProperties(); // [1]
init_TraditionalXmlConfigurations(); // [2]
init_LegacyStrutsProperties(); // [3]
init_CustomConfigurationProviders(); // [5]
init_FilterInitParameters() ; // [6]
init_AliasStandardObjects() ; // [7]
Container container = init_PreloadConfiguration();
container.inject(this);
init_CheckWebLogicWorkaround(container);
2、StrutsPrepareAndExecuteFilter,通过doFilter方法过滤所有客户端的请求(整个过程就在StrutsPrepareAndExecuteFilter的doFilter()中完成各种调用)
封装request,response
HttpServletRequest request = (HttpServletRequest) req; request = prepare.wrapRequest(request);
HttpServletResponse response = (HttpServletResponse) res;
prepare.setEncodingAndLocale(request, response);//设置编码
prepare.createActionContext(request, response);//创建actionContext
调用createActionContext
{
createAcitonContext(){
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();//利用容器创建值栈
stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));//构建值栈的结构
ActionContext.setContext(ctx);//把整个actionContext放入到了当前线程中,因为actionContext中有valueStack,所以valueStack也在当前线程中,这样就保证了数据的安全性
}
说明:
1、创建actionContext对象
2、创建ValueStack
3、把整个的actionContext放入到了ThreadLocal中
}
prepare.assignDispatcherToThread();//整个的过程都在当前线程中执行
execute.executeAction(request, response, mapping);
调用ExecuteOperations类中executeAction()
{
在这Dispatcher类中调用dispatcher.serviceAction(request, response, servletContext, mapping);
}
Dispatcher中serviceAction方法
{
serviceAction(){
ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
/**
* 因为在struts2容器中有太多的参数
request,response,valueStack,session,application,paramters
所以struts2容器对当前的请求中用到所有的数据封装在了ActionContext中的map中
*/
extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
//创建actionProxy
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false);
// 在DefaultActionProxyFactory的createActionProxy方法中执行了如下的内容:
{
重点:
执行了DefaultActionInvocation中的init方法
createAction(contextMap); 创建了action
stack.push(action); 把action放入到了对象的栈顶
List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());
interceptors = interceptorList.iterator();
获取所有的拦截器,并且返回了迭代器的形式
}
proxy.execute()方法
//通过DefaultActionProxy的execute方法调用 ActionContext.setContext(invocation.getInvocationContext());
再通过invocation.getInvocationContext()方法调用类DefaultActionInvocation的invoke方法
{
invoke(){
//调用了拦截器
if (interceptors.hasNext()) {
final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
String interceptorMsg = "interceptor: " + interceptor.getName();
UtilTimerStack.push(interceptorMsg);
try {
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
}
finally {
UtilTimerStack.pop(interceptorMsg);
}
}
resultCode = invokeActionOnly();//执行action
//在执行结果集之前执行PreResultListener
if (preResultListeners != null) {
for (Object preResultListener : preResultListeners) {
PreResultListener listener = (PreResultListener) preResultListener;
String _profileKey = "preResultListener: ";
try {
UtilTimerStack.push(_profileKey);
listener.beforeResult(this, resultCode);
}
finally {
UtilTimerStack.pop(_profileKey);
}
}
}
//执行结果集
executeResult();
}
}
prepare.cleanupRequest(request);//把struts2过程中的数据全部清空了
}
}