Detailed description of the Webwork Action call method, webworkaction

Source: Internet
Author: User

Detailed description of the Webwork Action call method, webworkaction

This article mainly introduces the webwork action calling knowledge in three aspects:

1. This part of framework class relationships

2. Obtain and wrap web parameters through Webwork

3. defaactionactionproxyfactory, defaactionactionproxy, and defaactionactioninvocation

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 is more or less packaged with Web Request Parameters for your convenience. Of course, webwork is no exception.
• 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); // encapsulate the request serviceAction (request, response, getNameSpace (request), getActionName (request ), getRequestMap (request), getParameterMap (request), getSessionMap (request), getApplicationMap ();} catch (IOException e) {String message = "cocould not wrap servlet request MultipartRequestWrapper! "; Log. error (message, e); sendError (request, response, new ServletException (message, e ));}}

• 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, export 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:

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: return the name of the requested Action. For example, "MyAction. action" returns "MyAction". The specific implementation is as follows:

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);}

• 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 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 your 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.

• Check whether a value stack is available in the previous request. If yes, put it in the Map object extraContext for 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), which directly inherits the ActionContext and provides the function of accessing images directly related to the 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.exe cute (); • Use the namespace, actionName, and extraContext obtained earlier to create a call proxy ActonProxy instance, which is DefaultActionProxy, and then call ActionProxy.exe cute route to execute our logical action.exe cute.

• ActionProxy is an interface, while ActionProxyFactory is an abstract class. By default, they are completed through DefaultActionProxy and DefaultActionProxyFactory.

• 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 );} • proteactionactionproxy constructor protected DefaultActionProxy (String namespace, String actionName, Map extraContext, boolean executeResult) throws Exception {if (LOG. isDebugEn Abled () {LOG. debug ("Creating an DefaultActionProxy for namespace" + namespace + "and action name" + actionName);} this. actionName = actionName; this. namespace = namespace‑this.exe cuteResult = 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. finddefatext text ("xwork. exception. missing-package-action ", Locale. getDefault (), new String [] {namespace, actionName});} else {message = LocalizedTextUtil. finddefatext text ("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, and then obtain the configuration information of the Action in the current request through ConfigurationManager [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);}

The above is the knowledge about Action calling in Webwork. I hope it will be helpful to you.

Articles you may be interested in:
  • Webwork implements file upload and download code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.