例如,我需要从表单中保存几名员工:
<h4>Employee 1</h4>
<div>
<input type="text" name="names">
<input type="text" name="ages">
<input type="text" name="salaries">
</div>
<h4>Employee 2</h4>
<div>
<input type="text" name="names">
<input type="text" name="ages">
<input type="text" name="salaries">
</div>
<h4>Employee 3</h4>
<div>
<input type="text" name="names">
<input type="text" name="ages">
<input type="text" name="salaries">
</div>
为此我这样做:
public EmployeeAction extends ActionSupport {
private String[] employeesNames;
private Integer[] employeesAges;
private Integer[] employeesSalaries;
// Setters and getters
public String save() {
Employee[] employees = new Employee[size];
// Creating employee objects with names, ages and salaries
// and then save
saveBatch(employees);
return SUCCESS;
}
}
有没有办法可以在不处理数组的情况下从表单保存数据, 但仅仅为了节省一大堆员工? 例如:
<h4>Employee 1</h4>
<div>
<input type="text" name="employees.name">
<input type="text" name="employees.age">
<input type="text" name="employees.salary">
</div>
<h4>Employee 2</h4>
<div>
<input type="text" name="employees.name">
<input type="text" name="employees.age">
<input type="text" name="employees.salary">
</div>
<h4>Employee 3</h4>
<div>
<input type="text" name="employees.name">
<input type="text" name="employees.age">
<input type="text" name="employees.salary">
</div>
和行动类:
public EmployeeAction extends ActionSupport {
private Employee[] employees;
// Setters and getters
public String save() {
saveBatch(employees);
return SUCCESS;
}
}