首页>代码>S2SH(spring+struts2+hibernate) 开发java学生信息管理网站源码下载>/JavaWebProject/src/com/student2where/action/ArmyStudentAction.java
package com.student2where.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.student2where.utils.ExportExcelUtil;
import com.student2where.dao.ArmyStudentDAO;
import com.student2where.domain.ArmyStudent;
import com.student2where.dao.ProfessionInfoDAO;
import com.student2where.domain.ProfessionInfo;
import com.student2where.dao.YearInfoDAO;
import com.student2where.domain.YearInfo;
import com.student2where.dao.ProvinceInfoDAO;
import com.student2where.domain.ProvinceInfo;
@Controller @Scope("prototype")
public class ArmyStudentAction extends ActionSupport {

    /*界面层需要查询的属性: 专业*/
    private ProfessionInfo professionObj;
    public void setProfessionObj(ProfessionInfo professionObj) {
        this.professionObj = professionObj;
    }
    public ProfessionInfo getProfessionObj() {
        return this.professionObj;
    }

    /*界面层需要查询的属性: 年级*/
    private String nianji;
    public void setNianji(String nianji) {
        this.nianji = nianji;
    }
    public String getNianji() {
        return this.nianji;
    }

    /*界面层需要查询的属性: 年份*/
    private YearInfo yearObj;
    public void setYearObj(YearInfo yearObj) {
        this.yearObj = yearObj;
    }
    public YearInfo getYearObj() {
        return this.yearObj;
    }

    /*界面层需要查询的属性: 省份*/
    private ProvinceInfo provinceObj;
    public void setProvinceObj(ProvinceInfo provinceObj) {
        this.provinceObj = provinceObj;
    }
    public ProvinceInfo getProvinceObj() {
        return this.provinceObj;
    }

    /*当前第几页*/
    private int currentPage;
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getCurrentPage() {
        return currentPage;
    }

    /*一共多少页*/
    private int totalPage;
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getTotalPage() {
        return totalPage;
    }

    private int studentId;
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }
    public int getStudentId() {
        return studentId;
    }

    /*当前查询的总记录数目*/
    private int recordNumber;
    public void setRecordNumber(int recordNumber) {
        this.recordNumber = recordNumber;
    }
    public int getRecordNumber() {
        return recordNumber;
    }

    /*业务层对象*/
    @Resource ArmyStudentDAO armyStudentDAO;

    @Resource ProfessionInfoDAO professionInfoDAO;
    @Resource YearInfoDAO yearInfoDAO;
    @Resource ProvinceInfoDAO provinceInfoDAO;
    /*待操作的ArmyStudent对象*/
    private ArmyStudent armyStudent;
    public void setArmyStudent(ArmyStudent armyStudent) {
        this.armyStudent = armyStudent;
    }
    public ArmyStudent getArmyStudent() {
        return this.armyStudent;
    }

    /*跳转到添加ArmyStudent视图*/
    public String AddView() {
        ActionContext ctx = ActionContext.getContext();
        /*查询所有的ProfessionInfo信息*/
        List<ProfessionInfo> professionInfoList = professionInfoDAO.QueryAllProfessionInfoInfo();
        ctx.put("professionInfoList", professionInfoList);
        /*查询所有的YearInfo信息*/
        List<YearInfo> yearInfoList = yearInfoDAO.QueryAllYearInfoInfo();
        ctx.put("yearInfoList", yearInfoList);
        /*查询所有的ProvinceInfo信息*/
        List<ProvinceInfo> provinceInfoList = provinceInfoDAO.QueryAllProvinceInfoInfo();
        ctx.put("provinceInfoList", provinceInfoList);
        return "add_view";
    }

    /*添加ArmyStudent信息*/
    @SuppressWarnings("deprecation")
    public String AddArmyStudent() {
        ActionContext ctx = ActionContext.getContext();
        try {
            if(true) {
            ProfessionInfo professionObj = professionInfoDAO.GetProfessionInfoByProfessionId(armyStudent.getProfessionObj().getProfessionId());
            armyStudent.setProfessionObj(professionObj);
            }
            if(true) {
            YearInfo yearObj = yearInfoDAO.GetYearInfoByYearId(armyStudent.getYearObj().getYearId());
            armyStudent.setYearObj(yearObj);
            }
            if(true) {
            ProvinceInfo provinceObj = provinceInfoDAO.GetProvinceInfoByProvinceId(armyStudent.getProvinceObj().getProvinceId());
            armyStudent.setProvinceObj(provinceObj);
            }
            armyStudentDAO.AddArmyStudent(armyStudent);
            ctx.put("message",  java.net.URLEncoder.encode("ArmyStudent添加成功!"));
            return "add_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("ArmyStudent添加失败!"));
            return "error";
        }
    }

    /*查询ArmyStudent信息*/
    public String QueryArmyStudent() {
        if(currentPage == 0) currentPage = 1;
        if(nianji == null) nianji = "";
        List<ArmyStudent> armyStudentList = armyStudentDAO.QueryArmyStudentInfo(professionObj, nianji, yearObj, provinceObj, currentPage);
        /*计算总的页数和总的记录数*/
        armyStudentDAO.CalculateTotalPageAndRecordNumber(professionObj, nianji, yearObj, provinceObj);
        /*获取到总的页码数目*/
        totalPage = armyStudentDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = armyStudentDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("armyStudentList",  armyStudentList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("professionObj", professionObj);
        List<ProfessionInfo> professionInfoList = professionInfoDAO.QueryAllProfessionInfoInfo();
        ctx.put("professionInfoList", professionInfoList);
        ctx.put("nianji", nianji);
        ctx.put("yearObj", yearObj);
        List<YearInfo> yearInfoList = yearInfoDAO.QueryAllYearInfoInfo();
        ctx.put("yearInfoList", yearInfoList);
        ctx.put("provinceObj", provinceObj);
        List<ProvinceInfo> provinceInfoList = provinceInfoDAO.QueryAllProvinceInfoInfo();
        ctx.put("provinceInfoList", provinceInfoList);
        return "query_view";
    }

    /*后台导出到excel*/
    public String QueryArmyStudentOutputToExcel() { 
        if(nianji == null) nianji = "";
        List<ArmyStudent> armyStudentList = armyStudentDAO.QueryArmyStudentInfo(professionObj,nianji,yearObj,provinceObj);
        ExportExcelUtil ex = new ExportExcelUtil();
        String title = "ArmyStudent信息记录"; 
        String[] headers = { "姓名","性别","专业","年级","年份","省份"};
        List<String[]> dataset = new ArrayList<String[]>(); 
        for(int i=0;i<armyStudentList.size();i++) {
        	ArmyStudent armyStudent = armyStudentList.get(i); 
        	dataset.add(new String[]{armyStudent.getStudentName(),armyStudent.getSex(),armyStudent.getProfessionObj().getProfessionName(),
armyStudent.getNianji(),armyStudent.getYearObj().getYearName(),
armyStudent.getProvinceObj().getProvinceName()
});
        }
        /*
        OutputStream out = null;
		try {
			out = new FileOutputStream("C://output.xls");
			ex.exportExcel(title,headers, dataset, out);
		    out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
		HttpServletResponse response = null;//创建一个HttpServletResponse对象 
		OutputStream out = null;//创建一个输出流对象 
		try { 
			response = ServletActionContext.getResponse();//初始化HttpServletResponse对象 
			out = response.getOutputStream();//
			response.setHeader("Content-disposition","attachment; filename="+"ArmyStudent.xls");//filename是下载的xls的名,建议最好用英文 
			response.setContentType("application/msexcel;charset=UTF-8");//设置类型 
			response.setHeader("Pragma","No-cache");//设置头 
			response.setHeader("Cache-Control","no-cache");//设置头 
			response.setDateHeader("Expires", 0);//设置日期头  
			String rootPath = ServletActionContext.getServletContext().getRealPath("/");
			ex.exportExcel(rootPath,title,headers, dataset, out);
			out.flush();
		} catch (IOException e) { 
			e.printStackTrace(); 
		}finally{
			try{
				if(out!=null){ 
					out.close(); 
				}
			}catch(IOException e){ 
				e.printStackTrace(); 
			} 
		}
		return null;
    }
    /*前台查询ArmyStudent信息*/
    public String FrontQueryArmyStudent() {
        if(currentPage == 0) currentPage = 1;
        if(nianji == null) nianji = "";
        List<ArmyStudent> armyStudentList = armyStudentDAO.QueryArmyStudentInfo(professionObj, nianji, yearObj, provinceObj, currentPage);
        /*计算总的页数和总的记录数*/
        armyStudentDAO.CalculateTotalPageAndRecordNumber(professionObj, nianji, yearObj, provinceObj);
        /*获取到总的页码数目*/
        totalPage = armyStudentDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = armyStudentDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("armyStudentList",  armyStudentList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("professionObj", professionObj);
        List<ProfessionInfo> professionInfoList = professionInfoDAO.QueryAllProfessionInfoInfo();
        ctx.put("professionInfoList", professionInfoList);
        ctx.put("nianji", nianji);
        ctx.put("yearObj", yearObj);
        List<YearInfo> yearInfoList = yearInfoDAO.QueryAllYearInfoInfo();
        ctx.put("yearInfoList", yearInfoList);
        ctx.put("provinceObj", provinceObj);
        List<ProvinceInfo> provinceInfoList = provinceInfoDAO.QueryAllProvinceInfoInfo();
        ctx.put("provinceInfoList", provinceInfoList);
        return "front_query_view";
    }

    /*查询要修改的ArmyStudent信息*/
    public String ModifyArmyStudentQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键studentId获取ArmyStudent对象*/
        ArmyStudent armyStudent = armyStudentDAO.GetArmyStudentByStudentId(studentId);

        List<ProfessionInfo> professionInfoList = professionInfoDAO.QueryAllProfessionInfoInfo();
        ctx.put("professionInfoList", professionInfoList);
        List<YearInfo> yearInfoList = yearInfoDAO.QueryAllYearInfoInfo();
        ctx.put("yearInfoList", yearInfoList);
        List<ProvinceInfo> provinceInfoList = provinceInfoDAO.QueryAllProvinceInfoInfo();
        ctx.put("provinceInfoList", provinceInfoList);
        ctx.put("armyStudent",  armyStudent);
        return "modify_view";
    }

    /*查询要修改的ArmyStudent信息*/
    public String FrontShowArmyStudentQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键studentId获取ArmyStudent对象*/
        ArmyStudent armyStudent = armyStudentDAO.GetArmyStudentByStudentId(studentId);

        List<ProfessionInfo> professionInfoList = professionInfoDAO.QueryAllProfessionInfoInfo();
        ctx.put("professionInfoList", professionInfoList);
        List<YearInfo> yearInfoList = yearInfoDAO.QueryAllYearInfoInfo();
        ctx.put("yearInfoList", yearInfoList);
        List<ProvinceInfo> provinceInfoList = provinceInfoDAO.QueryAllProvinceInfoInfo();
        ctx.put("provinceInfoList", provinceInfoList);
        ctx.put("armyStudent",  armyStudent);
        return "front_show_view";
    }

    /*更新修改ArmyStudent信息*/
    public String ModifyArmyStudent() {
        ActionContext ctx = ActionContext.getContext();
        try {
            if(true) {
            ProfessionInfo professionObj = professionInfoDAO.GetProfessionInfoByProfessionId(armyStudent.getProfessionObj().getProfessionId());
            armyStudent.setProfessionObj(professionObj);
            }
            if(true) {
            YearInfo yearObj = yearInfoDAO.GetYearInfoByYearId(armyStudent.getYearObj().getYearId());
            armyStudent.setYearObj(yearObj);
            }
            if(true) {
            ProvinceInfo provinceObj = provinceInfoDAO.GetProvinceInfoByProvinceId(armyStudent.getProvinceObj().getProvinceId());
            armyStudent.setProvinceObj(provinceObj);
            }
            armyStudentDAO.UpdateArmyStudent(armyStudent);
            ctx.put("message",  java.net.URLEncoder.encode("ArmyStudent信息更新成功!"));
            return "modify_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("ArmyStudent信息更新失败!"));
            return "error";
       }
   }

    /*删除ArmyStudent信息*/
    public String DeleteArmyStudent() {
        ActionContext ctx = ActionContext.getContext();
        try { 
            armyStudentDAO.DeleteArmyStudent(studentId);
            ctx.put("message",  java.net.URLEncoder.encode("ArmyStudent删除成功!"));
            return "delete_success";
        } catch (Exception e) { 
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("ArmyStudent删除失败!"));
            return "error";
        }
    }

}
最近下载更多
wanglinddad  LV55 2022年3月5日
tx1121  LV14 2021年5月6日
and123456  LV11 2021年4月20日
liangge2115  LV27 2020年11月27日
张青峰  LV10 2020年9月9日
Alan Turing  LV14 2020年7月9日
862960632  LV14 2020年7月6日
17797226326  LV3 2020年5月16日
pt11100  LV9 2020年5月7日
诗若灯清  LV8 2019年12月25日
最近浏览更多
justoday  LV11 9月11日
WBelong  LV8 9月9日
磊哥哥哥哥  LV13 2023年12月26日
haotzy  LV3 2023年10月5日
卢本伟不开挂  LV4 2023年9月3日
uni-code_0123  LV1 2023年8月8日
zhizhiz 2023年7月2日
暂无贡献等级
li951753  LV2 2023年6月19日
王华伟  LV19 2023年4月27日
喝喝XYZo  LV2 2023年4月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友