Struts2 Learning (3) and struts2 Learning

Source: Internet
Author: User

Struts2 Learning (3) and struts2 Learning
I. Three methods to access Servlet APIs 1. What is Action to access Servlet's API1 and Servlet's API:

1. Get the request object.

2. Accept request parameters.

3. Obtain the session and appliction.

4. Get the response object.

5. Use cookies.

6. Get the scope object and set the shared data (setAttribute ).

2. Action can access Servlet APIs in three ways: 1. perceive the API.

If an Action wants to obtain a ServletAPI object, it implements the sensing interface corresponding to the object.

For example:

To obtain the request object, the Action should implement ServletRequestAware.

To obtain the response object, the Action should implement ServletResponsetAware.

And overwrite the corresponding setter method.

2. Use the ServletActionContext tool class.

This class provides many static methods to directly obtain Servlet API objects.

HttpServletRequest request = ServletActionContext. getRequest ();

HttpServletResponse response = ServletActionContext. getResponse ();

It is very frequent, simple, and direct in development.

However, it is still coupled with Servlet APIs.

3. Use the ActionContext tool class.

Indicates the context object of the Action, that is, the stage in which the Action exists. ActionContext is the parent class of ServletActionContext.

1. Create an ActionContext object:

ActionContext ctx = ActionContext. getContext ();

Map <String, Object> getParameters (): gets the Map set of parameters.

2. Action request scope:

Void put (String key, Object value): similar to request. setAttribute (String key, Object value );

Object get (String key): similar to request. getAttribute (String key );

3. Operation session scope:

Map <String, Object> sessionMap = ctx. getSession ();

Store data in the session: void put (String key, Object value)

Retrieve the data in the session: Object get (String key)

4. Operation application Scope:

Map <String, Object> applicationMap = ctx. getApplication ();

Note: The third method is officially recommended, which can be unbound from the Servlet API. The second method is widely used in actual development.

2. Three Ways for Action to obtain Request Parameters 1. J2EE best practices:

Perform hierarchical development (presentation layer, business layer, and persistence layer) According to function responsibilities ):

Performance layer: interaction with users

1. Accept request data.

2. encapsulate data into Model objects.

3. Call the business logic method to process the request.

4. Control the page Jump.

2. MVC ideas:

M: data model object (encapsulated data, encapsulated operations, reusable)

V: JSP

C: StrutsPrepareAndExecuteFilter (front-end Controller)

Problem:What role does Action play?

The role of the Action to process the request.

3. Action:

First, Action itself acts as a Model object and is encapsulated by the setter method (attribute injection)

The front-end page submits logon information.

Type 2: create an independent Model object. The page is encapsulated by ognl expressions (attribute injection)

Third: Use the ModelDriven interface to encapsulate the request data (model-driven)

 

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.