Second, Action object

Source: Internet
Author: User
Tags http request

I. Recognize the Action object

The action object is an important object of the STRUTS2 framework, which is primarily used to process HTTP requests. In Struts2api, the Action object is an interface that is located in the

COM.OPENSYMPHONY.XWORK2 in the package. STRUTS2 project Development, create an Action object to implement this object directly or indirectly, its method is declared as follows:

public interface action{

public static final String success= "SUCCESS";

public static final String none= "NONE";

public static final String error= "ERROR";

public static final String input= "INPUT";

public static final String login= "LOGIN";

Public String Execute () throws execption;

}

In the Action interface, contains 5 static member variables, which are static variables defined by the STRUTS2API for the result

SUCCESS: Represents a successful return value for action, and if the action succeeds and needs to be returned to the Success page, you can set the return value to SUCCESS.

NONE: Performs a successful return value on behalf of the action, but does not need to return to the success page.

ERROR: Represents the action execution failed return value. eg, you can use the action to return this value in the case of some validation failures.

Input: Represents the return value that needs to be returned to a page of input information. eg, when you modify a message, the load data buckle needs to be returned to the Modify page so that the action

The return value of object processing is set to input.

Login: Represents the return value that requires the user to log on. eg, when verifying that the user is logged in, the action validation fails and requires the user to log on again, the action object can be

The return value is set to login

Second, the principle of injection of request parameters

In Struts2, the data submitted by the form is automatically injected with the corresponding property in the Action object, which is the same as the IOC injection principle in the sring framework, provided by the Action property

Setter () method for injection

public class Useraction extends actionsuppor{

User Name Properties

Private String username;

public void Setusername (String username) {

This.username = Username;

}

Public String GetUserName () {

return username;

}

Public String execute () {

return SUCCESS;

}

}

III. Basic process of action

(1) Browser request Web container

(2) Web container call Dofiter () Request STRUTS2 Filter

(3) Struts2 filter Call execute () Request action

(4) Action return success to STRUTS2 filter

(5) Sturts returns a message to message.jsp

(6) mmessage.jsp back to Browser

Browser (Request)-------->web container (DoFilter ())---->STRUTS2 filter (execute ())------>action

Because the STRUT2 filter is configured in a Web project, the Web container calls the Dofilter () method of the struts filter when the browser sends an HTTP request to the Web container.

At this point, Struts2 receives an HTTP request. Through the internal processing mechanism of struts, it will determine if the HTTP request matches an Action object, if found with this

Matches the action, the Execute () method of the Actoin object is called, and the corresponding value is returned based on the result of the action, and then Strut2 looks up the return value

The mapped page, and finally responds to the browser through a certain view.

Iv. Dynamic Action

The previously used action object is implemented by overriding the Execute () method to handle the browser request, with one action handling multiple user requests

V. Application of dynamic Action

public class Useraction extends actionsupport{

Private String info;

Add user Information

Public String Add () throws exception{

info= "Add user Information";

Return "Add"

}

Public String Update () throws exception{

info= "Update user Information";

return "Update";

}

Public String GetInfo () {

return info;

}

public void SetInfo (String info) {

This.info = info;

}

}

Struts.xml

<struts>
<!--declaration Pack--
<package name= "MyPackage" extends= "Struts-default" >
<!--define action--
<action name= "useraction" class= "Com.lyq.action.UserAction" >
<result name= "Add" >user_add.jsp</result>
<result name= "Update" >user_update.jsp</result>
</action>
</package>
</struts>

user_add.jsp

<body>

<font color= "Red" >

<s:property value= "Info"/>

</font>

</body>

user_upate.jsp

<body>

<font color= "Red" >

<s:property value= "Info"/>

</font>

</body>

idex.jsp

<body>

<a href= "Useraction!add" > Add Users </a>

<a href= "Useraction!update" > Update users </a>

</body>

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.