在Action 中读取页面传递来的数据

来源:互联网 发布:网络知识培训手机 编辑:程序博客网 时间:2024/04/30 13:55

    在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;    }

0 0