在上一篇中,例子是从前台传递参数到Action,有的时候,可能需要直接封装成对象,传递好几个对象到后台。
这时候,只能用List接收了,不能使用对象数组。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>1.使用数组接收多选框的值</h3>
<form action="user/common!register" method="post">
爱好:<br/>
<input type="checkbox" name="habits" value="sports">运动<br/>
<input type="checkbox" name="habits" value="reading">读书<br/>
<input type="checkbox" name="habits" value="sleep">睡觉<br/>
<input type="submit" value="Register">
</form>
<h3>2.使用List接收多选框的值</h3>
<form action="user/common!register2" method="post">
爱好:<br/>
<input type="checkbox" name="habitList" value="sports">运动<br/>
<input type="checkbox" name="habitList" value="reading">读书<br/>
<input type="checkbox" name="habitList" value="sleep">睡觉<br/>
<input type="submit" value="Register">
</form>
<h3>4.使用CheckBox传递对象,使用List接收</h3>
<form action="user/common!register4" method="post">
name:<input type="text" name="userList[0].name">
password:<input type="password" name="userList[0].password">
<input type="submit" value="Register">
</form>
</body>
</html>
package com.deppon.demo.struts;
import java.util.List;
import com.deppon.demo.entity.UserEntity;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements ModelDriven<UserEntity> {
private static final long serialVersionUID = 7146977447328113550L;
private String name;
private String password;
private UserEntity user;
private UserEntity userModel = new UserEntity();
private String[] habits;
private List<String> habitList;
private UserEntity[] users;
private List<UserEntity> userList;
public List<UserEntity> getUserList() {
return userList;
}
public void setUserList(List<UserEntity> userList) {
this.userList = userList;
}
public UserEntity[] getUsers() {
return users;
}
public void setUsers(UserEntity[] users) {
this.users = users;
}
public List<String> getHabitList() {
return habitList;
}
public void setHabitList(List<String> habitList) {
this.habitList = habitList;
}
public String[] getHabits() {
return habits;
}
public void setHabits(String[] habits) {
this.habits = habits;
}
public UserEntity getModel() {
return userModel;
}
public UserEntity getUser() {
return user;
}
public void setUser(UserEntity user) {
this.user = user;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
* 使用属性的方式,直接接收参数 当参数较多时,不方便
*
* @return
*/
public String login() {
System.out.println("name->" + name + ",password->" + password);
return SUCCESS;
}
/**
* 使用对象接收 需要加前缀,和变量名一致
*
* @return
*/
public String login2() {
System.out.println("user->" + user);
return SUCCESS;
}
/**
* 是想ModelDriven接口
*
* @return
*/
public String login3() {
System.out.println("userModel->" + userModel);
return SUCCESS;
}
/**
* 使用数据接收多选框的值
* @return
*/
public String register() {
//如果一个都没有选的话,habits会是null
if(habits != null) {
for(String each : habits) {
System.out.println("habits-->" + each);
}
}
return "checkbox";
}
/**
* 使用List接收多选框的值
* @return
*/
public String register2() {
System.out.println("habitList->" + habitList);
return "checkbox";
}
/**
* 这个是错误的,接收不到
* @return
*/
public String register3() {
System.out.println("users->" + users);
return "checkbox";
}
/**
* 使用List接收对象数组
* @return
*/
public String register4() {
System.out.println("userList->" + userList);
return "checkbox";
}
}