Struts1 and Struts2 principle Difference Detailed Summary

Source: Internet
Author: User
1.struts1 and struts2 are 2 completely different frameworks, in fact the STRUTS2 core is the WebWork framework 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 base class of Actionform, the design is not really pojo. The STRUTS2 core controller is the Filterdispatcher,struts 2 action instance for handling user requests, not the user-implemented business controller, but the action agent-because the user-implemented business controller is not coupled to the Servlet API, Obviously the user request cannot be processed. The Struts 2 framework provides a series of interceptors that resolve the request parameters in the HttpServletRequest request, pass them to the action, and call the Execute method of the action to handle the user request. Obviously, the process above is typical of AOP (aspect-oriented programming) approach. 
2. First of all, the STRUTS2 workflow:  Core Controller Filterdispatcher is the foundation of the Struts 2 framework, which includes the control process and processing mechanism within the framework.   Business controller action and business logic components need to be implemented by the user themselves. While developing action and business logic components  , users also need to write related configuration files for use by the core controller filterdispatcher. The   Struts 2 workflow is simple compared to struts 1, basically the same as the webwork framework, so Struts 2 is an upgraded version of   WebWork. The Struts 2 framework is divided by modules and can be divided into servlet Filters, struts core modules   blocks, interceptors, and user implementations. The Struts 2 frame chart is shown in Figure 3.1.   The process of a request in the Struts 2 framework is probably divided into the following steps.   Client submits a (HttpServletRequest) request, as above in the browser http://localhost:8080/bookcode/ch2/ Reg.action is to submit a (HttpServletRequest) request.   requests are submitted to a series of filters (mainly 3 layers), such as (Actioncontextcleanup, other   filters (Sitemesh), 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, the core of the control layer (Controller) in the MVC 2 implementation.   Filterdispatcher asks Actionmapper if you need to invoke an action to process this request (httpservlet  requests). If Actionmapper decides to call a action,filterdispatcher, give Actionproxy the requested place   reason.   Actionproxy through configuration Manager (Struts.xml) to inquire about the framework's configuration file and findTo the   action class that needs to be invoked.
For example, the user registration example will find the Userreg class. Actionproxy creates a actioninvocation instance and Actioninvocation calls action  through proxy mode.
However, before the call, Actioninvocation will load all interceptor (Interceptor)   associated with the action based on the configuration. Once the action has been executed, Actioninvocation is responsible for finding the corresponding return   result according to the configuration in the Struts.xml.   3. Struts2 should also say that the struts1 principle of work: 

Consists of Actionform and JavaBean, where actionform is used to encapsulate the user's request parameters, encapsulated into Actionform

Object that is Actionservlet forwarded to Action,action to process the user according to the request parameters inside the Actionfrom

's request. Sort out what I see and know.

For Web applications that use the Struts framework, the Actionservlet is loaded and initialized when the Web application is started. Actionservlet reads configuration information from the Struts-config.xml file and stores them in various configuration objects, such as the mapping information for the action stored in the Actionmapping object. (Struts profile Struts-config.xml: A user request is processed 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 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 with an element. These configuration information is read into memory when the system is started and is used by struts during run time. In memory, each element corresponds to an org.apache.struts.aciton.ActionMapping instance.

/**********************

The user sends a request to the server through the client, and we have configured all requests in web.xml that are in the specified servlet to be processed. For example, as long as a request (*.do) with a. Do end is processed by Org.apache.struts.action.ActionServlet. Both Actionservlet and actionmapping have been loaded into memory at the time the server was started.

Combined Login Example:

When the user logs on, the URL of the address bar is: http://localhost:8080/struts_login/login.do


Actionservlet will get the user's request and analyze the URL, it will intercept/ Login (does not contain. Do) this part, after truncation, it's purpose is to go to struts-config.xml this configuration file to find the label the value of the Path property equals the section of the action. Found, Actionservlet will be submitted to the form of data to the actionform, that actionservlet how to find actionform it. Because the action tag has an attribute of 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 attribute 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 according to the value of the type attribute, which is an instance of the object new. At the same time put the contents of the tag inside the specified class that inherits the action class (this example uses loginaction) for public Actionforward execute (actionmapping mapping, Actionform form,

HttpServletRequest request, HttpServletResponse response)

Throws Exception

This method inside the parameter mapping inside, put good, then call our own Write Class (Loginaction), and the mapping,form,request,response as an argument to the Execute method inside.

As for how to turn the problem: This is done by struts, as long as your execute can return a Actionforward object, and contains information about the steering, struts will be based on the parameters (Mapping.findforward &quot ;success") to find the corresponding tag () in the corresponding configuration file (Struts-config.xml), and then the struts framework decides who to turn to. As it has already been said, the entire steering information has been stored in the mapping, so Mapping.findforward ("success") can find the appropriate label.

*/--This is my own increase, the rest are from "proficient in struts based on MVC java.web design and development"

When Actionservlet receives a client request, the following process is performed:

(1) Retrieves the actionmapping instance that matches the user request, and returns the information that the user request path is invalid if it does not exist.

(2) If the Actionform instance does not exist, a Actionform object is created to save the form data submitted by the customer to the Actionform object.

(3) Depending on the configuration information, determine if form validation is required. If validation is required, the Actionform Validate () method is invoked.

(4) If the Actionform Validate () method returns Null to return a Actionerrors object that does not contain actionmessage, the form validation succeeds.

(5) Actionservlet determines which action is forwarded to the request based on the mapping information contained in the Actionmapping instance. If the corresponding action instance does not exist, the instance is created first and the Execute () method of the action is invoked.

(6) The Execute () method of the action returns a Actionforward object 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, which is returned to the customer.

For processes in the above process (4), if the Actionform Validate () method returns a Actionerrors object that contains one or more actionmessage, the form validation fails. At this point Actionservlet will forward the request directly to the JSP component that contains the user submitting form. In this case, the action object is not created again and the Execute () method of the action is invoked.
The last old talk about the difference between Struts1 and struts2:

First: Struts1 is started through the servlet.

STRUTS1 requires the action class to inherit an abstract base class, rather than an interface.

The STRUTS2 action class can implement either an action interface or another interface.



Second, the Sturts1 action is a single case mode, the thread is not secure.

The Struts2 action thread is safe, and the action generates an instance for each request.



Third, Sturts1 used to rely on the Serlet API, not easy to test.

Struts2 does not depend on the container, allowing the action to detach the container to be tested individually.



Struts1 use the Actionform object to capture the input. All Actionform must inherit a base class.

Struts 2 eliminates the need for a second input object by using the action attribute directly as an input property.



Five, Struts1 integrated Jstl, so the use of Jstl EL. This El has a basic object graph traversal, but the 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) to the object.



The Struts 1 Actionform properties are usually string types. Struts1 uses commons-beanutils for type conversions.

STRUTS2 uses OGNL for type conversions. A converter that provides basic and commonly used objects.



The Struts 1 supports manual checksums in the Actionform validate method, or is validated by Commons Validator extensions.

STRUTS2 supports validation through the Validate method and the Xwork checksum framework.



Nine, STRUTS1 support each module has a separate request processors (lifecycle), but all the action in the module must share the same life cycle.

STRUTS2 supports creating different lifecycles for each action through the Interceptor Stack (Interceptor Stacks). The stack can be used in conjunction with different action as needed. Http://www.linuxso.com/architecture/7694.html

10. Implementation process a) Struts1 JSP initiates HttpRequest request->servlet capture->struts.xml->namespace+actionname-> action-> Populate Form setxxx ()->action.execute ()-> "Success"->result-> set request Property-> jump target page

b) Action (JSP initiates HttpRequest request, is captured by the filter)->filterdispatcher->struts.xml->namespace+actionname->new action-> fill Form setxxx ()->action.execute ()-> "Success"->result-> set request Property-> jump target page

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.