后台配置进行界面的功能:导入、导出和查看(一)
一、引言
最近要做一个对后台配置进行界面化的功能,其中包含了导入,导出,和查看功能;第一次本人做这个,过程艰辛曲折,但是最后也给自己解决了,功能全不实现。虽然跟大牛比起来不算什么,但是这是自己每次遇到困难,不猥琐,迎难而上,最后解决了这些难题。所以很有成就感,所以将自己实现的过程记录下来。
二、需求一:实现某配置导入功能
我们现在还在使用spring+struts2+mybaits+mysql模式,所以只适用这个框架。springmvc和springboot其实只是在前台调用后台这块用的不一样,其实还是有共性的
- 需求详细:
将txt文件格式的内容导入mysql某表的一个列字段,该字段的数据类型是longblob型,所以longblob型也需要用到mybaits的typeHandler类型转换的知识点。
1. 主要介绍struts2代码,mybaits代码就不说了,很简单。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class NeScoreRegionAct extends ActionSupport 2 { 3 4 private static final long serialVersionUID = 1L; 5 6 private static final Logger LOG = Logger.getLogger(NeScoreRegionAct.class); 7 8 @Autowired 9 private INeScoreRegionService service; 10 11 private File file; 12 13 private String neid; 14 15 private String region; 16 17 private String ipcontent; 18 19 private Map<String, Object> result; 20 21 public String importFile() throws Exception 22 { 23 result = new HashMap<String, Object>(); 24 //1.创建list 存放ipcontent数据 25 List<String> ipcontentList = new ArrayList<String>(); 26 BufferedReader br = null; 27 try 28 { 29 //2.读取文件 30 br = new BufferedReader(new FileReader(file)); 31 String str = null; 32 while ((str = br.readLine()) != null) 33 { 34 //3.将文件内容放入list文件中 35 ipcontentList.add(str); 36 } 37 38 //4.把ipcontentList遍历用Stringbuffer进行储存 39 StringBuffer sb = new StringBuffer(); 40 41 for (String ip : ipcontentList) 42 { 43 sb.append(ip); 44 } 45 46 ipcontent = sb.toString(); 47 48 } 49 catch (FileNotFoundException e) 50 { 51 LOG.error(e); 52 } 53 finally 54 { 55 try 56 { 57 br.close(); 58 } 59 catch (IOException e) 60 { 61 LOG.error(e); 62 } 63 } 64 65 Map<String, Object> params = new HashMap<String, Object>(); 66 if (StringUtils.isNotBlank(neid)) 67 { 68 params.put("neid", neid); 69 } 70 if (StringUtils.isNotBlank(region)) 71 { 72 params.put("region", region); 73 } 74 params.put("ipcontent", ipcontent); 75 //5.导入数据 76 Boolean update = service.updateIpcontent(params); 77 if (update == true) 78 { 79 80 result.put("success", true); 81 } 82 else 83 { 84 result.put("data", " "); 85 } 86 return SUCCESS; 87 } 88 89 public File getFile() 90 { 91 return file; 92 } 93 94 public void setFile(final File file) 95 { 96 this.file = file; 97 } 98 99 public String getNeid() 100 { 101 return neid; 102 } 103 104 public void setNeid(final String neid) 105 { 106 this.neid = neid; 107 } 108 109 public String getRegion() 110 { 111 return region; 112 } 113 114 public void setRegion(final String region) 115 { 116 this.region = region; 117 } 118 119 public String getIpcontent() 120 { 121 return ipcontent; 122 } 123 124 public void setIpcontent(final String ipcontent) 125 { 126 this.ipcontent = ipcontent; 127 } 128 129 public Map<String, Object> getResult() 130 { 131 return result; 132 } 133 134 public void setResult(final Map<String, Object> result) 135 { 136 this.result = result; 137 } 138 139 }
三、需求二:实现某配置导出功能
- 需求详细:
将之前导入的数据,以浏览器的方式在平台界面上导出
1. 只介绍struts2代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @Service("exportFileCtrl") 2 @Scope("prototype") 3 public class NeScoreRegionDownloadFileAct extends ActionSupport 4 { 5 private static final long serialVersionUID = 1L; 6 7 private static final Logger LOG = Logger.getLogger(NeScoreRegionDownloadFileAct.class); 8 9 @Autowired 10 private INeScoreRegionService service; 11 12 private String neid; 13 14 private String region; 15 16 private Map<String, Object> result; 17 18 public String exportFile() 19 { 20 //获取req和response 21 HttpServletRequest req = ServletActionContext.getRequest(); 22 HttpServletResponse response = ServletActionContext.getResponse(); 23 24 //获取该节点下的区域的详单数据 25 Map<String, Object> params = new HashMap<String, Object>(); 26 if (StringUtils.isNotBlank(neid)) 27 { 28 params.put("neid", neid); 29 } 30 if (StringUtils.isNotBlank(region)) 31 { 32 params.put("region", region); 33 } 34 List<NeScoreRegion> query4list = service.query4list(params); 35 NeScoreRegion neScoreRegion = query4list.get(0); 36 String ipcontent = neScoreRegion.getIpcontent(); 37 List<String> ipList = Arrays.asList(ipcontent.split(";")); 38 39 BufferedOutputStream bos = null; 40 41 try 42 { 43 //清空输出流 44 response.reset(); 45 //以txt格式导出 46 response.setContentType("text/plain; charset=utf-8"); 47 //设置编码 48 response.setCharacterEncoding("utf-8"); 49 //设置导出的文件名NEID+REGION.TXT 50 String exportFileName = neid + "-" + region; 51 //设置txt文件名称编码,防止中文乱码 52 response.setHeader("Content-disposition", 53 "attachment; filename=" + URLEncoder.encode(exportFileName + ".txt", "UTF-8")); 54 55 bos = new BufferedOutputStream(response.getOutputStream()); 56 57 //不为空的处理 58 String ipseg = null; 59 60 for (String ip : ipList) 61 { 62 if (ip.equals("")) 63 { 64 ipseg = ""; 65 } 66 else 67 { 68 ipseg = ip + ";" + "\n"; 69 } 70 bos.write(ipseg.getBytes()); 71 bos.flush(); 72 } 73 74 } 75 catch (IOException e) 76 { 77 LOG.error(e); 78 } 79 finally 80 { 81 try 82 { 83 bos.close(); 84 } 85 catch (IOException e) 86 { 87 LOG.error(e); 88 } 89 90 } 91 92 return null; 93 } 94 95 public Map<String, Object> getResult() 96 { 97 return result; 98 } 99 100 public void setResult(final Map<String, Object> result) 101 { 102 this.result = result; 103 } 104 105 public String getNeid() 106 { 107 return neid; 108 } 109 110 public void setNeid(final String neid) 111 { 112 this.neid = neid; 113 } 114 115 public String getRegion() 116 { 117 return region; 118 } 119 120 public void setRegion(final String region) 121 { 122 this.region = region; 123 } 124 125 }
四、需求三:实现某配置查看功能
- 需求详细:
1. 调用远程服务端的文本文件,该文件有几十万条,如果一次展示会导致浏览器崩溃,所以采用截断的方法,根据前台展示的需要一部分一部分展示给前台。
2.调用远程服务端的文件,需要用到httpinvoker技术,所以改天单独列个专题说下这个使用方法。^_^
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class CostWeightAct extends ActionSupport 2 { 3 private static final Logger LOG = Logger.getLogger(CostWeightAct.class); 4 5 private static final long serialVersionUID = 1L; 6 7 @Autowired 8 private INeInfoService neInfoService; 9 10 //定义成本配置的服务端配置路径 11 private static final String AGENT_COST_WEIGHT_SERVICE = "/agentCostWeightService"; 12 13 @Value("${costweight.filepath}") 14 private String costweightfilepath; 15 16 private String neid; 17 18 private String operatorname; 19 20 private Map<String, Object> jsonRlt; 21 22 private int pageNo; 23 24 private int pageSize; 25 26 public String getCostIpContent() 27 { 28 try 29 { 30 LOG.debug("开始执行=======>"); 31 LOG.debug("获取neid: " + neid); 32 LOG.debug("获取operatorname:" + operatorname); 33 LOG.debug("获取costweightfilepath:" + costweightfilepath); 34 35 //1.调用agent接口 36 ICostWeightDetailsService proxy = 37 HttpInvokerProxyFactory.getProxy(ICostWeightDetailsService.class, getNeServiceUrl(neid)); 38 39 //2.获取对应的neid的对应运营商的ip列表 40 List<String> ipList = proxy.getCostWeightDetails(operatorname, costweightfilepath); 41 42 PageUtil page = getPage(ipList, pageNo, pageSize); 43 44 setJsonResult(true, page); 45 46 } 47 catch (DnsHijackException e) 48 { 49 setJsonResult(false, e.getMessage()); 50 } 51 catch (Exception e) 52 { 53 setJsonResult(false, StringUtils.isBlank(e.getMessage()) ? "未知异常" : e.getMessage()); 54 } 55 56 return SUCCESS; 57 } 58 59 private PageUtil getPage(final List<String> ipList, final int pageNo, final int pageSize) 60 { 61 PageUtil page = new PageUtil(); 62 //刚开始的页面为第一页 63 if (page.getCurrentPage() == null) 64 { 65 page.setCurrentPage(pageNo); 66 } 67 else 68 { 69 page.setCurrentPage(page.getCurrentPage()); 70 } 71 //设置每页数据为十条 72 page.setPageSize(pageSize); 73 //每页的开始数 74 page.setStar((page.getCurrentPage() - 1) * page.getPageSize()); 75 //list的大小 76 page.setTotalSize(ipList.size()); 77 int count = ipList.size(); 78 //设置总页数 79 page.setTotalPage((count % pageSize) == 0 ? count / pageSize : (count / pageSize) + 1); 80 //对list进行截取 81 page.setDataList(ipList.subList(pageSize * (pageNo - 1), (pageSize * pageNo) > count ? count 82 : (pageSize * pageNo))); 83 return page; 84 } 85 86 private String getNeServiceUrl(final String neid) 87 { 88 if (StringUtils.isBlank(neid)) 89 { 90 throw new DnsHijackException("未指定配置网元"); 91 } 92 93 NeInfo neInfo = neInfoService.getNeInfo(neid); 94 if (neInfo == null) 95 { 96 throw new DnsHijackException("网元不存在"); 97 } 98 99 if (neInfo.getAdminState().equals(NeAdminState.DELETE.getValue())) 100 { 101 throw new DnsHijackException("网元已被删除"); 102 } 103 104 return neInfo.getUrl() + AGENT_COST_WEIGHT_SERVICE; 105 } 106 107 public String getOperatorname() 108 { 109 return operatorname; 110 } 111 112 public void setOperatorname(final String operatorname) 113 { 114 this.operatorname = operatorname; 115 } 116 117 public String getNeid() 118 { 119 return neid; 120 } 121 122 public void setNeid(final String neid) 123 { 124 this.neid = neid; 125 } 126 127 public Map<String, Object> getJsonRlt() 128 { 129 return jsonRlt; 130 } 131 132 public void setJsonRlt(final Map<String, Object> jsonRlt) 133 { 134 this.jsonRlt = jsonRlt; 135 } 136 137 private void setJsonResult(final boolean success, final Object data) 138 { 139 this.jsonRlt = new HashMap<String, Object>(); 140 this.jsonRlt.put("success", success); 141 this.jsonRlt.put("data", data); 142 } 143 144 public int getPageNo() 145 { 146 return pageNo; 147 } 148 149 public void setPageNo(final int pageNo) 150 { 151 this.pageNo = pageNo; 152 } 153 154 public int getPageSize() 155 { 156 return pageSize; 157 } 158 159 public void setPageSize(final int pageSize) 160 { 161 this.pageSize = pageSize; 162 } 163 164 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class PageUtil 2 { 3 private Integer currentPage;//当前页 4 5 private int pageSize;//每页显示记录条数 6 7 private int totalPage;//总页数=总记录数/展示的条数 8 9 private List<?> dataList;//每页显示的数据 10 11 private int star;//开始数据 12 13 private int totalSize;//总记录数 14 15 public Integer getCurrentPage() 16 { 17 return currentPage; 18 } 19 20 public void setCurrentPage(final Integer currentPage) 21 { 22 this.currentPage = currentPage; 23 } 24 25 public int getPageSize() 26 { 27 return pageSize; 28 } 29 30 public void setPageSize(final int pageSize) 31 { 32 this.pageSize = pageSize; 33 } 34 35 public int getTotalPage() 36 { 37 return totalPage; 38 } 39 40 public void setTotalPage(final int totalPage) 41 { 42 this.totalPage = totalPage; 43 } 44 45 public List<?> getDataList() 46 { 47 return dataList; 48 } 49 50 public void setDataList(final List<?> dataList) 51 { 52 this.dataList = dataList; 53 } 54 55 public int getStar() 56 { 57 return star; 58 } 59 60 public void setStar(final int star) 61 { 62 this.star = star; 63 } 64 65 public int getTotalSize() 66 { 67 return totalSize; 68 } 69 70 public void setTotalSize(final int totalSize) 71 { 72 this.totalSize = totalSize; 73 } 74 75 }