struts1里依赖的核心控制器ؓ(f)ActionServlet而struts2依赖ServletDispatcher,一个是servlet一个是filter,正是采用了filter才不至于和servlet耦合Q所有的数据 都是通过拦截器来实现Q如下图昄Q?/blockquote> ?jpg)
- web层表现层的丰富,struts2已经可以使用jsp、velocity、freemarker
- U程模式斚wQstruts1的action是单例模式而且必须是线E安全或同步的,是struts2的actionҎ(gu)一个请求都产生一个新的实例,因此没有U程安全? 题?/li>
- 装h参数Q是struts1采用ActionForm装h参数Q都必须l承ActionForm基类Q而struts2通过bean的属性封装,大大降低了耦合?/li>
- cd转换Qstruts1装的ActionForm都是StringcdQ采用Commons- Beanutilsq行cd转换Q每个类一个{换器Qstruts2采用OGNLq行cd? 换,支持基本数据cd和封装类型的自动转换?/li>
- 数据校验Qstruts1在ActionForm中重写validateҎ(gu)Qstruts2直接重写validateҎ(gu)Q直接在action里面重写卛_Q不需要承Q何基c,实际的调用顺序是Qvalidate()-->execute()Q会(x)在执行execute之前调用validate,也支持xwork校验框架来校验?/li>
其次Q讲一下ؓ(f)什么要采用webwork来重新设计struts2
首先的从核心控制器谈Pstruts2的FilterDispatcherQ这里我们知道是一个filter而不是一个servlet,讲到q里很多不是很清楚web.xml里它们之间的联系Q先短讲一下它们的加蝲序Qcontext-param(应用范围的初始化参数)-->listener(监听应用端的M修改通知)-->filter(qo(h))-->servlet?br /> filter在执行servlet之间׃?qing)调用了Q所以才有可能解脱完全依赖servlet的局面,那我们来看看q个filter做了什么事情:(x)
/** * Process an action or handle a request a static resource.
* <p/>
* The filter tries to match the request to an action mapping.
* If mapping is found, the action processes is delegated to the dispatcher's serviceAction method.
* If action processing fails, doFilter will try to create an error page via the dispatcher.
* <p/>
* Otherwise, if the request is for a static resource,
* the resource is copied directly to the response, with the appropriate caching headers set.
* <p/>
* If the request does not match an action mapping, or a static resource page,
* then it passes through.
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
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 {
// FIXME: this should be refactored better to not duplicate work with the action invocation
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
ActionContext ctx = new ActionContext(stack.getContext());
ActionContext.setContext(ctx);
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 (staticResourceLoader.canHandle(resourcePath)) {
staticResourceLoader.findStaticResource(resourcePath, 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);//qo(h)用户hQ拦截器执行Q把对应的actionh转到业务action执行 }
finally {
try {
ActionContextCleanUp.cleanUp(req);
} finally {
UtilTimerStack.pop(timerKey);
}
}
}
对应的action参数由拦截器获取?br /> 解耦servlet是struts2采用webwork思\的最重要的一个原因,也迎合了整个技术的一个发展方向,解耦一直诏I于整个框架?/span>

]]>