Struts core operating mechanism

Source: Internet
Author: User

StrutsInCLayer functions:

1. Accept client requests

2. Call the M-layer model component to execute the corresponding business logic.

3. Obtain the processing result of M-layer business logic

4. select an appropriate view based on the processing result and present it to the client.

The C-layer core control component is:ActionservletIt is a servlet Of The Org. Apache. Struts. Action. actionservlet type.

He inherits javax. servlet. http. httpservlet and implements methods used in servlet lifecycles such as Init (), doget (), dopost (), and destroy. AddedProcess ()But it calls other complex methods.

When the actionservlet receives a customer request from the servlet container, its processing steps are as follows:

1. Call the dopost () and doget () Methods Based on the browser's post or get requests, and call the process () method of actionservlet.

2. Process () gets the requestprocessor object-à calls the process () method of the requestprocessor object

3. Process () reads the struts-config.xml and searches<Action>Sub-element. The search condition is that the URI requested by the user is the same as the path attribute value of the Action Element. Locate the sub-element and determine the name attribute.<Form-bean>Element, and then according toNameFind the corresponding actionform bean.

4. Call actionform Bean'sSetterThe method fills in the formbean attributes, and then calls the validate () method of formbean to verify the validity of the form data.

5. The process () method of requestprocessor is based onTypeProperty to find the action bean object (if any, use, or create if none), and then send the formbean object, encapsulated request object, and other information to the found action object. Action bean calls its ownExecute ()Method.

6. After the execute () method of action bean is executed, an actionforward object is returned and the process () method of the requestprocessor object is returned, the process method will redirect to the specified page according to the actionforward request.

Process:

1. The customer requests Req. Do

Actionservlet> process ()> requestprocessor. Process ()> struts-config.xml>

<Action>-> Path = "/req"

<Action>-> name-> <form-bean>-> setxxx ()

<Action>-> type-> get action-> execute ()

-> Execute () returns actionforward ()

The real request handler of the struts controller when the requstprocessor class is used. It defines multiple processxxx () Methods to complete specific processing. To extend the actionservlet class, you can inherit from requstprocessor to implement your own control logic. Among them, it provides a processpreprocess () method for rewriting in the subclass, adding a custom preprocessing function.

StrutsActionform in:Using form data

The description and use of the actionform bean are separated in the struts-config.xml. Such a bean can process different request form information on multiple pages.

Declaration: <form-bean>

Use: <action name = "formbean">

After the actionform bean is defined, it must be declared in the struts-config.xml. Then, specify the correspondence between the actionform bean and the request to be processed, a radish (of course, this request contains form information ). That is, a specific actionform bean is agreed in advance in that case, which is provided in the configuration file. Similar to this:

<Action-mappings>

<Action Path = "/login" type = "com. fzxy. loginaction" name = "formbean1" Scope = "request" input = "/login. jsp"/>

<Action Path = "/regist" Forward = "/regist. jsp"/>

</Action-mappings>

This <action-mappings> element provides the correspondence between various possible client requests and processing methods, and a <action> method for processing a request URI.

<Action> attributes of an element:

Path: The page URI requested by the customer.

Type: Type of the Action object to process the request, including the package name

Name: the name of the actionform Bean used to automatically receive the form information of the path request, which is declared in <form-bean>.

Scope: the scope of application of actionform bean, usually request or session,

The dotted line indicates the first request, and the solid line indicates the second request when the range is:

RequestHourIs valid only for the current request and response. If a request is forwarded by one component to another, the object remains valid. When the server returns the response to the customer, the actionform bean is destroyed. When the range is request, the form object will overwrite the four components in the first request and response cycle, and the four components will share an actionform object. When the browser sends a second request, a new actionform object is created.

When the two requests are in a session, these components always share an actionform object.

 

Input: page to be redirected when an error occurs in processing the request

 

From: http://hi.baidu.com/baozi52/blog/item/98c928088c135531e8248877.html

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.