The method of action invocation in WebWork
Learn about the WebWork action invocation in three ways:
1.Webwork getting and wrapping web parameters
2. This part of the framework-class relationship
3.DefaultActionProxyFactory, Defaultactionproxy, defaultactioninvocation
Finally to start webwork core Business class summary, webwork through the client passed the Web parameters repackaged, carry out business Action class, and feedback execution results, this article source analysis corresponds to the webwork frame flow map of the red box.
1. This part of the framework-class relationship
2.Webwork getting and wrapping web parameters
• Each Web frame is more or less wrapped around Web request parameters for ease of use, and WebWork is no exception. WebWork The entry method for each response request:
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); Package Request Requests 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));}}
• Accept the request, response parameters, and package the request parameters, this package is mainly for multimedia requests for special processing, such as file upload requests in the project, export various types of files, etc...
• After wrapping the request, the service method calls the Servletdispatche.serviceaction () method and calls Getapplicationmap, Getsessionmap, Getrequestmap, Getparametermap, Getactionname, GetNamespace 6 methods start the foreplay before the action business logic call.
The GetNamespace method is used to obtain a namespace to which an action belongs, for example: "/my/myaction.action" returns "/my", as follows:
Getactionname Returns the name of the requested action, for example: "Myaction.action" returns "Myaction", implemented 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 properties in the request, and the implementation class is Requestmap, with the following code:
The Getparametermap method returns a map containing all the parameters in the request, with the following code:
Protected Map Getparametermap (HttpServletRequest request) throws Ioexception{return Request.getparametermap ();
The Getsessionmap method returns a Map containing all the attributes in the session, and the implementation class is Sessionmap, with the following code:
Protected Map Getsessionmap (HttpServletRequest request) {return new sessionmap (request);
The Getapplicationmap method returns a map containing all the attributes in the application, and the implementation class is Applicationmap, with the following code:
webwork The attributes of the request, the parameters, the properties in the session, the attributes in the application, are encapsulated into a Map, just for the convenience of its own use.
public void Serviceaction (HttpServletRequest request, httpservletresponse Response, String namespace, String ActionName , map Requestmap, map Parametermap, map Sessionmap, map Applicationmap) {HashMap Extracontext = Createcontextmap (Requestma P, 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 ());p roxy.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, $, E);}}
• First serviceaction called Createcontextmap to create the action context (Extracontext). It wraps Javaservlet related objects and puts them into the Extracontext map object.
• Then check to see if there is a stack of values available in the previous request, and if so, put it in the Extracontext map object for this request.
Actioncontext (Com.opensymphony.xwork.ActionContext) is the context in which the action executes, and the context can be thought of as a container (in fact, our container here is a map). It holds the object that the action needs to use when it executes.
Servletactioncontext (com.opensymphony.webwork. Servletactioncontext), this class directly inherits the Actioncontext, which provides direct access to Javaservlet-related functions.
The main function of Ognlvaluestack is to access the properties of an object through an expression language.
3.DefaultActionProxyFactory, Defaultactionproxy, defaultactioninvocation
Before the play finally finished, action call three brothers to debut for the most important operation, is the following three lines of code, and WebWork Learning Road (v) please
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);
request.setAttribute(
"webwork.valueStack"
, proxy.getInvocation().getStack());
Proxy.execute (); • Create a call proxy Actonproxy instance from the previously obtained namespace, ActionName, Extracontext, which is defaultactionproxy, and then call Actionproxy.execute method to execute our logical action.execute.
Actionproxy is an interface, Actionproxyfactory is an abstract class, and by default they are performed by Defaultactionproxy and Defaultactionproxyfactory.
• There is a static variable factory in Actionproxyfactory, which points to a defaultactionproxyfactory instance with the following code:
Newvoid= Factory;} public static Actionproxyfactory GetFactory () {return factory;}
The defaultactionproxyfactory Createactionproxy method returns the Defaultactionproxy instance.
Public actionproxy Createactionproxy (String namespace, String actionname, Map extracontext) throws Exception { Setupconfigifactioniscommand (namespace, actionname);return NewDefaultactionproxy (namespace, ActionName, Extracontext,true);} The Defaultactionproxy constructor protected Defaultactionproxy (string namespace, String actionname, Map Extracontext,Booleanexecuteresult) 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 (),Newstring[] {namespace, actionname});} Else{message= Localizedtextutil.finddefaulttext ("Xwork.exception.missing-action", Locale.getdefault (),Newstring[] {actionname});}Throw Newconfigurationexception (message);} Prepare ();}
• Assign parameters such as incoming namespaces, action names to local variables, and then get configuration information for the currently requested action via ConfigurationManager [described here in 5]. It then calls its own prepare method to create a Actioninvocation object that assigns its own variable invocation. In the following Execute method, we implement our own call to action by manipulating invocation.
protected void Prepare () throws Exception {this.invocation = Actionproxyfactory.getfactory (). Createactioninvocation ( this, this.extracontext);}
The method of action invocation in WebWork