Struts2中擷取請求參數,struts2請求參數

來源:互聯網
上載者:User

Struts2中擷取請求參數,struts2請求參數

    Action在Strucs中負責請求參數的處理,那麼他是如何?的呢?這篇博文就將給大家做個簡單的介紹。在struts中,擷取請求參數主要有一下兩種方式:


    1.屬性驅動方式  

    

    方式一:使用屬性驅動,直接將Action作為Model;但是這樣做不好的一點就是無法將參數傳到Service層進行進一步處理。

package cn.tgb.action;//方式一:使用屬性驅動,將Action作為Modelpublic class Action01 {private String name;private String passwrod;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return passwrod;}public void setPassword(String password) {this.passwrod = password;}public String login(){System.out.println(name+""+passwrod);return null;}}
    

    方式二:在Action中添加對Model的引用。這樣做的優點是請求參數直接封裝到了javaBean中,不需要在通過action複製,缺點是在頁面上還需要些ognl運算式。

package cn.tgb.action;import cn.tgb.model.User;//方式二:使用屬性驅動,在Action添加Model的引用public class Action02 {private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String login(){System.out.println(user.getName() +" "+user.getPassword());return null;}}

    2.模型驅動方式

    

    使用模型驅動需要使Action實現ModelDriven介面,這種方法需要在Action中執行個體化model對象,然後重寫ModelDriven介面中的getModel()方法,返回這個對象即可。

package cn.tgb.action;import cn.tgb.model.User;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;//方式三://擷取請求參數--使用模型驅動public class Action03 extends ActionSupport implements ModelDriven<User>{private User user = new User();@Overridepublic User getModel() {return user;}public String login(){System.out.println(user.getName() + "  " + user.getPassword());return null;}}

    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.