这里把webwork的工作流程作一个简单的记录吧,算是这里看源码的总结:
当一个*.action请求过来之后 ,被com.opensymphony.webwork.dispatcher.ServletDispatcher这个类截获,很自然这个Servlet首先会调用
- public void init(ServletConfig servletConfig) throws ServletException {
- super.init(servletConfig);
- DispatcherUtils.initialize(getServletContext());
- }
- public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException
这个方法,剩下所有事情都发生到这里了~~~~
- public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
- DispatcherUtils du = DispatcherUtils.getInstance();
- //根据前面的初始数据对字符集、本地化等一些基本信息进行设置
- du.prepare(request, response);
- //(一)、通过request对象,读取URL来得到action的name和methodName
- ActionMapping mapping = ActionMapperFactory.getMapper().getMapping(request);
- if (mapping == null) {
- try {
- response.sendError(404);
- } catch (IOException e) {
- LOG.error("Could not send 404 after not finding any ActionMapping", e);
- }
- return;
- }
- try {
- //对HttpServletRequest进行了包装
- request = du.wrapRequest(request, getServletContext());
- } catch (IOException e) {
- String message = "Could not wrap servlet request with MultipartRequestWrapper!";
- LOG.error(message, e);
- throw new ServletException(message, e);
- }
- //转发继续处理
- du.serviceAction(request, response, getServletContext(), mapping);
- }
在这里有必要看一下上面的(一)发生了什么
首先通过工厂类来得到一个ActionMaper的实例,ActionMaper就是提供一个request到action的mapping(映射),它其实做很少的工作,它并不会去考虑这个映射的合法性。工厂类通过读取default.properties属性文件里的
- webwork.mapper.class=com.opensymphony.webwork.dispatcher.mapper.DefaultActionMapper
得到要实例化那个对象,这个对象会根据URL得到nameSpace,actionnName,methodNamet等信息。最后转发到DispatcherUtils对象的serviceAction(request, response, getServletContext(), mapping);方法继续处理。
在这个方法中做的主要工作就是创建一个DefaultActionProxy对象,它是ActionProxy默认实现,它就是一个从webwork到xwork的action之间的一个桥梁
在DefaultActionProxy的构建函数中,有通过
- config = ConfigurationManager.getConfiguration().getRuntimeConfiguration().getActionConfig(namespace, actionName);//这里完成解析xwork.xml的工作
这段代码解析xwork.xml文件,实际上它最后通过XmlConfigurationProvider这个类进行XML文件的解析,在DefaultActionProxy这个对象的构建过程中还会创建一个DefaultActionInvocation对象,它是ActionInvocation的默认实现,action执行就是在这里完成的,包括所有interptors和action执行后的result的执行,都在这里完成,这个类的关键是invoke()这个方法,
- public String invoke() throws Exception {
- if (executed) {
- throw new IllegalStateException("Action has already executed");
- }
- //执行interceptors,这里用的责任链模式,最好看一下这个模式,才能对这段代码怎么运行有一个更好的理解
- if (interceptors.hasNext()) {
- Interceptor interceptor = (Interceptor) interceptors.next();
- resultCode = interceptor.intercept(this);
- } else {
- //在action之后调用Action类中的方法,并返回result代码
- resultCode = invokeActionOnly();
- }
- // this is needed because the result will be executed, then control will return to the Interceptor, which will
- // return above and flow through again
- // 这里干什么不太清楚
- if (!executed) {
- if (preResultListeners != null) {
- for (Iterator iterator = preResultListeners.iterator();
- iterator.hasNext();) {
- PreResultListener listener = (PreResultListener) iterator.next();
- listener.beforeResult(this, resultCode);
- }
- }
- // 在这里执行result
- if (proxy.getExecuteResult()) {
- executeResult();
- }
- executed = true;
- }
- return resultCode;
- }