strut2接收參數的三種方式(屬性驅動\對象驅動\模型驅動)說明

來源:互聯網
上載者:User

strut2接收參數有三種方式(屬性驅動\對象驅動\模型驅動),分別對三種進行一個總結: 一.屬性驅動

Jsp代碼

<body>

       <h1>屬性驅動</h1>

       <form action="${pageContext.request.contextPath}/Demo1Action" method="post">

              使用者名稱:<input type="text" name="name"  ><br>

              年齡:<input type="text" name="age"  ><br>

              入職日期:<input type="text" name="hireDate"  ><br>

              <input type="submit" value="提交"  ><br>

       </form>

</body>


Action代碼


//使用Action獲得表單提交參數

//1.在Action中準備與參數鍵相同的屬性,並提供相應的set方法,參數值會自動封裝到屬性中.

//2.封裝參數還會幫我們自動類型轉換. (8大基礎資料型別 (Elementary Data Type)|指定格式的Date類型)

public class Demo7Action extends ActionSupport {

       private static final long serialVersionUID = 1L;

       private String name;

       private Integer age;

       private Date hireDate;

       @Override

       public String execute() throws Exception {

              System.out.println("name:"+name+",age:"+age+",hireDate:"+hireDate);    

              return SUCCESS;

       }

       public void setName(String name) {

              this.name = name;

       }

       public void setAge(Integer age) {

              this.age = age;

       }

       public void setHireDate(Date hireDate) {

              this.hireDate = hireDate;

       } }

二.對象驅動

Jsp代碼

<body>

       <h1>對象驅動</h1>

       <form action="${pageContext.request.contextPath}/Demo8Action" method="post">

              使用者名稱:<input type="text" name="user.name"  ><br>

              年齡:<input type="text" name="user.age"  ><br>

              入職日期:<input type="text" name="user.hireDate"  ><br>

              <input type="submit" value="提交"  ><br>

       </form>

</body>

  Action代碼

//使用Action獲得表單提交參數

//1 在action中準備對象屬性.並提供對象的set/get方法

//2 表單提交的鍵格式:對象屬性名稱.屬性名稱

public class Demo8Action extends ActionSupport {

       private static final long serialVersionUID = 1L;

       private User user;

       @Override

       public String execute() throws Exception {

              System.out.println(user);   

              return SUCCESS;

       }

       public void setUser(User user) {

              this.user = user;

       }

public User getUser() {

              return user;

       }  

}


三.模型驅動
Jsp代碼

<body>

       <h1>模型驅動</h1>

       <form action="${pageContext.request.contextPath}/Demo9Action" method="post">

              使用者名稱:<input type="text" name="name"  ><br>

              年齡:<input type="text" name="age"  ><br>

              入職日期:<input type="text" name="hireDate"  ><br>

              <input type="submit" value="提交"  ><br>

       </form>

</body> Action代碼

//使用Action獲得表單提交參數

//1 action實現ModelDriven介面.在實現方法中返回封裝參數的User對象

//2 表單提交參數時,直接寫對象中屬性名稱即可.

public class Demo9Action extends ActionSupport implements ModelDriven<User> {

       private static final long serialVersionUID = 1L;

       private User user = new User();

      

       public User getModel() {

              return user;

       }

       @Override

       public String execute() throws Exception {

             

              System.out.println(user);

             

              return SUCCESS;

       } }

 複雜類型接收

Jsp代碼

<body>

       <h1>複雜類型接收</h1>

       <form action="${pageContext.request.contextPath}/Demo10Action" method="post">

              數組:<input type="text" name="arr"  ><br>

              數組:<input type="text" name="arr"  ><br>

              數組:<input type="text" name="arr"  ><br>

              List:<input type="text" name="list"  ><br>

              List:<input type="text" name="list"  ><br>

              List:<input type="text" name="list"  ><br>

              List:<input type="text" name="list[6]"  ><br>

              Map:<input type="text" name="map['name']"  ><br>

              Map:<input type="text" name="map['age']"  ><br>

              Map:<input type="text" name="map['hehe']"  ><br>

              <input type="submit" value="提交"  ><br>

       </form>

</body>

Action代碼

//示範使用Action獲得表單提交參數 => 複雜類型接收

public class Demo10Action extends ActionSupport  {

 

       private static final long serialVersionUID = 1L;

       private String[] arr ; 

       private List list; 

       private Map map;

       @Override

       public String execute() throws Exception {

             

              System.out.println(Arrays.toString(arr));

              System.out.println(list);

              System.out.println(map);

             

              return SUCCESS;

       }


       public void setArr(String[] arr) {

              this.arr = arr;

       }

      public void setList(List list) {

              this.list = list;

       }

       public void setMap(Map map) {

              this.map = map;

       }  

}

 



聯繫我們

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