1. Basic attribute Injection
We can pass the form data item directly to the action, and the action only needs to provide the basic property to receive the parameter, which is called the basic attribute injection. For example
JSP page:
<S:formMethod= "POST"Action= "/login"> <S:textfieldname= "username"label= "User name"/> <S:passwordname= "Password"label= "Password"/> <S:submit></S:submit></S:form>
Action:
PackageAction;Importcom.opensymphony.xwork2.Action;ImportCom.opensymphony.xwork2.ActionContext;Importmodels. User; Public classloginaction {PrivateString username; PrivateString password; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString Execute () {//incorrect user name or password if(!" Admin ". Equals ( This. username) | | !" 123456 ". Equals ( This. Password)) { returnAction.login; } //user name and password are correct Else{ returnaction.success; } }}
You only need to provide username and password two properties in the action, and provide the Setxxx () method, you can implement the automatic transfer of parameters, where the name and parameter name of the member variable is not consistent, only need to setxxx () for the same property name and parameter name.
2. Domain Model Injection
If there are a lot of data items on the form, we can wrap the data items in the form into entity objects and pass them to the action, and the action needs to provide the entity object properties to receive the arguments, which is called Domain model injection.
As the above example, you can define a user entity class first
Packagemodels; Public classUser {PrivateString username; PrivateString password; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; }}
In the action
PackageAction;Importcom.opensymphony.xwork2.Action;ImportCom.opensymphony.xwork2.ActionContext;Importmodels. User; Public classloginaction {Privateuser User; PublicUser GetUser () {returnuser; } Public voidsetUser (user user) { This. user =user; } PublicString Execute () {//incorrect user name or password if(user==NULL|| !" Admin ". Equals (User.getusername ()) | | !" 123456 ". Equals (User.getpassword ())) { returnAction.login; } //user name and password are correct Else{ returnaction.success; } }}
JSP page:
<name= "User.username" label= "username"/>< name = "User.password" label = "password"/> <s:submit></s:submit>
3, model-driven transmission parameters (models driven)
Action:
PackageAction;Importcom.opensymphony.xwork2.Action;ImportCom.opensymphony.xwork2.ActionContext;ImportCom.opensymphony.xwork2.ModelDriven;Importmodels. User; Public classLoginactionImplementsModeldriven<user>{ PrivateUser user=NewUser (); PublicString Login () {//incorrect user name or password if(user==NULL|| !" Admin ". Equals (User.getusername ()) | | !" 123456 ". Equals (User.getpassword ())) { returnAction.login; } //user name and password are correct Else{ returnaction.success; } } PublicUser Getmodel () {//TODO auto-generated Method Stub returnuser; }}
JSP page
<name= "username" label= "username"/>< name= "password" label= "password"/>< S:submit ></ S:submit >
Struts2 page to action parameter mode