stucts2 頁面上的值如何與Action的屬性值對應

來源:互聯網
上載者:User

stucts2 頁面上的值如何與Action的屬性值對應
在Strut2中,頁面的資料和Action有兩種基本對應方式:分別是:屬性驅動(FieldDriven)和模型驅動(ModelDriven)。屬性驅動又分為兩種情況:一種是基礎資料型別 (Elementary Data Type)的屬性對應:另一種是JavaBean風格的屬性對應。下面就分別來看看它們是什麼意思都如何?。屬性驅動(FieldDriven):基礎資料型別 (Elementary Data Type)的屬性對應在index.jsp中,我是這樣寫的 

<form action="helloWorldAction.action" method="post">    帳號:<input type="text" name="account"/><br>    密碼:<input type="password" name="password"/>    <input type="submit" value="提交"/>  </form>

 

在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;    }         //其他代碼省略....}

 

可以發現,在頁面上index.jsp的name的屬性,和Action的屬性是同一個名稱的。這樣一來,當頁面提交的時候,Struts2會自動從request對象裡把資料提取出來,然後按照名稱進行對應,自動化佈建到Action屬性裡面去。2.屬性驅動FieldDriven(JavaBean風格的屬性對應) (1)先看看域對象的寫法,按照JavaBean的風格來寫,範例程式碼如下:
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)這時候Action寫法也有變化,主要就是直接使用這個對象,其實就是定義一個屬性就是這樣物件類型,然後為這個屬性提供相應的getter()和setter()方法即可。如果把這個屬性的可訪問屬性設定為public就不需要getter()和setter()方法。Action修改後的範例程式碼如下:
public class HelloWorldAction extends ActionSupport{    private HelloWorldModel hwm =  new HelloWorldModel();    public HelloWorldModel getHwm() {        return hwm;    }    public void setHwm(HelloWorldModel hwm) {        this.hwm = hwm;    }    //其他代碼省略....}

 

 (3)Action發生變化後,index.jsp頁面中的內容也要進行相應的改變,否則資料就無法正確對應,主要是在相應的name屬性上,添加一個域對象的首碼,指明這個值到底是對應到哪宇哥域對象裡面去,樣本如下: 
<form action="helloWorldAction.action" method="post">         帳號:<input type="text" name="hwm.account"/><br>         密碼:<input type="password" name="hwm.password"/>         <input type="submit" value="提交"/>    </form></body>

 

3.模型驅動(ModelDriven)   它的基本實現方式讓Action實現一個ModelDriven的介面,這個介面需要我們實現getModel的方法。這個方法的傳回值就是Action所使用的資料模型對象。修改後的範例程式碼如下: 
public class HelloWorldAction extends ActionSupport implements ModelDriven{    private HelloWorldModel hwm =  new HelloWorldModel();    public Object getModel() {        // TODO Auto-generated method stub        return hwm;    }    //其他代碼省略....}

 

在index.jsp中也需要做相應的調整,主要是是去掉剛才給name屬性添加的hwm首碼。其範例程式碼如下:
<form action="helloWorldAction.action" method="post">         帳號:<input type="text" name="account"/><br>         密碼:<input type="password" name="password"/>         <input type="submit" value="提交"/> </form>

 

為什麼種方式不需要首碼了呢?  因為使用這種ModelDriven的方式,一個Actiion只能對應一個Model,因此不需要添加首碼,Struts2j就知道,頁面上account的值就對應到這個Model的account屬性。如果添加上器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.