The creation of an action in Struts2 and the method call in the action

Source: Internet
Author: User
Tags string find

The action interface in STRUCTS2 is very important to match the corresponding class handling in the Struts.xml configuration according to the corresponding action name, and if class is not written, the Execute method is executed by default using the Actionsopport class

, the success string is returned, and the corresponding processing is made. There are roughly 3 ways to create an action, 1: Create a normal class, inside can not execute method, but must implement method return string, otherwise the receiver can not receive the corresponding parameters to make corresponding processing.
2: Write a class inherits from Actionsupport, because the Actionsupport class has implemented the action method, so this kind of writing is generally overriding the Execute method in the Actionsupport class
3: Define a class to implement the action interface directly, implement the Execute interface inside
So, what is the method invocation of action, and the overall can be divided into 3 kinds:
1: Call the Execute method directly

2: Configure the Adction method to be called by means property, eg:

The name of the action was agreed at the front desk.

Processing end:

The corresponding field is matched by a wildcard * in action and given to the method in the package named Com.yc.web.actions.StudentAction (), which is the Reg () method.

3:dmi Dynamic Call method, but the official document says, in fact, is not recommended this method, because there are 2:1: The action invoked in this way, will expose the corresponding Bean class, will affect the security of the project
2: Overrides attributes such as the pass-through wildcard in Structs1.
If it's really going to work, it's also very simple, now set the default dynamic method invocation property to True,eg in Stucts.xml:

Then in the previous section the following page:

The Struts.xml configuration file is as follows:

The Personaction class is as follows:

Package com.yc.web.actions;

Import Java.util.Random;
Import com.opensymphony.xwork2.Action;


Import Com.opensymphony.xwork2.ActionSupport; /** * Actionsupport is a class that implements the action interface, which makes a simple implementation of the Execute () method, that is, return success * then we need to rewrite execute () to implement a different return * @author Feng * */P Ublic class Personaction extends actionsupport{public String execute () {System.out.println ("Execute method that accesses Personaction
		");
		Random r=new random ();
		int Result=r.nextint (2);
		if (result==0) {return "faild";
		}else{return "Success"; }}/** * through wildcards to achieve the Severlet of the OP forwarding function, if there is no matching method to directly report the exception that cannot find the method * @return */public String Add () {System.out.print
		ln ("Access to Personaction's Add Method");
	return action.success;
		} public String Delete () {System.out.println ("Access to personaction Delete method");
	return action.success;
		Public String Update () {System.out.println ("Access to Personaction Update method");
	return action.success;
		} public String Find () {System.out.println ("access to Personaction's Find Method");
	return action.success;
 }
}
Then you can match up and access the corresponding method, and make a jump to the corresponding page based on the returned string.

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.