stuaction.java

来自「Hibernate开发及整合应用大全 蔡雪焘编著 本书用典型的示例剖析Hiber」· Java 代码 · 共 86 行

JAVA
86
字号
package action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Student;
import model.StudentForm;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


/**
 * @author Administrator
 * 
 * TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class StuAction extends BaseAction {
     //添加学生记录
    public ActionForward insert(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        StudentForm stu_vo=(StudentForm)form;
        Student stu_po=new Student();
        BeanUtils.copyProperties(stu_po, stu_vo);
        getService().getStuDAO().createObj(stu_po);
        return list(mapping, form, request, response);

    }



    //删除学生记录
    public ActionForward del(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        StudentForm stu_vo = (StudentForm) form;
        Student stu_po=getService().getStuDAO().findById(stu_vo.getId());
        getService().getStuDAO().delObject(stu_po);
        return list(mapping, form, request, response);
    }

    //取得要修改的学生资料,并把页面导向detail.sjp
    public ActionForward getMdfInfo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        StudentForm stu_vo = (StudentForm) form;
        Student stu_po =  getService().getStuDAO().findById(stu_vo.getId());
        BeanUtils.copyProperties(stu_vo, stu_po);
        request.setAttribute("stuForm", stu_vo);
        return mapping.findForward("detail");

    }
    //修改学生记录
    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        StudentForm stu_vo = (StudentForm) form;
        Student stu_po = new Student();
        BeanUtils.copyProperties(stu_po, stu_vo);
        getService().getStuDAO().mdfObj(stu_po);
        return list(mapping, form, request, response);
    }
    //取得学生列表,并发送到stu_list.jsp
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        List temp =  getService().getStuDAO().getAllStu();
        List list = new ArrayList(temp.size());
        for (int i = 0; i < temp.size(); i++) {
            Student stu_po = (Student) temp.get(i);
            StudentForm stu_vo = new StudentForm();
            BeanUtils.copyProperties(stu_vo, stu_po);
            list.add(stu_vo);
        }
        request.setAttribute("list", list);
        return mapping.findForward("stu_list");
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?