
1.这部分框架类关系

2.Webwork 获取和包装 web 参数
•每个Web 框架或多或少的对 Web 请求参数的包装,用来拿来方便自己使用,当然webwork 也不例外。
•Webwork 每次响应请求的入口方法:
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {try {if (encoding != null) {try {request.setCharacterEncoding(encoding);} catch (Exception localException) {}}if (locale != null) {response.setLocale(locale);}if (this.paramsWorkaroundEnabled) {request.getParameter("foo");}request = wrapRequest(request); //封装 request请求serviceAction(request, response, getNameSpace(request), getActionName(request), getRequestMap(request), getParameterMap(request), getSessionMap(request), getApplicationMap());} catch (IOException e) {String message = "Could not wrap servlet request with MultipartRequestWrapper!";log.error(message, e);sendError(request, response, , new ServletException(message, e));}}•接受 request 、response 参数,并对 request 参数进行封装,这次封装主要是针对多媒体请求进行的特殊处理,例如项目中的文件上传请求,导出各种类型文件等...protected String getNameSpace(HttpServletRequest request){String servletPath = request.getServletPath();return getNamespaceFromServletPath(servletPath);}public static String getNamespaceFromServletPath(String servletPath){servletPath = servletPath.substring(, servletPath.lastIndexOf("/"));return servletPath;}•getActionName 返回请求的Action的名字,例如:"MyAction.action"则返回"MyAction",具体实现如下:protected String getActionName(HttpServletRequest request){String servletPath = (String)request.getAttribute("javax.servlet.include.servlet_path");if (servletPath == null) {servletPath = request.getServletPath();}return getActionName(servletPath);}protected String getActionName(String name){int beginIdx = name.lastIndexOf("/");int endIdx = name.lastIndexOf(".");return name.substring(beginIdx == - ? : beginIdx + , endIdx == - ? name.length() : endIdx);}• getRequestMap 方法返回一个包含请求中所有属性的Map,具体实现类是 RequestMap,具体代码如下: protected Map getRequestMap(HttpServletRequest request){return new RequestMap(request);}protected Map getParameterMap(HttpServletRequest request) throws IOException{return request.getParameterMap();}•getSessionMap 方法返回一个包含 session 中所有属性的 Map,具体实现类是 SessionMap,具体代码如下: protected Map getSessionMap(HttpServletRequest request){return new SessionMap(request);}•getApplicationMap 方法返回一个包含 Application 中所有属性的Map,具体实现类 是ApplicationMap,具体代码如下: protected Map getApplicationMap(){return new ApplicationMap(getServletContext());}•WebWork之所以要把request 的属性、参数,session 中的属性,Application 中的属性封装成 Map,仅仅是为了自己使用方便。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);}}•首先 ServiceAction 调用了createContextMap 创建Action 上下文(extraContext)。 它将JavaServlet 相关的对象进行包装,放入extraContext Map对象里。ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);request.setAttribute("webwork.valueStack", proxy.getInvocation().getStack());
static ActionProxyFactory factory = new DefaultActionProxyFactory();public static void setFactory(ActionProxyFactory factory){factory = factory;}public static ActionProxyFactory getFactory(){return factory;}• DefaultActionProxyFactory 的 createActionProxy 方法返回了 DefaultActionProxy 实例。public ActionProxy createActionProxy(String namespace, String actionName, Map extraContext)throws Exception {setupConfigIfActionIsCommand(namespace, actionName);return new DefaultActionProxy(namespace, actionName, extraContext, true);} •DefaultActionProxy的构造函数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();}•将传入的名称空间、 Action 的名字等参数赋予本地变量,接着通过 ConfigurationManager 获得当前请求的 Action 的配置信息[这里在5中已经描述过]。接着调用自身的 prepare 方法创建一个 ActionInvocation 对象赋予自身变量 invocation。在之后的 execute 方法中通过操纵invocation 来实现我们自己写的Action 的调用。protected void prepare() throws Exception {this.invocation = ActionProxyFactory.getFactory().createActionInvocation(this, this.extraContext);}以上所示是针对Webwork中Action 调用 的相关知识,希望对大家有所帮助。