Method One: Attribute encapsulation
(1) Declare the attribute member variable in action, requiring the name of the variable to be the same as the name in the form, and the form content code is as follows:
<form name= "Form1" method= "Post" action= "${pagecontext.request.contextpath}/data1.action" >
Name: <input name= "username" type= "text"/><br/>
Password: <input name= "Password" type= "text"/><br/>
Address: <input name= "Address" type= "text"/><br/>
<input type= "Submit" value= "Submission" >
</form>
The Code in action is as follows:
public class UserInfo extends Actionsupport {
Private String username;
private String password;
Private String address;
@Override
Public String toString () {
Return "UserInfo [GetUserName () =" + getusername () + ", getpassword () ="
+ GetPassword () + ", getaddress () =" + getaddress () + "]";
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
Public String getaddress () {
return address;
}
public void setaddress (String address) {
this.address = address;
}
Public String Execute ()
{
System.out.println (ToString ());
return NONE;
}
}
The configuration code is as follows:
<action name = "Data1" class = "Com.data.UserInfo" ></action>
Method Two: Model-driven encapsulation
(1) Implement Interface Modeldriven in action
(2) Implement the Getmodel method inside the interface, return the created object
(3) Create the entity class manually in the action. (Prerequisite: The form entry name is the same as the entity member variable name)
The code in the action is as follows:
public class Data2 extends Actionsupport implements modeldriven<userinfo>{
Private UserInfo UI = new UserInfo ();
@Override
Public UserInfo Getmodel () {
TODO auto-generated Method Stub
return UI;
}
Public String Execute ()
{
System.out.println (UI);
return NONE;
}
}
Method Three: attribute-expression Encapsulation
(1) declaring an entity class in action
(2) generating set and get methods for entity classes in action
(3) Enter name in the form using the entity class. Name method
The form code is as follows:
<form name= "Form1" method= "Post" action= "${pagecontext.request.contextpath}/data3.action" >
Name: <input name= "Userinfo.username" type= "text"/><br/>
Password: <input name= "Userinfo.password" type= "text"/><br/>
Address: <input name= "userinfo.address" type= "text"/><br/>
<input type= "Submit" value= "Submission" >
</form>
The code in the action is as follows:
public class Data3 extends Actionsupport {
Private UserInfo UserInfo;
Public UserInfo GetUserInfo () {
return userinfo;
}
public void Setuserinfo (UserInfo UserInfo) {
This.userinfo = UserInfo;
}
Public String Execute ()
{
System.out.println (userinfo);
return NONE;
}
}
The difference between model-driven and attribute encapsulation:
(1) Model drivers can assign values to only one entity class at a time, and if the data in the form wants to be assigned to two entity classes separately, the method does not
(2) Attribute encapsulation can be done, the code is as follows:
The Code in action is as follows:
public class Data3 extends Actionsupport {
Private UserInfo UserInfo;
Private User2 User2;
Public User2 GetUser2 () {
return user2;
}
public void SetUser2 (User2 user2) {
This.user2 = User2;
}
Public UserInfo GetUserInfo () {
return userinfo;
}
public void Setuserinfo (UserInfo UserInfo) {
This.userinfo = UserInfo;
}
Public String Execute ()
{
System.out.println (userinfo + "\ n" + user2);
return NONE;
}
}
The code in the form is as follows:
<form name= "Form1" method= "Post" action= "${pagecontext.request.contextpath}/data3.action" >
Name: <input name= "Userinfo.username" type= "text"/><br/>
Password: <input name= "Userinfo.password" type= "text"/><br/>
Address: <input name= "userinfo.address" type= "text"/><br/>
Gender: <input name= "User2.sex" type= "text"/><br/>
<input type= "Submit" value= "Submission" >
</form>