strut2接收参数的三种方式(属性驱动\对象驱动\模型驱动)说明

strut2接收参数有三种方式(属性驱动\对象驱动\模型驱动),分别对三种进行一个总结:

一.属性驱动

Jsp代码

<body>

      

属性驱动

      

              用户名:

              年龄:

              入职日期:

              提交"  >

      

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>

      

对象驱动

      

              用户名:

              年龄:

              入职日期:

              提交"  >

      

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>

      

模型驱动

      

              用户名:

              年龄:

              入职日期:

              提交"  >

      

body>

Action代码

//使用Action获得表单提交参数

//1 action实现ModelDriven接口.在实现方法中返回封装参数的User对象

//2 表单提交参数时,直接写对象中属性名即可.

public class Demo9Action extends ActionSupport implements ModelDriven {

       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>

      

复杂类型接收

      

              数组:

              数组:

              数组:

              List:

              List:

              List:

              List:

              Map:

              Map:

              Map:

              提交"  >

      

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;

       }  

}

 



你可能感兴趣的:(三大框架)