strut2接收参数的三种方式

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;

}

}

上一篇:ssh框架总结之action接收参数的三种方式


下一篇:springmvc Controller接收前端参数的几种方式总结