Webwork learning path (6) Action call, webworkaction
Along the way, we finally need to begin a summary of core webwork business classes. webwork re-wraps the web parameters passed by the client to execute the Business Action class and feedback the execution results, this article analyzes the source code corresponding to the red box in the flow chart of the WebWork framework.
1. This part of framework class relationships
2. Obtain and wrap web parameters through Webwork
- Each Web framework packages Web Request Parameters more or less for your convenience. Of course, webwork is no exception.
- 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); // encapsulate request 16 serviceAction (request, response, getNameSpace (request), getActionName (request ), getRequestMap (request), getParameterMap (request), getSessionMap (request), getApplicationMap (); 17} catch (IOException e) {18 String message = "cocould not Wrap servlet request with MultipartRequestWrapper! "; 19 log. error (message, e); 20 sendError (request, response, 500, new ServletException (message, e); 21} 22}
- Accept the request and response parameters and encapsulate the request parameters. This encapsulation is mainly for special processing of multimedia requests, such as file upload requests in projects and exporting various types of files...
- After the request is packaged, the service method calls ServletDispatche. serviceAction () method, and call getApplicationMap, getSessionMap, getRequestMap, getParameterMap, getActionName, and getNameSpace to start the foreplay before calling the Action business logic.
- The getNameSpace method is used to obtain the namespace of an Action. For example, "/my/MyAction. action" returns "/my". The specific implementation is as follows:
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 }
- GetActionName: the name of the requested Action. For example, "MyAction. action" indicates that "MyAction" is returned. The specific implementation is as follows:
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 }
- The getRequestMap method returns a Map containing all the attributes in the request. The specific implementation class is RequestMap. The Code is as follows:
protected Map getRequestMap(HttpServletRequest request){ return new RequestMap(request); }
- The getParameterMap method returns a Map containing all the parameters in the request. The Code is as follows:
protected Map getParameterMap(HttpServletRequest request) throws IOException{ return request.getParameterMap(); }
- The getSessionMap method returns a Map containing all the attributes of the session. The implementation class is SessionMap. The Code is as follows:
protected Map getSessionMap(HttpServletRequest request){ return new SessionMap(request); }
- The getApplicationMap method returns a Map containing all attributes of the Application. The specific implementation class is ApplicationMap. The Code is as follows:
protected Map getApplicationMap(){ return new ApplicationMap(getServletContext()); }
- WebWork encapsulates request attributes, parameters, session attributes, and Application attributes into Map for convenience.
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 first calls createContextMap to create the Action context (extraContext ). It wraps objects related to assumervlet and puts them in the extraContext Map object.
- Then, check whether there is a value stack available in the previous request. If so, put it into the Map object extraContext for use in this request.
- ActionContext (com. opensymphony. xwork. actionContext is the context of Action execution. The context can be considered as a container (in fact, the container here is only a Map). It stores the objects required for Action execution.
- ServletActionContext (com. opensymphony. webwork. ServletActionContext). This class directly inherits ActionContext and provides the function of accessing images directly related to assumervlet.
- The main function of OgnlValueStack is to access object attributes through expression languages.
3. defaactionactionproxyfactory, defaactionactionproxy, and defaactionactioninvocation
After the foreplay is finished, the three brothers who call the Action are about to perform the most important operations. The following three codes are the same as the Webwork learning path. (5) The xwork before the request jumps. the xml read code has very similar writing methods and designs:
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);
request.setAttribute("webwork.valueStack", proxy.getInvocation().getStack());
proxy.execute();
- Create an ActonProxy instance by using the namespace, actionName, and extraContext obtained earlier. Here, DefaultActionProxy is called, and then ActionProxy.exe cute route is called to execute our logic action.exe cute.
- ActionProxy is an interface, and ActionProxyFactory is an abstract class. By default, they are completed through DefaultActionProxy and defaactionactionproxyfactory.
- There is a static variable factory in ActionProxyFactory, which points to a defaactionactionproxyfactory instance. The Code is as follows:
static ActionProxyFactory factory = new DefaultActionProxyFactory(); public static void setFactory(ActionProxyFactory factory){ factory = factory; } public static ActionProxyFactory getFactory(){ return factory; }
- The createActionProxy method of defaactionactionproxyfactory returns the defaactionactionproxy instance.
public ActionProxy createActionProxy(String namespace, String actionName, Map extraContext)throws Exception { setupConfigIfActionIsCommand(namespace, actionName); return new DefaultActionProxy(namespace, actionName, extraContext, true); }
- Actionactionproxy Constructor
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(); }
- Assign the input namespace, Action name, and other parameters to the local variable. Then, use ConfigurationManager to obtain the configuration information of the Action in the current request. [This is described in Section 5]. Call its own prepare method to create an ActionInvocation object and assign its own variable invocation. In the subsequent execute method, we can call the Action we write by manipulating invocation.
protected void prepare() throws Exception { this.invocation = ActionProxyFactory.getFactory().createActionInvocation(this, this.extraContext); }