一路走来,终于要开始 webwork 核心业务类的总结,webwork 通过对客户端传递的 web 参数重新包装,进行执行业务 Action 类,并反馈执行结果,本篇源码分析对应下图 WebWork 框架流转图中红色框的地方。
1 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException { 2 try { 3 if (encoding != null) { 4 try { 5 request.setCharacterEncoding(encoding); 6 } catch (Exception localException) { 7 } 8 } 9 if (locale != null) { 10 response.setLocale(locale); 11 } 12 if (this.paramsWorkaroundEnabled) { 13 request.getParameter("foo"); 14 } 15 request = wrapRequest(request); //封装 request请求 16 serviceAction(request, response, getNameSpace(request), getActionName(request), getRequestMap(request), getParameterMap(request), getSessionMap(request), getApplicationMap()); 17 } catch (IOException e) { 18 String message = "Could not wrap servlet request with MultipartRequestWrapper!"; 19 log.error(message, e); 20 sendError(request, response, 500, new ServletException(message, e)); 21 } 22 }
1 protected String getNameSpace(HttpServletRequest request){ 2 String servletPath = request.getServletPath(); 3 return getNamespaceFromServletPath(servletPath); 4 } 5 6 public static String getNamespaceFromServletPath(String servletPath){ 7 servletPath = servletPath.substring(0, servletPath.lastIndexOf("/")); 8 return servletPath; 9 }
1 protected String getActionName(HttpServletRequest request){ 2 String servletPath = (String)request.getAttribute("javax.servlet.include.servlet_path"); 3 if (servletPath == null) { 4 servletPath = request.getServletPath(); 5 } 6 return getActionName(servletPath); 7 } 8 9 protected String getActionName(String name){ 10 int beginIdx = name.lastIndexOf("/"); 11 int endIdx = name.lastIndexOf("."); 12 return name.substring(beginIdx == -1 ? 0 : beginIdx + 1, endIdx == -1 ? name.length() : endIdx); 13 }
protected Map getRequestMap(HttpServletRequest request){ return new RequestMap(request); }
protected Map getParameterMap(HttpServletRequest request) throws IOException{ return request.getParameterMap(); }
protected Map getSessionMap(HttpServletRequest request){ return new SessionMap(request); }
protected Map getApplicationMap(){ return new ApplicationMap(getServletContext()); }
public void serviceAction(HttpServletRequest request, HttpServletResponse response, String namespace, String actionName, Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap) { HashMap extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response, getServletConfig()); extraContext.put("com.opensymphony.xwork.dispatcher.ServletDispatcher", this); OgnlValueStack stack = (OgnlValueStack) request.getAttribute("webwork.valueStack"); if (stack != null) { extraContext.put("com.opensymphony.xwork.util.OgnlValueStack.ValueStack", new OgnlValueStack(stack)); } try { ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext); request.setAttribute("webwork.valueStack", proxy.getInvocation().getStack()); proxy.execute(); if (stack != null) { request.setAttribute("webwork.valueStack", stack); } } catch (ConfigurationException e) { log.error("Could not find action", e); sendError(request, response, 404, e); } catch (Exception e) { log.error("Could not execute action", e); sendError(request, response, 500, e); } }
前戏终于做完了,Action 调用的三兄弟要登场进行最重要的操作了,就是下面这三句代码,与 Webwork 学习之路(五)请求跳转前 xwork.xml 的读取 代码有非常相似的写法和设计:
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext); request.setAttribute("webwork.valueStack", proxy.getInvocation().getStack()); proxy.execute();
static ActionProxyFactory factory = new DefaultActionProxyFactory(); public static void setFactory(ActionProxyFactory factory){ factory = factory; } public static ActionProxyFactory getFactory(){ return factory; }
public ActionProxy createActionProxy(String namespace, String actionName, Map extraContext)throws Exception { setupConfigIfActionIsCommand(namespace, actionName); return new DefaultActionProxy(namespace, actionName, extraContext, true); }
protected DefaultActionProxy(String namespace, String actionName, Map extraContext, boolean executeResult) throws Exception{ if (LOG.isDebugEnabled()) { LOG.debug("Creating an DefaultActionProxy for namespace " + namespace + " and action name " + actionName); } this.actionName = actionName; this.namespace = namespace; this.executeResult = executeResult; this.extraContext = extraContext; this.config = ConfigurationManager.getConfiguration().getRuntimeConfiguration().getActionConfig(namespace, actionName); if (this.config == null) { String message; String message; if ((namespace != null) && (namespace.trim().length() > 0)) { message = LocalizedTextUtil.findDefaultText("xwork.exception.missing-package-action", Locale.getDefault(), new String[] { namespace, actionName }); } else { message = LocalizedTextUtil.findDefaultText("xwork.exception.missing-action", Locale.getDefault(), new String[] { actionName }); } throw new ConfigurationException(message); } prepare(); }
protected void prepare() throws Exception { this.invocation = ActionProxyFactory.getFactory().createActionInvocation(this, this.extraContext); }