Xwork2 源码阅读(一)

利用工作之余,看了一下xwork2 的源码,借此学学设计模式,
并仿照Struts2 和 xwork2, 写了一个简单的mvc框架。

 

这东西容易忘,遂把这段时间的成果整理一下,不然过段时间忘光了就可惜了。
写的也比较粗糙,望大家多提意见,一起学习提高。

我的email : [email protected]

 

内容:
xwork-2.1.1 源码,
部分struts-2.0.11.2源码 (核心代码为 xwork-2.1.1, 在其上增加了部分外围功能),
 
Action 调用过程:

FilterDispatcher -> Dispatcher ->

DefaultActionProxy ->DefaultActionInvocation,Interceptor -> Result

 

web.xml

 

                <filter>
		<filter-name>minimvc</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>minimvc</filter-name>
		<url-pattern>/*.action</url-pattern>
	</filter-mapping>

 

在web.xml中定义mvc 中转类,

FilterDispatcher:

 

看下源码:

 

public class FilterDispatcher implements StrutsStatics, Filter {

 

可以看到 FilterDispatcher 是一个 Filter, 而不是像struts1.x 一样的 servlet。

 

当我们提交请求时, 请求会先进入这个filter,执行 doFilter 方法。

 

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {


        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        ServletContext servletContext = getServletContext();

        String timerKey = "FilterDispatcher_doFilter: ";
        try {
            UtilTimerStack.push(timerKey);
            request = prepareDispatcherAndWrapRequest(request, response);
            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;
            }

            if (mapping == null) {
                // there is no action in this request, should we look for a static resource?
                String resourcePath = RequestUtils.getServletPath(request);

                if ("".equals(resourcePath) && null != request.getPathInfo()) {
                    resourcePath = request.getPathInfo();
                }

                if (serveStatic && resourcePath.startsWith("/struts")) {
                    String name = resourcePath.substring("/struts".length());
                    findStaticResource(name, request, response);
                } else {
                    // this is a normal request, let it pass through
                    chain.doFilter(request, response);
                }
                // The framework did its job here
                return;
            }

            dispatcher.serviceAction(request, response, servletContext, mapping);

        } finally {
            try {
                ActionContextCleanUp.cleanUp(req);
            } finally {
                UtilTimerStack.pop(timerKey);
            }
        }
    }

 

 其中

 

request = prepareDispatcherAndWrapRequest(request, response);

 

对 request 进行一些处理,如设置字符集。

mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager());

 

对request请求进行判断,如果是调用的action,则返回非空的 ActionMapping

 

public class ActionMapping {

    private String name;
    private String namespace;
    private String method;
    private Map params;
    private Result result;

 

ActionMapping 是struts2 定义的一个类,内容是action的一些描述信息,

类似 xwork2 里边的 ActionConfig。

(关于这一步骤以及涉及的类,里边东西也不少,并没有仔细看,以后有机会在补充)

如非空,说明是要调用action, 进入框架的 Action调用流程,

 

 

dispatcher.serviceAction(request, response, servletContext, mapping);

 

 

你可能感兴趣的:(设计模式,mvc,框架,struts,Gmail)