目 录
1. JAVA开源
1.1 Xwork2 源码阅读(一) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Xwork2 源码阅读(二) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Xwork2 源码阅读(三) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4 Xwork2 源码阅读(四) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.5 Xwork2 源码阅读(五) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
http://gutou9.javaeye.com
第 2 / 28 页
1.1 Xwork2 源码阅读(一)
发表时间: 2008-11-17
利用工作之余,看了一下xwork2 的源码,借此学学设计模式,
并仿照Struts2 和 xwork2, 写了一个简单的mvc框架。
这东西容易忘,遂把这段时间的成果整理一下,不然过段时间忘光了就可惜了。
写的也比较粗糙,望大家多提意见,一起学习提高。
我的email : shaolz1982@gmail.com
内容:
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:
http://gutou9.javaeye.com
1.1 Xwork2 源码阅读(一)
第 3 / 28 页
看下源码:
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")) {
http://gutou9.javaeye.com
1.1 Xwork2 源码阅读(一)
第 4 / 28 页
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;
http://gutou9.javaeye.com
1.1 Xwork2 源码阅读(一)
第 5 / 28 页