Struts2 page to action parameter mode

Source: Internet
Author: User

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

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.