首页>代码>S2SH(spring+struts2+hibernate) 开发java学生信息管理网站源码下载>/JavaWebProject/src/com/student2where/action/CollegeInfoAction.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.CollegeInfoDAO;
import com.student2where.domain.CollegeInfo;
import com.student2where.dao.ProvinceInfoDAO;
import com.student2where.domain.ProvinceInfo;
@Controller @Scope("prototype")
public class CollegeInfoAction extends ActionSupport {

    /*界面层需要查询的属性: 所在省份*/
    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 collegeId;
    public void setCollegeId(int collegeId) {
        this.collegeId = collegeId;
    }
    public int getCollegeId() {
        return collegeId;
    }

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

    /*业务层对象*/
    @Resource CollegeInfoDAO collegeInfoDAO;

    @Resource ProvinceInfoDAO provinceInfoDAO;
    /*待操作的CollegeInfo对象*/
    private CollegeInfo collegeInfo;
    public void setCollegeInfo(CollegeInfo collegeInfo) {
        this.collegeInfo = collegeInfo;
    }
    public CollegeInfo getCollegeInfo() {
        return this.collegeInfo;
    }

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

    /*添加CollegeInfo信息*/
    @SuppressWarnings("deprecation")
    public String AddCollegeInfo() {
        ActionContext ctx = ActionContext.getContext();
        try {
            if(true) {
            ProvinceInfo provinceObj = provinceInfoDAO.GetProvinceInfoByProvinceId(collegeInfo.getProvinceObj().getProvinceId());
            collegeInfo.setProvinceObj(provinceObj);
            }
            collegeInfoDAO.AddCollegeInfo(collegeInfo);
            ctx.put("message",  java.net.URLEncoder.encode("CollegeInfo添加成功!"));
            return "add_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("CollegeInfo添加失败!"));
            return "error";
        }
    }

    /*查询CollegeInfo信息*/
    public String QueryCollegeInfo() {
        if(currentPage == 0) currentPage = 1;
        List<CollegeInfo> collegeInfoList = collegeInfoDAO.QueryCollegeInfoInfo(provinceObj, currentPage);
        /*计算总的页数和总的记录数*/
        collegeInfoDAO.CalculateTotalPageAndRecordNumber(provinceObj);
        /*获取到总的页码数目*/
        totalPage = collegeInfoDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = collegeInfoDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("collegeInfoList",  collegeInfoList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("provinceObj", provinceObj);
        List<ProvinceInfo> provinceInfoList = provinceInfoDAO.QueryAllProvinceInfoInfo();
        ctx.put("provinceInfoList", provinceInfoList);
        return "query_view";
    }

    /*后台导出到excel*/
    public String QueryCollegeInfoOutputToExcel() { 
        List<CollegeInfo> collegeInfoList = collegeInfoDAO.QueryCollegeInfoInfo(provinceObj);
        ExportExcelUtil ex = new ExportExcelUtil();
        String title = "CollegeInfo信息记录"; 
        String[] headers = { "院校编号","所在省份","院校名称"};
        List<String[]> dataset = new ArrayList<String[]>(); 
        for(int i=0;i<collegeInfoList.size();i++) {
        	CollegeInfo collegeInfo = collegeInfoList.get(i); 
        	dataset.add(new String[]{collegeInfo.getCollegeId() + "",collegeInfo.getProvinceObj().getProvinceName(),
collegeInfo.getCollegeName()});
        }
        /*
        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="+"CollegeInfo.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;
    }
    /*前台查询CollegeInfo信息*/
    public String FrontQueryCollegeInfo() {
        if(currentPage == 0) currentPage = 1;
        List<CollegeInfo> collegeInfoList = collegeInfoDAO.QueryCollegeInfoInfo(provinceObj, currentPage);
        /*计算总的页数和总的记录数*/
        collegeInfoDAO.CalculateTotalPageAndRecordNumber(provinceObj);
        /*获取到总的页码数目*/
        totalPage = collegeInfoDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = collegeInfoDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("collegeInfoList",  collegeInfoList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("provinceObj", provinceObj);
        List<ProvinceInfo> provinceInfoList = provinceInfoDAO.QueryAllProvinceInfoInfo();
        ctx.put("provinceInfoList", provinceInfoList);
        return "front_query_view";
    }

    /*查询要修改的CollegeInfo信息*/
    public String ModifyCollegeInfoQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键collegeId获取CollegeInfo对象*/
        CollegeInfo collegeInfo = collegeInfoDAO.GetCollegeInfoByCollegeId(collegeId);

        List<ProvinceInfo> provinceInfoList = provinceInfoDAO.QueryAllProvinceInfoInfo();
        ctx.put("provinceInfoList", provinceInfoList);
        ctx.put("collegeInfo",  collegeInfo);
        return "modify_view";
    }

    /*查询要修改的CollegeInfo信息*/
    public String FrontShowCollegeInfoQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键collegeId获取CollegeInfo对象*/
        CollegeInfo collegeInfo = collegeInfoDAO.GetCollegeInfoByCollegeId(collegeId);

        List<ProvinceInfo> provinceInfoList = provinceInfoDAO.QueryAllProvinceInfoInfo();
        ctx.put("provinceInfoList", provinceInfoList);
        ctx.put("collegeInfo",  collegeInfo);
        return "front_show_view";
    }

    /*更新修改CollegeInfo信息*/
    public String ModifyCollegeInfo() {
        ActionContext ctx = ActionContext.getContext();
        try {
            if(true) {
            ProvinceInfo provinceObj = provinceInfoDAO.GetProvinceInfoByProvinceId(collegeInfo.getProvinceObj().getProvinceId());
            collegeInfo.setProvinceObj(provinceObj);
            }
            collegeInfoDAO.UpdateCollegeInfo(collegeInfo);
            ctx.put("message",  java.net.URLEncoder.encode("CollegeInfo信息更新成功!"));
            return "modify_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("CollegeInfo信息更新失败!"));
            return "error";
       }
   }

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

}
最近下载更多
wanglinddad  LV54 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日
最近浏览更多
磊哥哥哥哥  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日
彩色天空  LV5 2023年4月11日
Tayirjan  LV11 2022年12月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友