Struts2 response request process

When the service starts, the filter will be initialized. The most important filter is filterDispatcher. The initialization code of filterDispatcher is as follows
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);
        }
    }

As can be seen from the above code, in the initialization of filterDispatcher,
log will be initialized, a dispatcher will be created and initialized, and the inject method of its container will be called. In this method, according to
[struts-default.xml, struts -plugin.xml, struts.xml] These configuration files, get the configuration information of the action and inceptor inside, and
put them in the ActionMapper.
When an http request comes, it goes through layers of filters, and finally calls the filterDispatcher filter to execute his doFilter, the code is as follows:
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);
....
}

In this method, according to the
incoming to ActionMapper to find out whether a certain action class needs to be called. If necessary, obtain the mapping of the action in the configuration file, and use the serviceAction method of the following dispatcher:
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();
            }
....
}

In the serviceAction method, the proxy class for the action to be called, actionProxy, will be created, and the execute() method of the proxy class will be called. The code is:
public String execute() throws Exception {
....
 retCode = invocation.invoke();
....
}

As above, in the execute() method, the Invocation() method of the proxy class will be called. At this moment, the default series of struts interceptors defined in struts-default.xml will be called. Such as ParametersInterceptor, ActionMappingParametersInteceptor interceptor and user-defined interceptor, after execution, the specified method body of the action class actually executed by the user will be called

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327070501&siteId=291194637