How does the value on the stucts2 page correspond to the property value of the Action?

Source: Internet
Author: User

How does the value on the stucts2 page correspond to the property value of the Action?
In Strut2, there are two basic mappings between page data and actions: FieldDriven and ModelDriven ). Property drivers can be divided into two types: one is the attribute correspondence of the basic data type, and the other is the attribute correspondence of the JavaBean style. Next, let's take a look at how they are implemented. FieldDriven: the attribute of the basic data type corresponds to index. jsp.

<Form action = "helloWorldAction. action "method =" post "> account: <input type =" text "name =" account "/> <br> password: <input type = "password" name = "password"/> <input type = "submit" value = "submit"/> </form>

 

This is written in Action.
Public class HelloWorldAction extends ActionSupport {private String account; private String password; public String getAccount () {return account;} public void setAccount (String account) {this. account = account;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} // other code is omitted ....}

 

You can find that the name attribute of index. jsp on the page is the same name as the attribute of Action. In this way, when the page is submitted, Struts2 automatically extracts the data from the request object, and then maps the data according to the name, which is automatically set to the Action attribute. 2. Attribute-driven FieldDriven (JavaBean-style attribute ing) (1) first, let's look at the syntax of the domain object and write it according to the JavaBean style. The sample code is as follows:
public class HelloWorldModel {         private String account;    private String password;    public String getAccount() {        return account;    }    public void setAccount(String account) {        this.account = account;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

 

(2) At this time, the statement of Action also changes. The main reason is to directly use this object. In fact, it is to define an attribute as the object type, and then provide the corresponding getter () and setter () for this attribute () method. If you set the accessible attribute of this attribute to public, the getter () and setter () methods are not required. The sample code after the Action is modified is as follows:
Public class HelloWorldAction extends ActionSupport {private HelloWorldModel hwm = new HelloWorldModel (); public HelloWorldModel getHwm () {return hwm;} public void setHwm (HelloWorldModel hwm) {this. hwm = hwm;} // other code is omitted ....}

 

(3) index. the content on the jsp page must be changed accordingly. Otherwise, the data cannot be correctly matched, mainly by adding a domain object prefix on the corresponding name attribute, specify which yuge domain object the value corresponds to. The example is as follows:
<Form action = "helloWorldAction. action "method =" post "> account: <input type =" text "name =" hwm. account "/> <br> password: <input type =" password "name =" hwm. password "/> <input type =" submit "value =" submit "/> </form> </body>

 

3. The basic implementation method of ModelDriven allows Action to implement a ModelDriven interface, which requires us to implement the getModel method. The returned value of this method is the data model object used by Action. The modified sample code is as follows:
Public class HelloWorldAction extends ActionSupport implements ModelDriven {private HelloWorldModel hwm = new HelloWorldModel (); public Object getModel () {// TODO Auto-generated method stub return hwm ;} // other Code omitted ....}

 

You also need to adjust the index. jsp to remove the hwm prefix added to the name attribute. The sample code is as follows:
<Form action = "helloWorldAction. action "method =" post "> account: <input type =" text "name =" account "/> <br> password: <input type = "password" name = "password"/> <input type = "submit" value = "submit"/> </form>

 

Why is the prefix no longer required? Because of this ModelDriven method, a Actiion can only correspond to one Model, so you do not need to add a prefix. Struts2j knows that the account value on the page corresponds to the account attribute of this Model. If you add an uploader

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.