Struts2 's Action class overview

Source: Internet
Author: User
the action of Struts2

1. Action is the core of the application, and the action class contains the processing logic for user requests. The action class is also called the business controller
2. The action class is an ordinary Pojo (traditional, normal Java object containing a parameterless Execute method) without inheriting any base classes or implementing any interfaces
3. The action encapsulates the HTTP request parameters, contains the instance variables corresponding to the request parameters, and provides the corresponding setter and getter methods.

public class loginaction{
    private String user;
    User's getter and setter method public
    String GetUser () {return user;}
    public void SetUser (String user) {
        this.user=user
    }
    Public String Execute () {//default processing user request Method
        ...
        return resulterstr;  Return processing result string
    }
}

4, action can also encapsulate the processing results
5, STRUTS2 provides an action interface, so that users develop the action class more standardized.
6, STRUTS2 provides an implementation class:Actionsuport class (is Struts2 default processing class, Inherit the class, will simplify the development of action) action Access Servletapi

STRUTS2 provides a Actioncontext class with the following common methods:

Object get (Object key): Call the HttpServletRequest getattribute (String name) method
Object put (object Key,object value)
Map getapplication (): Returns a Map object that simulates the ServletContext instance
Static Actioncontext GetContext (): Obtaining a Actioncontext instance of a system
Map getparameters (): Get all Request parameters
Map getsession (): Returns a Map object that simulates the HttpSession instance
void Setapplication (Map application): Passing directly into a map instance
void Setsession (Map session) action directly accesses Servletapi

STRUTS2 provides the following interfaces:

Servletcontextaware: A ServletContext instance of direct access to Web applications
Servletrequestaware: A HttpServletRequest instance of direct access to Web applications
Servletresponseaware: A HttpServletResponse instance of direct access to Web applications

Cases

public class Action implements action,servletresponseaware{...
    Private Httpserlvetresponse response;
    Overriding the method that this interface must implement is public
    void Setservletresponse (httpservletresponse response) {
        this.response=response;
    }
    .
}
using Servletactionncontext to access Servletapi

The Servletactioncontext tool class provides a static method such as the following:

Static HttpServletRequest Getrequest ()
Static HttpServletResponse GetResponse ()
the configuration of the static ServletContext Getservletcontext () action

Configure the action in Struts.xml. Configuring the action is to complete the correspondence between the user request and the action
STRUTS2 uses packages to organize and manage action.
<package.../> elements are used to define package configurations, including the following properties

Name: Must be property. This property specifies the name of the package.
Extends: Optional. Specifies the other packages that the package inherits.
namespace: Optional attribute. Define the package namespace.
Abstract: Optional. Specifies whether to be an abstract package. An abstract package cannot contain an action definition

Namespace namespace: Primarily handle situations in the same Web application that contain an action of the same name
Defines the action, specifying at least the Name property of the action, both as the first and the URL of the request being processed
Class attribute is not required, the system defaults to using the system's Actionsupport class
The action is just a logical controller and does not generate a response directly to the viewer. When the action finishes processing a user request, the specified view resource is presented to the user.
The Configure action should configure the relationship between the logical view and the physical view resource, and the mapping relationship is defined by <result.../>

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.