首页>代码>基于s2sh框架和easyui前台的停车信息后台管理系统>/项目源码/src/com/chengxusheji/action/CarTypeAction.java
package com.chengxusheji.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.io.PrintWriter;
import java.util.ArrayList;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
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.chengxusheji.utils.ExportExcelUtil;
import com.chengxusheji.dao.CarTypeDAO;
import com.chengxusheji.domain.CarType;
@Controller @Scope("prototype")
public class CarTypeAction extends ActionSupport {

    /*当前第几页*/
    private int page;
    public void setPage(int page) {
        this.page = page;
    }
    public int getPage() {
        return page;
    }

    /*每页显示多少条数据*/
    private int rows;
    public void setRows(int rows) {
    	this.rows = rows;
    }
    public int getRows() {
    	return this.rows;
    }
    /*一共多少页*/
    private int totalPage;
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getTotalPage() {
        return totalPage;
    }

    private int carTypeId;
    public void setCarTypeId(int carTypeId) {
        this.carTypeId = carTypeId;
    }
    public int getCarTypeId() {
        return carTypeId;
    }

    /*要删除的记录主键集合*/
    private String carTypeIds;
    public String getCarTypeIds() {
		return carTypeIds;
	}
	public void setCarTypeIds(String carTypeIds) {
		this.carTypeIds = carTypeIds;
	}
    /*当前查询的总记录数目*/
    private int recordNumber;
    public void setRecordNumber(int recordNumber) {
        this.recordNumber = recordNumber;
    }
    public int getRecordNumber() {
        return recordNumber;
    }

    /*业务层对象*/
    @Resource CarTypeDAO carTypeDAO;

    /*待操作的CarType对象*/
    private CarType carType;
    public void setCarType(CarType carType) {
        this.carType = carType;
    }
    public CarType getCarType() {
        return this.carType;
    }

    /*ajax添加CarType信息*/
    @SuppressWarnings("deprecation")
    public void ajaxAddCarType() throws IOException, JSONException {
    	String message = "";
    	boolean success = false;
        try {
            carTypeDAO.AddCarType(carType);
            success = true;
            writeJsonResponse(success, message); 
        } catch (Exception e) {
            e.printStackTrace();
            message = "CarType添加失败!";
            writeJsonResponse(success, message);
        }
    }

    /*向客户端输出操作成功或失败信息*/
	private void writeJsonResponse(boolean success,String message)
			throws IOException, JSONException {
		HttpServletResponse response=ServletActionContext.getResponse(); 
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter(); 
		//将要被返回到客户端的对象 
		JSONObject json=new JSONObject();
		json.accumulate("success", success);
		json.accumulate("message", message);
		out.println(json.toString());
		out.flush(); 
		out.close();
	}

    /*查询CarType信息*/
    public void ajaxQueryCarType() throws IOException, JSONException {
        if(page == 0) page = 1;
        if(rows != 0) carTypeDAO.setRows(rows);
        List<CarType> carTypeList = carTypeDAO.QueryCarTypeInfo(page);
        /*计算总的页数和总的记录数*/
        carTypeDAO.CalculateTotalPageAndRecordNumber();
        /*获取到总的页码数目*/
        totalPage = carTypeDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = carTypeDAO.getRecordNumber();
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setContentType("text/json;charset=UTF-8"); 
		PrintWriter out = response.getWriter();
		//将要被返回到客户端的对象
		JSONObject jsonObj=new JSONObject();
		jsonObj.accumulate("total", recordNumber);
		JSONArray jsonArray = new JSONArray();
		for(CarType carType:carTypeList) {
			JSONObject jsonCarType = carType.getJsonObject();
			jsonArray.put(jsonCarType);
		}
		jsonObj.accumulate("rows", jsonArray);
		out.println(jsonObj.toString());
		out.flush();
		out.close();
    }

    /*查询CarType信息*/
    public void ajaxQueryAllCarType() throws IOException, JSONException {
        List<CarType> carTypeList = carTypeDAO.QueryAllCarTypeInfo();        HttpServletResponse response=ServletActionContext.getResponse();
        response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		//将要被返回到客户端的对象
		JSONArray jsonArray = new JSONArray();
		for(CarType carType:carTypeList) {
			JSONObject jsonCarType = new JSONObject();
			jsonCarType.accumulate("carTypeId", carType.getCarTypeId());
			jsonCarType.accumulate("carTypeName", carType.getCarTypeName());
			jsonArray.put(jsonCarType);
		}
		out.println(jsonArray.toString());
		out.flush();
		out.close();
    }

    /*查询要修改的CarType信息*/
    public void ajaxModifyCarTypeQuery() throws IOException, JSONException {
        /*根据主键carTypeId获取CarType对象*/
        CarType carType = carTypeDAO.GetCarTypeByCarTypeId(carTypeId);

        HttpServletResponse response=ServletActionContext.getResponse(); 
        response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter(); 
		//将要被返回到客户端的对象 
		JSONObject jsonCarType = carType.getJsonObject(); 
		out.println(jsonCarType.toString()); 
		out.flush();
		out.close();
    };

    /*更新修改CarType信息*/
    public void ajaxModifyCarType() throws IOException, JSONException{
    	String message = "";
    	boolean success = false;
        try {
            carTypeDAO.UpdateCarType(carType);
            success = true;
            writeJsonResponse(success, message);
        } catch (Exception e) {
            message = "CarType修改失败!"; 
            writeJsonResponse(success, message);
       }
   }

    /*删除CarType信息*/
    public void ajaxDeleteCarType() throws IOException, JSONException {
    	String message = "";
    	boolean success = false;
        try { 
        	String _carTypeIds[] = carTypeIds.split(",");
        	for(String _carTypeId: _carTypeIds) {
        		carTypeDAO.DeleteCarType(Integer.parseInt(_carTypeId));
        	}
        	success = true;
        	message = _carTypeIds.length + "条记录删除成功";
        	writeJsonResponse(success, message);
        } catch (Exception e) { 
            //e.printStackTrace();
            message = "有记录存在外键约束,删除失败";
            writeJsonResponse(success, message);
        }
    }

    /*前台查询CarType信息*/
    public String FrontQueryCarType() {
        if(page == 0) page = 1;
        List<CarType> carTypeList = carTypeDAO.QueryCarTypeInfo(page);
        /*计算总的页数和总的记录数*/
        carTypeDAO.CalculateTotalPageAndRecordNumber();
        /*获取到总的页码数目*/
        totalPage = carTypeDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = carTypeDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("carTypeList",  carTypeList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("page", page);
        return "front_query_view";
    }

    /*查询要修改的CarType信息*/
    public String FrontShowCarTypeQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键carTypeId获取CarType对象*/
        CarType carType = carTypeDAO.GetCarTypeByCarTypeId(carTypeId);

        ctx.put("carType",  carType);
        return "front_show_view";
    }

    /*删除CarType信息*/
    public String DeleteCarType() {
        ActionContext ctx = ActionContext.getContext();
        try { 
            carTypeDAO.DeleteCarType(carTypeId);
            ctx.put("message", "CarType删除成功!");
            return "delete_success";
        } catch (Exception e) { 
            e.printStackTrace();
            ctx.put("error", "CarType删除失败!");
            return "error";
        }
    }

    /*后台导出到excel*/
    public String queryCarTypeOutputToExcel() { 
        List<CarType> carTypeList = carTypeDAO.QueryCarTypeInfo();
        ExportExcelUtil ex = new ExportExcelUtil();
        String title = "CarType信息记录"; 
        String[] headers = { "车辆类别id","车辆类别名称"};
        List<String[]> dataset = new ArrayList<String[]>(); 
        for(int i=0;i<carTypeList.size();i++) {
        	CarType carType = carTypeList.get(i); 
        	dataset.add(new String[]{carType.getCarTypeId() + "",carType.getCarTypeName()});
        }
        /*
        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="+"CarType.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;
    }
}
最近下载更多
朱朱啊哈  LV16 2023年2月1日
2017143155  LV12 2022年12月2日
bluesky2016  LV15 2022年7月4日
随便取个名字_哈哈  LV27 2022年6月10日
testuser1234567  LV24 2022年5月23日
and123456  LV11 2022年4月28日
ksksksks  LV11 2022年2月27日
mafangnu  LV8 2021年10月7日
雷阳雷  LV9 2021年6月26日
qqdasa  LV10 2021年6月20日
最近浏览更多
WaZiN7  LV6 昨天
hai666666  LV1 2023年12月25日
刘洋66666  LV1 2023年11月5日
bai6011  LV1 2023年6月5日
IlIlIIl  LV2 2023年5月9日
hhhhhz  LV7 2023年4月18日
yuanchuang  LV22 2023年4月11日
朱俪的邮件及存储  LV8 2023年4月11日
小熊专属  LV3 2023年2月25日
朱朱啊哈  LV16 2023年1月31日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友