1.struts1 and struts2 are 2 completely different frameworks, in fact Struts2 core is the WebWork framework The STRUTS1 takes Actionservlet as the core controller, and the Actionservlet is responsible for intercepting all requests from the user. The Struts 1 Framework has 3 important components: Action, Actionform, and Actionforward objects.
Actionform must implement the Actionform base class, which is not a real pojo in design. The STRUTS2 core controller is the action instance that Filterdispatcher,struts 2 uses to process user requests, not a user-implemented business controller, but an action proxy-because the user-implemented business controller is not coupled with the Servlet API, Obviously unable to process user requests. The Struts 2 framework provides a series of interceptors, which are responsible for parsing the request parameters in the HttpServletRequest request, passing in the action, and callback the action's Execute method to handle the user request. Obviously, the above process is a typical AOP (aspect-oriented programming) approach.
2. Let's start with the Struts2 workflow. The: Core controller Filterdispatcher is the foundation of the Struts 2 framework and contains the control processes and processing mechanisms within the framework. Business controller action and business logic components are required to be implemented by the user. While developing the action and business logic components , the user also needs to write the relevant configuration files for use by the core controller filterdispatcher. The Struts 2 workflow is simpler than struts 1 and basically the same as the webwork framework, so Struts 2 is an upgraded version of WebWork. The Struts 2 framework is divided into modules, which can be divided into servlet Filters, struts core modules blocks, interceptors, and user implementations. The Struts 2 frame structure is shown in Figure 3.1. The processing of a request in the Struts 2 framework is roughly divided into the following steps. Client submits a (HttpServletRequest) request, as previously entered in the browser http://localhost:8080/bookcode/ch2/ Reg.action is to submit a (HttpServletRequest) request. The request is submitted to a series (mainly 3 layers) of filters (filter) such as (Actioncontextcleanup, other over filters (Sitemesh, etc.), filterdispatcher). Note: Here is the order, first actioncontext CleanUp, and then other filters (Othter Filters, Sitemesh, etc.), and finally to Filterdispatcher. Filterdispatcher is the core of the controller, which is the core of the control Layer (Controller) in the implementation of the Struts 2 of MVC. Filterdispatcher asked if Actionmapper needed to invoke an action to process the request (httpservlet requests). If Actionmapper decides to call one of the Action,filterdispatcher, give Actionproxy the requested place . Actionproxy through configuration Manager (Struts.xml) to ask for a framework profileTo the action class that needs to be called.
For example, the user registration example will find the Userreg class. Actionproxy creates an Actioninvocation instance, and Actioninvocation calls action through proxy mode.
But before the call, Actioninvocation loads all interceptor (interceptors) that are related to the action based on the configuration. Once the action is executed, Actioninvocation is responsible for finding the corresponding return result results based on the configuration in the Struts.xml. 3. Struts2 should also say how STRUTS1 works: consists of Actionform and JavaBean, where actionform is used to encapsulate the user's request parameters, encapsulated as actionform objects , the object is Actionservlet forwarded to Action,action to process the user request based on the request parameters inside the Actionfrom. Sort out what I see and know. for Web applications that use the Struts framework, the Actionservlet is loaded and initialized when the Web app is launched. Actionservlet reads the configuration information from the Struts-config.xml file and stores them in various configuration objects, such as the action mapping information stored in the Actionmapping object. (Struts configuration file Struts-config.xml: A user request is handled and forwarded through Actionservlet.) So, how does Actionservlet decide which action object to forward the user request to. This requires some configuration information that describes the user request path and the action mapping relationship. In struts, these configuration mapping information is stored in a specific XML file Struts-config.xml. In this configuration file, the mapping information for each action is configured through an element. These configuration information is read into memory when the system is started and is used by struts during operation. In memory, each element corresponds to an instance of org.apache.struts.aciton.ActionMapping) /********************** The user sends a request to the server via the client, And we've configured all of the requests in the Web. Xml to be in a certain format to be made by the specified servletProcessing. For example, any request that ends with a. Do (*.do) is handled by Org.apache.struts.action.ActionServlet. Both Actionservlet and actionmapping are loaded into memory when the server is started. Combined Login Example: when a user logs in, the URL of the address bar is: http://localhost:8080/struts_login/login.do Actionservlet will get the user's request and parse the URL , it will intercept the/login (does not contain. Do) section, after it is truncated, its purpose is to go to struts-config.xml this configuration file to find the label of the Path property is equal to the section of the action. When found, Actionservlet will put the form submitted to the data deposited to actionform, that actionservlet how to find actionform it. Because there is a property in the action tag that is name, its value is the name of the actionform you are looking for, so Actionservlet will go to the label above (note not) The value of the Name property in the tag is equal to the value of name in the action tag, and then the corresponding data set in the form is entered, based on the value of the type attribute inside, which is the object new instance of the reference. At the same time, put the contents of the tag into the specified class that inherits the action class (this example uses loginaction) of the public Actionforward execute (actionmapping mapping, Actionform form, HttpServletRequest request, httpservletresponse response) throws exception This method inside the parameter mapping inside, put it right after , then call our own class (Loginaction), and Mapping,form,request,response as a parameter to the Execute method inside. The question of how to turn: This is done by struts, as long as your execute can return a Actionforward object and contains information about the turn, struts will be based on the parameters (Mapping.findforward ( "success") ("success") go to the corresponding tag (struts-config.xml) in the corresponding configuration file (), and then decide who to turn to by the struts framework. As mentioned above, the entire steering information has been stored in the mapping, so Mapping.findforward ("success") can find the corresponding label. */--This is my own addition, the rest comes from "Mastering struts based on MVC java.web design and development" when Actionservlet receives a customer request, the following process is executed: (1) Retrieves the actionmapping instance that matches the user request and returns information that the user request path is invalid if it does not exist. (2) If the Actionform instance does not exist, create a Actionform object and save the form data submitted by the customer to the Actionform object. (3) Determines whether form validation is required based on configuration information. If validation is required, call Actionform's Validate () method. (4) If Actionform's Validate () method returns NULL, returns a Actionerrors object that does not contain actionmessage, indicating that the form validation was successful. (5) Actionservlet determines which action to forward the request to, based on the mapping information contained in the Actionmapping instance. If the corresponding action instance does not exist, create the instance first, and then call the action's execute () method. (6) The Execute () method of the action returns a Actionforward object that actionservlet the client request to the JSP component that the Actionforward object points to. (7) The JSP component that the Actionforward object points to generates a dynamic Web page that is returned to the customer. for the process above (4), if Actionform's Validate () method returns a Actionerrors object that contains one or more actionmessage, it indicates that the form validation failed. At this point Actionservlet will forward the request directly to the JSP component that contains the user submission form. In this case, the action object is no longer created and the e of the action is calledXecute () method.
The last old talk about the difference between Struts1 and struts2: First: STRUTS1 is initiated through the servlet. STRUTS1 requires the action class to inherit an abstract base class, not an interface. The action class of STRUTS2 can implement an action interface, or it can implement other interfaces. STURTS1 Action is a singleton mode, and threads are unsafe. The STRUTS2 action thread is secure, and the action generates an instance for each request. STURTS1 used to rely on Serlet API, it is not easy to test. STRUTS2 does not depend on the container, allowing the action to be tested separately from the container. STRUTS1 Use the Actionform object to capture the input. All Actionform must inherit a base class. Struts 2 uses the Action property directly as an input property, eliminating the need for a second input object. STRUTS1 integrates the Jstl, so Jstl EL is used. This El has a basic object graph traversal, but support for collections and indexed properties is weak. STRUTS2 can use Jstl, but it also supports a more powerful and flexible expression language--"object Graph Notation language" (OGNL) . , Struts 1 uses the standard JSP mechanism to bind objects to the page to access them. Struts 2 Use " valuestack" technology enables taglib to access values without having to bind your page (view) and objects. The Struts 1 Actionform properties are usually string types. Struts1 uses commons-beanutils for type conversion. STRUTS2 use OGNL for type conversion. A converter that provides basic and common objects. Struts 1 supports manual validation in the Validate method of the Actionform, or is verified by Commons Validator extensions. STRUTS2 supports validation through the Validate method and the Xwork check framework. Nine, STRUTS1 support eachEach module has a separate request processors (life cycle), but all the actions in the module must share the same life cycle. STRUTS2 supports the creation of a different life cycle for each action through the Interceptor Stack (Interceptor Stacks). Stacks can be used in conjunction with different actions as needed.
Http://www.linuxso.com/architecture/7694.html
10. Execution process a) Struts1 JSP initiates HttpRequest request->servlet capture->struts.xml->namespace+actionname-> action-> Fill Form setxxx ()->action.execute (), "Success"->result-> set request property, jump target page
b) Action (JSP initiates HttpRequest request, captured by filter)->filterdispatcher->struts.xml->namespace+actionname->new action-> fill Form setxxx ()->action.execute (), Success->result-> set request property, jump target page