struts2核心工作流程与原理做为一名技术人员,听到太多关于.net和java的比较的话题。
我想对那些技术人员说,请先了解一下什么是java(或者.net)吧,其实你根本不了解。
这是Struts2官方站点提供的Struts 2 的整体结构。
一个请求在Struts2框架中的处理大概分为以下几个步骤1.客户端提起一个(HttpServletRequest)请求,如上文在浏览器中输入”http://localhost:8080/TestMvc/add.action”就是提起一个(HttpServletRequest)请求。
2.请求被提交到一系列(主要是三层)的过滤器(Filter),如(ActionContextCleanUp、其他过滤器(SiteMesh等)、 FilterDispatcher)。
注意这里是有顺序的,先ActionContextCleanUp,再其他过滤器(SiteMesh等)、最后到 FilterDispatcher。
3.FilterDispatcher是控制器的核心,就是mvc中c控制层的核心。
下面粗略的分析下我理解的FilterDispatcher工作流程和原理:FilterDispatcher进行初始化并启用核心doFilter其代码如下:public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain ) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) res;ServletContext servletContext = filterConfig.getServletContext();// 在这里处理了HttpServletRequest和HttpServletResponse。
DispatcherUtils du = DispatcherUtils.getInstance();du.prepare(request, response);//正如这个方法名字一样进行locale、encoding 以及特殊request parameters设置try {request = du.wrapRequest(request, servletContext);//对request进行包装} catch (IOException e) {String message = "Could not wrap servlet request with MultipartReques tWrapper!";LOG.error(message, e);throw new ServletException(message, e);}ActionMapperIF mapper = ActionMapperFactory.getMapper();//得到action的mapperActionMapping mapping = mapper.getMapping(request);// 得到action 的 mappingif (mapping == null) {// there is no action in this request, should we look for a static re source?String resourcePath = RequestUtils.getServletPath(request);if ("".equals(resourcePath) && null != request.getPathInfo()) {resourcePath = request.getPathInfo();}if ("true".equals(Configuration.get(WebWorkConstants.WEBWORK_SERVE_ST ATIC_CONTENT))&& resourcePath.startsWith("/webwork")) {String name = resourcePath.substring("/webwork".length());findStaticResource(name, response);} else {// this is a normal request, let it pass throughchain.doFilter(request, response);}// WW did its job herereturn;}Object o = null;try {//setupContainer(request);o = beforeActionInvocation(request, servletContext);//整个框架最最核心的方法,下面分析du.serviceAction(request, response, servletContext, mapping);} finally {afterActionInvocation(request, servletContext, o);ActionContext.setContext(null);}}du.serviceAction(request, response, servletContext, mapping);//这个方法询问ActionMapper是否需要调用某个Action来处理这个(request)请求,如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxypublic void serviceAction(HttpServletRequest request, HttpServletResponse respo nse, String namespace, String actionName, Map requestMap, Map parameterMap, Map ses sionMap, Map applicationMap) {HashMap extraContext = createContextMap(requestMap, parameterMap, session Map, applicationMap, request, response, getServletConfig()); //实例化Map请求,询问ActionMapper是否需要调用某个Action来处理这个(request)请求extraContext.put(SERVLET_DISPATCHER, this);OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActio nContext.WEBWORK_VALUESTACK_KEY);if (stack != null) {extraContext.put(ActionContext.VALUE_STACK,new OgnlValueStack(stack)) ;}try {ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy (namespace, actionName, extraContext);//这里actionName是通过两道getActionName解析出来的, FilterDispatcher把请求的处理交给ActionProxy,下面是ServletDispatcher的 TODO:request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, pro xy.getInvocation().getStack());proxy.execute();//通过代理模式执行ActionProxyif (stack != null){request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack);}} catch (ConfigurationException e) {log.error("Could not find action", e);sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);} catch (Exception e) {log.error("Could not execute action", e);sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_E RROR, e);}}4.FilterDispatcher询问ActionMapper是否需要调用某个Action来处理这个(request)请求,如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy。
5.ActionProxy通过Configuration Manager(struts.xml)询问框架的配置文件,找到需要调用的Action类.如上文的struts.xml配置<?xml version="1.0" encoding="GBK"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configura tion 2.0//EN" "/dtds/struts-2.0.dtd"><struts><include file="struts-default.xml"/><package name="struts2" extends="struts-default"><action name="add"class="edisundong.AddAction"><result>add.jsp</result></action></package></struts>如果提交请求的是add.action,那么找到的Action类就是edisundong.AddAction。