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.CarPositionDAO; import com.chengxusheji.domain.CarPosition; import com.chengxusheji.dao.AreaInfoDAO; import com.chengxusheji.domain.AreaInfo; @Controller @Scope("prototype") public class CarPositionAction extends ActionSupport { /*界面层需要查询的属性: 所在区域*/ private AreaInfo areaObj; public void setAreaObj(AreaInfo areaObj) { this.areaObj = areaObj; } public AreaInfo getAreaObj() { return this.areaObj; } /*界面层需要查询的属性: 车位编号*/ private String positionNo; public void setPositionNo(String positionNo) { this.positionNo = positionNo; } public String getPositionNo() { return this.positionNo; } /*当前第几页*/ 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 positionId; public void setPositionId(int positionId) { this.positionId = positionId; } public int getPositionId() { return positionId; } /*要删除的记录主键集合*/ private String positionIds; public String getPositionIds() { return positionIds; } public void setPositionIds(String positionIds) { this.positionIds = positionIds; } /*当前查询的总记录数目*/ private int recordNumber; public void setRecordNumber(int recordNumber) { this.recordNumber = recordNumber; } public int getRecordNumber() { return recordNumber; } /*业务层对象*/ @Resource CarPositionDAO carPositionDAO; @Resource AreaInfoDAO areaInfoDAO; /*待操作的CarPosition对象*/ private CarPosition carPosition; public void setCarPosition(CarPosition carPosition) { this.carPosition = carPosition; } public CarPosition getCarPosition() { return this.carPosition; } /*ajax添加CarPosition信息*/ @SuppressWarnings("deprecation") public void ajaxAddCarPosition() throws IOException, JSONException { String message = ""; boolean success = false; try { AreaInfo areaObj = areaInfoDAO.GetAreaInfoByAreaId(carPosition.getAreaObj().getAreaId()); carPosition.setAreaObj(areaObj); carPositionDAO.AddCarPosition(carPosition); success = true; writeJsonResponse(success, message); } catch (Exception e) { e.printStackTrace(); message = "CarPosition添加失败!"; 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(); } /*查询CarPosition信息*/ public void ajaxQueryCarPosition() throws IOException, JSONException { if(page == 0) page = 1; if(positionNo == null) positionNo = ""; if(rows != 0) carPositionDAO.setRows(rows); List<CarPosition> carPositionList = carPositionDAO.QueryCarPositionInfo(areaObj, positionNo, page); /*计算总的页数和总的记录数*/ carPositionDAO.CalculateTotalPageAndRecordNumber(areaObj, positionNo); /*获取到总的页码数目*/ totalPage = carPositionDAO.getTotalPage(); /*当前查询条件下总记录数*/ recordNumber = carPositionDAO.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(CarPosition carPosition:carPositionList) { JSONObject jsonCarPosition = carPosition.getJsonObject(); jsonArray.put(jsonCarPosition); } jsonObj.accumulate("rows", jsonArray); out.println(jsonObj.toString()); out.flush(); out.close(); } /*查询CarPosition信息*/ public void ajaxQueryAllCarPosition() throws IOException, JSONException { List<CarPosition> carPositionList = carPositionDAO.QueryAllCarPositionInfo(); HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType("text/json;charset=UTF-8"); PrintWriter out = response.getWriter(); //将要被返回到客户端的对象 JSONArray jsonArray = new JSONArray(); for(CarPosition carPosition:carPositionList) { JSONObject jsonCarPosition = new JSONObject(); jsonCarPosition.accumulate("positionId", carPosition.getPositionId()); jsonCarPosition.accumulate("positionNo", carPosition.getPositionNo()); jsonArray.put(jsonCarPosition); } out.println(jsonArray.toString()); out.flush(); out.close(); } /*查询要修改的CarPosition信息*/ public void ajaxModifyCarPositionQuery() throws IOException, JSONException { /*根据主键positionId获取CarPosition对象*/ CarPosition carPosition = carPositionDAO.GetCarPositionByPositionId(positionId); HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType("text/json;charset=UTF-8"); PrintWriter out = response.getWriter(); //将要被返回到客户端的对象 JSONObject jsonCarPosition = carPosition.getJsonObject(); out.println(jsonCarPosition.toString()); out.flush(); out.close(); }; /*更新修改CarPosition信息*/ public void ajaxModifyCarPosition() throws IOException, JSONException{ String message = ""; boolean success = false; try { AreaInfo areaObj = areaInfoDAO.GetAreaInfoByAreaId(carPosition.getAreaObj().getAreaId()); carPosition.setAreaObj(areaObj); carPositionDAO.UpdateCarPosition(carPosition); success = true; writeJsonResponse(success, message); } catch (Exception e) { message = "CarPosition修改失败!"; writeJsonResponse(success, message); } } /*删除CarPosition信息*/ public void ajaxDeleteCarPosition() throws IOException, JSONException { String message = ""; boolean success = false; try { String _positionIds[] = positionIds.split(","); for(String _positionId: _positionIds) { carPositionDAO.DeleteCarPosition(Integer.parseInt(_positionId)); } success = true; message = _positionIds.length + "条记录删除成功"; writeJsonResponse(success, message); } catch (Exception e) { //e.printStackTrace(); message = "有记录存在外键约束,删除失败"; writeJsonResponse(success, message); } } /*前台查询CarPosition信息*/ public String FrontQueryCarPosition() { if(page == 0) page = 1; if(positionNo == null) positionNo = ""; List<CarPosition> carPositionList = carPositionDAO.QueryCarPositionInfo(areaObj, positionNo, page); /*计算总的页数和总的记录数*/ carPositionDAO.CalculateTotalPageAndRecordNumber(areaObj, positionNo); /*获取到总的页码数目*/ totalPage = carPositionDAO.getTotalPage(); /*当前查询条件下总记录数*/ recordNumber = carPositionDAO.getRecordNumber(); ActionContext ctx = ActionContext.getContext(); ctx.put("carPositionList", carPositionList); ctx.put("totalPage", totalPage); ctx.put("recordNumber", recordNumber); ctx.put("page", page); ctx.put("areaObj", areaObj); List<AreaInfo> areaInfoList = areaInfoDAO.QueryAllAreaInfoInfo(); ctx.put("areaInfoList", areaInfoList); ctx.put("positionNo", positionNo); return "front_query_view"; } /*查询要修改的CarPosition信息*/ public String FrontShowCarPositionQuery() { ActionContext ctx = ActionContext.getContext(); /*根据主键positionId获取CarPosition对象*/ CarPosition carPosition = carPositionDAO.GetCarPositionByPositionId(positionId); List<AreaInfo> areaInfoList = areaInfoDAO.QueryAllAreaInfoInfo(); ctx.put("areaInfoList", areaInfoList); ctx.put("carPosition", carPosition); return "front_show_view"; } /*删除CarPosition信息*/ public String DeleteCarPosition() { ActionContext ctx = ActionContext.getContext(); try { carPositionDAO.DeleteCarPosition(positionId); ctx.put("message", "CarPosition删除成功!"); return "delete_success"; } catch (Exception e) { e.printStackTrace(); ctx.put("error", "CarPosition删除失败!"); return "error"; } } /*后台导出到excel*/ public String queryCarPositionOutputToExcel() { if(positionNo == null) positionNo = ""; List<CarPosition> carPositionList = carPositionDAO.QueryCarPositionInfo(areaObj,positionNo); ExportExcelUtil ex = new ExportExcelUtil(); String title = "CarPosition信息记录"; String[] headers = { "车位id","所在区域","车位编号","建立时间"}; List<String[]> dataset = new ArrayList<String[]>(); for(int i=0;i<carPositionList.size();i++) { CarPosition carPosition = carPositionList.get(i); dataset.add(new String[]{carPosition.getPositionId() + "",carPosition.getAreaObj().getAreaName(), carPosition.getPositionNo(),carPosition.getAddTime()}); } /* 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="+"CarPosition.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; } }
最近下载更多
wuying8208 LV15
10月23日
朱朱啊哈 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日