Implement Action

Implement Action 

 For developers, Action is the core of the application, developers need to provide a large number of Class Action, and Action in Struts.xml configuration file. Action class contains the user request processing logic, Action also called service controller.

 Struts1 respect it, using a low-intrusive Struts2 design, Struts2 Action class inheritance does not require any Struts base class, or any interfaces Struts2, in this design, the Struts2 Action class just a plain POJO (plain Java objects), execute method generally comprises a non-parameter, so that a very good code reusability.

  Action Struts2 typically used to encapsulate HTTP request parameters, and therefore should contain Action parameter attributes corresponding to a request, and providing getter and setter methods for these properties

  For example: a user request parameter contains the user request and pass two parameters, then the user should provide Action and pass two attributes to a user request parameters package, as follows

Package com.strtus2.action; 

// process Action class requested by the user is just a regular Java class 
public  class the LoginAction {
     Private String User;
     Private String Pass; 

    public String the getUser () {
         return User; 
    } 

    public  void setUser (User String) {
         the this the .user = User; 
    } 

    public String getpass () {
         return Pass; 
    } 

    public  void SETPASS (String Pass) {
         the this .pass = Pass; 
    } 

    //Action class default processing method of user request: Execute () 
    public String Execute () { 

        // return the processing result string 
        return ResultStr; 
    } 
}

   Action to be processed even if the request contains a user HTTP request and pass two parameters, Action class may not include user and pass variables, because the system through the corresponding getter and setter methods to handle the request parameters, rather than by a variable name processing the request parameters, that is, if it contains the user's HTTP request parameters, Action class contains a user variable is not important, important is the inclusion of void setUser (String user) and String getUser () two methods

  Action class properties, not only for encapsulation request parameters, may also be used to encapsulate the processing result. For example, we want the server prompt "login success" or other information output next page, you can add a tip in the Action property, not the property provides setter and gettier methods used, both to increase as follows Class Action code.

   private String tip;

    public String getTip() {
        return tip;
    }

    public void setTip(String tip) {
        this.tip = tip;
    }

 

  

  Once the value of the property is provided in the tip of Action, Struts2 tags can be used to the next page of the output value of the attribute, attribute value such as the output of the tip in a JSP page

  <s:property value="tip"/>

   It will not be a strict distinction between the value which is used to encapsulate the Action request parameters, which property is used to encapsulate the processing results for the system, and the encapsulation package request parameter attributes processing results are completely equal. If the request contains a parameter called the tip, the system calls back void setTip Action classes (String tip) method, this method, called the tip of the request parameter can be passed Action instance, if there is no void setTip ( String tip) method, then the request parameter called tip is not passed to the Action instance.

  Similarly, in the output attribute Action JSP page, it does not distinguish between a request parameter or attribute for the package for encapsulating the processing result, therefore, the use of the tag library may be Struts2 outputs the processing result of Action, it may be output request value of the parameter.

  As can be seen from the above, using the struts2 tag library may output a simple string, using <s: property .... /> tag to control the output, in fact, Action classes can be packaged very complex attributes, including other class, arrays, collections Map objects and user-defined objects, for these types of complex output, as may be accomplished by Struts2 tag.

 

ActionSupport a base class and interfaces .Action

  In order to allow users to develop more standardized Action, Action Struts2 provides an interface that defines the specifications of Struts2 Action class should implement the process.

public  class Action {
     // definition of some of the interface Action String 
    public  static  Final String = ERROR "error" ;
     public  static  Final String the INPUT = "INPUT" ;
     public  static  Final String the LOGIN = "Login" ;
     public  static  Final String = NONE "none" ;
     public  static  Final String = SUCCESS "Success" ; 

    // Execute method defined user request 
    public String Execute () throws Exception { 

        return  null ;

    }
}

 

  Action defines a method execute, the interface specification defines an Action class should contain execute method, which returns a string, in addition, the interface also defines five string constants, their role is unity execute the return value of the method, if the developer wishes to use a specific string as return values ​​are also possible.

  In addition, Struts2 also provides a ActionSupport class, which is to achieve Action interface class, which provides a number of default method, because ActionSupport fully meet the requirements of an Action, more than we can use directly ActionSuppot as a business controller, If we are no specific configuration Action class specified (both user does not provide the Action class), the system will automatically use the class as ActionSupport Action handler class.

 

Guess you like

Origin www.cnblogs.com/zhilili/p/10923961.html