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大基本資料型別|指定格式的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;

       }

}