(Struts) Action class and Related Classes

Source: Internet
Author: User
Org. Apache. Struts. action. Action class Is the heart of struts and a bridge between customer requests and business operations. Each action class is usually designed to complete some operation instead of the customer.
Once the correct action instance is determined, the execute () method of the requestprocessor class is called. The structure of this method is as follows:

// From org. Apache. Struts. action. Action class
Public Actionforward execute (actionmapping mapping, actionform form, servletrequest request, servletresponse response) Throws Exception
{
Try
{
ReturnExecute (mapping, form, (httpservletrequest) (object) request, (httpservletresponse) (object) response );
}   Catch (Classcastexception E) {
Return Null;
}
}

Public Actionforward execute (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) Throws Exception
{
Return Null;
}

Apply in strutsProgramTo provide the Implementation of The execute () method. The execute () method has four parameters: actionmapping object, actionform object, httpservletrequest object, and httpservletresponse object. The actionform object encapsulates form data. Therefore, the action class can use the getter method to obtain form data from the object and then call the model component to process the data. The action class obtains an actionforward object through the findforward () method of the actionmapping object, and forwards the processing result to the target indicated by the actionforward object.
Action example:

Package Struts. Action;

Import Java. util. arraylist;

Import Javax. servlet. http. httpservletrequest;
Import Javax. servlet. http. httpservletresponse;
Import Javax. servlet. http. httpsession;

Import Model. loginhandler;

Import Org. Apache. Struts. action. Action;
Import Org. Apache. Struts. Action. actionform;
Import Org. Apache. Struts. Action. actionforward;
Import Org. Apache. Struts. Action. actionmapping;
Import Org. Apache. Struts. Action. dynaactionform;

Import Struts. Form. loginhandlerform;

Public   Class Loginhandleraction Extends Action {

Public Actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response) {

Loginhandlerform = (Loginhandlerform) form;
// Get form data from Form
String Username = Loginhandlerform. GetUserName ();
String userpwd = Loginhandlerform. getuserpwd ();

// Generate a Session Object
Httpsession = Request. getsession ( True );
Session. removeattribute ( " Username " );
Session. setattribute ( " Username " , Username );

// Generate an arraylist
Arraylist arr =   New Arraylist ();
Arr. Add (username );
Arr. Add (userpwd );

String forward;

// Call Model Components
Loginhandler Login =   New Loginhandler ();
Boolean Flag = Login. checklogin (ARR );
If (FLAG)
Forward =   " Success " ;
Else
Forward =   " Fail " ;

// Redirection
Return Mapping. findforward (forward );

}
}

ActionmappingStores information about specific actions corresponding to specific user requests, such as input pages and forwarding pages. Actionservlet transfers actionmapping to the execute () method of the action class, and then action calls the findforward () method of actionmapping. This method returns an actionforward with the specified name, in this way, the local Forwarding is completed. If no specific actionforward is found, a null value is returned. Actionmapping classSource codeAs follows:

Package Org. Apache. Struts. Action;
Import Java. util. arraylist;

Import Org. Apache. Struts. config. actionconfig;
Import Org. Apache. Struts. config. forwardconfig;

Public   Class Actionmapping Extends Actionconfig
{
Public Actionforward findforward (string name)
{
Forwardconfig config =   This . Findforwardconfig (name );
If (Config =   Null )
Config =   This . Getmoduleconfig (). findforwardconfig (name );
Return (Actionforward) config;
}

Public String [] findforwards ()
{
Arraylist results =   New Arraylist ();
Forwardconfig [] FCs =   This . Findforwardconfigs ();
For ( Int I =   0 ; I < FCS. length; I ++ )
Results. Add (FCS [I]. getname ());
Return (String []) results. toarray ( New String [results. Size ()]);
}

Public Actionforward getinputforward ()
{< br> If ( This . getmoduleconfig (). getcontrollerconfig (). getinputforward ()
return findforward ( This . getinput ();
return New actionforward ( This . getinput ();
}
}

Actionforward class
From the above discussion of the action class, we can see that the execute () method returns an actionforward object. The actionforward object represents the logical abstract representation of a web resource. The web resources here are usually JSP pages or Java Servlets.
Actionforward is the packaging class of the resource package, so there is not much between the application and the actual resources. The actual web resource is specified only in the profile struts-config.xml, not to the extentCode. Requestdispatcher determines whether to forward or redirect the actionforward instance based on the value of the Redirect attribute.
To return an actionforward instance from an action instance, you can dynamically create an actionforward instance in the action class, or use findforward () of actionmapping () method To find a pre-configured actionforward instance in the configuration file, as shown below:
Return Mapping. findforward ("success ");
Mapping is an actionmapping instance. This program fragment can return an actionforward instance corresponding to the parameter "success. The following code is the forward element defined in the profile struts-config.xml:

  < Action
Attribute = "Studentform"
Input = "/Register. jsp"
Name = "Studentform"
Path = "/Student"
Scope = "Request"
Validate = "True"
Type = "Struts. Action. studentaction"   >
< Forward Name = "Success" Path = "/Registerok. jsp"   />
</ Action >

The findforward () method of the actionmapping class first calls the findforwardconfig () method to check whether the <action> element contains the <forward> sub-element. If yes, the <global-forwards> element fragment is checked. Once a matched actionforward instance is found, it is returned to requestprocessor from the execute () method. The findforward () method of the actionmapping class is as follows:

Public Actionforward findforward (string name)
{
Forwardconfig config =   This . Findforwardconfig (name );
If (Config =   Null )
Config =   This . Getmoduleconfig (). findforwardconfig (name );
Return (Actionforward) config;
}

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.