stuaction.java

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

JAVA
83
字号
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;
import org.apache.struts.actions.DispatchAction;

import persistence.StudentDAO;

public class StuAction extends DispatchAction {
    //添加学生记录
    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);
        StudentDAO.createObj(stu_po);
        List list = StudentDAO.getAllStu();
        request.setAttribute("list", list);
        return list(mapping, form, request, response);

    }



    //删除学生记录
    public ActionForward del(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        StudentForm stuForm = (StudentForm) form;
        StudentDAO.delObject(stuForm.getId());
        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 = StudentDAO.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);
        StudentDAO.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 list = StudentDAO.getAllStu();
        List temp = new ArrayList(list.size());
        for (int i = 0; i < list.size(); i++) {
            Student stu_po = (Student) list.get(i);
            StudentForm stu_vo = new StudentForm();
            BeanUtils.copyProperties(stu_vo, stu_po);
            temp.add(stu_vo);
        }
        request.setAttribute("list", temp);
        return mapping.findForward("stu_list");
    }

}

⌨️ 快捷键说明

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