Struts2响应请求流程

service 启动时会初始化过滤器,其中最主要的过滤器是filterDispatcher这个过滤器,filterDispatcher的初始化代码如下

public void init(FilterConfig filterConfig) throws ServletException {
try {
this.filterConfig = filterConfig;
initLogging();
dispatcher = createDispatcher(filterConfig);
dispatcher.init();
dispatcher.getContainer().inject(this);
staticResourceLoader.setHostConfig(newFilterHostConfig(filterConfig));
} finally {
ActionContext.setContext(null);
}
}

由上面的代码可以看出,在filterDispatcher的初始化中,
会初始化log,创建一个dispatcher并对他进行初始化,并调用它的container的inject方法,在这个方法中会根据
[struts-default.xml, struts-plugin.xml, struts.xml]这些配置文件,获取里面action,inceputor的配置信息,
放在ActionMapper里面。
当一个http请求过来时,经过层层过滤器,最后调用filterDispatcher过滤器,执行他的doFilter,代码如下:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
....
ActionMapping mapping;
try {
mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager());
} catch (Exception ex) {
log.error("error getting ActionMapping", ex);
dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);
return;
}
....
dispatcher.serviceAction(request, response, servletContext, mapping);
....
}

在这个方法里面会根据
传入的HTTP信息,到ActionMapper查找是否需要调用某个action类,如果需要,获取该action在配置文件中的mapping,并掉用如下dispatcher的serviceAction方法:

public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
....
try {
UtilTimerStack.push(timerKey);
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
Configuration config = configurationManager.getConfiguration();
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false);
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
// if the ActionMapping says to go straight to a result, do it!
if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
} else {
proxy.execute();
}
....
}

在serviceAction方法中会创建需要调用的action的代理类,actionProxy,调用代理类的excute()方法。代码为:
public String execute() throws Exception {
....
retCode = invocation.invoke();
....
}

如上在excute()方法中,会调用代理类的Invocation()方法,此刻,就会调用struts默认的一系列拦截器在struts-default.xml中定义。如ParametersInterceptor,ActionMappingParametersInteceptor拦截器和用户自己定义的拦截器,执行完后就会调用用户真正执行的action类的指定方法体
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值