Method of action receive parameter in STRUTS2

Source: Internet
Author: User

"Turn from" http://www.cnblogs.com/bukudekong/archive/2012/03/29/2423064.html

There are three main ways to receive parameters in the Struts2 action:
1. Use the action's properties to receive parameters:
A. Definition: Define properties in the action class, create get and set methods;
B. Receive: Receive parameters via attributes, such as: UserName;
C. Send: Use attribute names to pass parameters such as: USER1!ADD?USERNAME=MAGCI;
2. Use Domainmodel to receive parameters:
A. Definition: Define the Model class, define the object of the Model class in action (no new is required), and create a Get and set method for the object;
B. Receive: Receive parameters through the object's properties, such as: User.getusername ();
C. Send: Use the object's attributes to pass parameters, such as: USER2!ADD?USER.USERNAME=MGC;
3. Use Modeldriven to receive parameters:
A. Definition: The action implements the Modeldriven generic interface, defines the object of the model class (must be New), returns the object by means of the Getmodel method;
B. Receive: Receive parameters through the object's properties, such as: User.getusername ();
C. Send: Pass parameters directly using attribute names, such as: USER2!ADD?USERNAME=MGC

There are three main ways to receive parameters in the Struts2 action:

There are three main ways to receive parameters in the Struts2 action:
1. Use the action's properties to receive parameters:
A. Definition: Define properties in the action class, create get and set methods;
B. Receive: Receive parameters via attributes, such as: UserName;
C. Send: Use attribute names to pass parameters such as: USER1!ADD?USERNAME=MAGCI;
2. Use Domainmodel to receive parameters:
A. Definition: Define the Model class, define the object of the Model class in action (no new is required), and create a Get and set method for the object;
B. Receive: Receive parameters through the object's properties, such as: User.getusername ();
C. Send: Use the object's attributes to pass parameters, such as: USER2!ADD?USER.USERNAME=MGC;
3. Use Modeldriven to receive parameters:
A. Definition: The action implements the Modeldriven generic interface, defines the object of the model class (must be New), returns the object by means of the Getmodel method;
B. Receive: Receive parameters through the object's properties, such as: User.getusername ();
C. Send: Pass parameters directly using attribute names, such as: USER2!ADD?USERNAME=MGC

Struts2 action Get table only son value
1. By attribute-driven
Jsp:

<form action="sys/login.action"method="post"> <input type="text" name="username"> <input type="submit"value="submit"></form>

Action: Obtained directly from the GET, set method.

publicclasssysAction extendsActionSupport{ privateString username; publicString login() throwsException {  System.out.println(username);  returnSUCCESS; } publicString getUsername() {  returnusername; } publicvoid setUsername(String username) {  this.username= username; }}

  


2. Model-driven mode, you must implement Modeldriven<t> interface. Inconvenient for the second way to pass multiple model

Jsp:
<form action= "Sys/login.action" method= "POST" >
<input type= "text" name= "username" >
<input type= "Submit" value= "Submit" >
</form>




Action: The Getmodel () method must be implemented

public class Sysaction extends Actionsupport implements modeldriven<user>{
private user user;

Public String Login () throws Exception {
System.out.println (Getmodel (). GetUserName ());
return SUCCESS;
}

Public User Getmodel () {
if (null = = user) {
Return user = new User ();
}
return user;
}
}




3. The third Way can be completely non-modeldriven<t> You can also use the properties of multiple model objects.

Jsp:
<form action= "Sys/login.action" method= "POST" >
<input type= "text" name= "User.username" >
<input type= "text" name= "Teacher.level" >
<input type= "Submit" value= "Submit" >
</form>



Action: The set method must be supplied

public class Sysaction extends actionsupport{
private user user;
Private Teacher Teacher;

Public String Login () throws Exception {
System.out.println (User.getusername ());
System.out.println (Teacher.getlevel ());
return SUCCESS;
}

public void SetUser (user user) {
This.user = user;
}
public void Setteacher (Teacher Teacher) {
This.teacher = teacher;
}
}

Method of action receive parameter in STRUTS2

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.