在action獲取頁面數據有兩種方式
1 屬性驅動
爲屬性設置get 和set方法
2 模型驅動
模型驅動中的action 必須實現ModelDriven
在模型驅動的通過getModel方法將 將數據接收對象放置到棧頂進行接收數據
age com.wuyihuai.oa.struts2.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wuyihuai.ao.struts2.action.base.BaseAction;
import com.wuyihuai.oa.domain.Department;
import com.wuyihuai.oa.service.DepartmentService;
public class DepartmentAction extends BaseAction implements ModelDriven<Department>{
private DepartmentService departmentService;
private Department model=new Department();
//ModelDriven攔截器將model對象放置到棧頂接收數據
@Override
public Department getModel() {
// TODO Auto-generated method stub
return this.model;
}
public DepartmentService getDepartmentService() {
return departmentService;
}
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
public String add(){
/**
* 增加邏輯
* 1. 獲取頁面數據
* 屬性驅動
* 屬性和set 。get 方法
*
* 模型驅動
* 這個action必須實現ModelDriven
* 建立一個私有的模型對象
* 在這個action中有一個getModel方法
* 2. 把數據封裝到javabean中
*/
//重新定義department 而不直接是由this.getModel進行保存是爲了 避免直接操作session而出錯
Department department=new Department();
department.setDname(this.getModel().getDname());
department.setDescription(this.getModel().getDescription());
this.departmentService.saveDepartment(department);
return null;
}