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.AreaInfoDAO; import com.chengxusheji.domain.AreaInfo; @Controller @Scope("prototype") public class AreaInfoAction 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 areaId; public void setAreaId(int areaId) { this.areaId = areaId; } public int getAreaId() { return areaId; } /*要删除的记录主键集合*/ private String areaIds; public String getAreaIds() { return areaIds; } public void setAreaIds(String areaIds) { this.areaIds = areaIds; } /*当前查询的总记录数目*/ private int recordNumber; public void setRecordNumber(int recordNumber) { this.recordNumber = recordNumber; } public int getRecordNumber() { return recordNumber; } /*业务层对象*/ @Resource AreaInfoDAO areaInfoDAO; /*待操作的AreaInfo对象*/ private AreaInfo areaInfo; public void setAreaInfo(AreaInfo areaInfo) { this.areaInfo = areaInfo; } public AreaInfo getAreaInfo() { return this.areaInfo; } /*ajax添加AreaInfo信息*/ @SuppressWarnings("deprecation") public void ajaxAddAreaInfo() throws IOException, JSONException { String message = ""; boolean success = false; try { areaInfoDAO.AddAreaInfo(areaInfo); success = true; writeJsonResponse(success, message); } catch (Exception e) { e.printStackTrace(); message = "AreaInfo添加失败!"; 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(); } /*查询AreaInfo信息*/ public void ajaxQueryAreaInfo() throws IOException, JSONException { if(page == 0) page = 1; if(rows != 0) areaInfoDAO.setRows(rows); List<AreaInfo> areaInfoList = areaInfoDAO.QueryAreaInfoInfo(page); /*计算总的页数和总的记录数*/ areaInfoDAO.CalculateTotalPageAndRecordNumber(); /*获取到总的页码数目*/ totalPage = areaInfoDAO.getTotalPage(); /*当前查询条件下总记录数*/ recordNumber = areaInfoDAO.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(AreaInfo areaInfo:areaInfoList) { JSONObject jsonAreaInfo = areaInfo.getJsonObject(); jsonArray.put(jsonAreaInfo); } jsonObj.accumulate("rows", jsonArray); out.println(jsonObj.toString()); out.flush(); out.close(); } /*查询AreaInfo信息*/ public void ajaxQueryAllAreaInfo() throws IOException, JSONException { List<AreaInfo> areaInfoList = areaInfoDAO.QueryAllAreaInfoInfo(); HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType("text/json;charset=UTF-8"); PrintWriter out = response.getWriter(); //将要被返回到客户端的对象 JSONArray jsonArray = new JSONArray(); for(AreaInfo areaInfo:areaInfoList) { JSONObject jsonAreaInfo = new JSONObject(); jsonAreaInfo.accumulate("areaId", areaInfo.getAreaId()); jsonAreaInfo.accumulate("areaName", areaInfo.getAreaName()); jsonArray.put(jsonAreaInfo); } out.println(jsonArray.toString()); out.flush(); out.close(); } /*查询要修改的AreaInfo信息*/ public void ajaxModifyAreaInfoQuery() throws IOException, JSONException { /*根据主键areaId获取AreaInfo对象*/ AreaInfo areaInfo = areaInfoDAO.GetAreaInfoByAreaId(areaId); HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType("text/json;charset=UTF-8"); PrintWriter out = response.getWriter(); //将要被返回到客户端的对象 JSONObject jsonAreaInfo = areaInfo.getJsonObject(); out.println(jsonAreaInfo.toString()); out.flush(); out.close(); }; /*更新修改AreaInfo信息*/ public void ajaxModifyAreaInfo() throws IOException, JSONException{ String message = ""; boolean success = false; try { areaInfoDAO.UpdateAreaInfo(areaInfo); success = true; writeJsonResponse(success, message); } catch (Exception e) { message = "AreaInfo修改失败!"; writeJsonResponse(success, message); } } /*删除AreaInfo信息*/ public void ajaxDeleteAreaInfo() throws IOException, JSONException { String message = ""; boolean success = false; try { String _areaIds[] = areaIds.split(","); for(String _areaId: _areaIds) { areaInfoDAO.DeleteAreaInfo(Integer.parseInt(_areaId)); } success = true; message = _areaIds.length + "条记录删除成功"; writeJsonResponse(success, message); } catch (Exception e) { //e.printStackTrace(); message = "有记录存在外键约束,删除失败"; writeJsonResponse(success, message); } } /*前台查询AreaInfo信息*/ public String FrontQueryAreaInfo() { if(page == 0) page = 1; List<AreaInfo> areaInfoList = areaInfoDAO.QueryAreaInfoInfo(page); /*计算总的页数和总的记录数*/ areaInfoDAO.CalculateTotalPageAndRecordNumber(); /*获取到总的页码数目*/ totalPage = areaInfoDAO.getTotalPage(); /*当前查询条件下总记录数*/ recordNumber = areaInfoDAO.getRecordNumber(); ActionContext ctx = ActionContext.getContext(); ctx.put("areaInfoList", areaInfoList); ctx.put("totalPage", totalPage); ctx.put("recordNumber", recordNumber); ctx.put("page", page); return "front_query_view"; } /*查询要修改的AreaInfo信息*/ public String FrontShowAreaInfoQuery() { ActionContext ctx = ActionContext.getContext(); /*根据主键areaId获取AreaInfo对象*/ AreaInfo areaInfo = areaInfoDAO.GetAreaInfoByAreaId(areaId); ctx.put("areaInfo", areaInfo); return "front_show_view"; } /*删除AreaInfo信息*/ public String DeleteAreaInfo() { ActionContext ctx = ActionContext.getContext(); try { areaInfoDAO.DeleteAreaInfo(areaId); ctx.put("message", "AreaInfo删除成功!"); return "delete_success"; } catch (Exception e) { e.printStackTrace(); ctx.put("error", "AreaInfo删除失败!"); return "error"; } } /*后台导出到excel*/ public String queryAreaInfoOutputToExcel() { List<AreaInfo> areaInfoList = areaInfoDAO.QueryAreaInfoInfo(); ExportExcelUtil ex = new ExportExcelUtil(); String title = "AreaInfo信息记录"; String[] headers = { "区域编号","区域名称","停车费用"}; List<String[]> dataset = new ArrayList<String[]>(); for(int i=0;i<areaInfoList.size();i++) { AreaInfo areaInfo = areaInfoList.get(i); dataset.add(new String[]{areaInfo.getAreaId() + "",areaInfo.getAreaName(),areaInfo.getPrice() + ""}); } /* 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="+"AreaInfo.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日