实现功能:java 从一个工程action 跳转到另外一个工程action
在我们实际编程的过程中,大家一定遇到过这种情况,那就是在一个工程中,需要使用到另外一个工程的实体Bean和方法。那么遇到这种情况我们应该怎么处理呢?就可以采用下面这种方法,将工程一中的参数传到工程二中来使用。最后得到我们需要的结果。详情代码如下:
工程一action:
@Controller @RequestMapping("/conferences.do") public class ConferencesAction extends BasicAction implements ServletContextAware{ private ConferencesService conferencesService; private ServletContext servletContext; private AdminuserOperatingService adminuserOperatingService; private AdminuserService adminuserService; private AdminuserConferencesService adminuserConferencesService; private ClientVersionService clientVersionService; private DataService dataService; private SpeakerService speakerService; private SessiongroupService sessiongroupService; private CheckCodesChyService checkCodesChyService; private EsmoService esmoService; //课件信息统计 @RequestMapping(params = "method=getKeJianCount",method = RequestMethod.GET) public void getKeJianCount(String[] bank2,String startTime,String endTime,ModelMap model,HttpServletRequest request,HttpServletResponse response) { try { JSONObject result = new JSONObject(); String bank2String = ""; int bank2Length = bank2.length; int state = 0; if(bank2Length != 0){ for (int i = 0; i < bank2Length; i++) { bank2String = bank2[i]+","; } int bank2StringLength = bank2String.length(); bank2String = bank2String.substring(0, (bank2StringLength-1)); HashMap<String,String> map = new HashMap<String, String>(); String url = request.getScheme()+"://"+request.getServerName()+":"+request.getLocalPort()+"/Exam"; startTime = URLEncoder.encode(startTime,"UTF-8"); endTime = URLEncoder.encode(endTime,"UTF-8"); HttpUtils httpUtils = new HttpUtils(); map.put("method", "getKeJianCount1"); map.put("bank2", bank2String); map.put("startTime", startTime); map.put("endTime", endTime); String jsonString = httpUtils.httpGet(url+"/data", map); JSONObject jsObject = JSONObject.fromObject(jsonString); result.accumulate("url", jsObject.getString("url")); state = 1; result.accumulate("state", state); writeToJson(response, result.toString()); } } catch (Exception e) { e.printStackTrace(); } } }
工程二action:
@Controller @RequestMapping("/data") public class DataAction extends BasicAction{ @Resource(name="dataService") private DataService dataService; //课件信息统计 @RequestMapping(params = "method=getKeJianCount1",method = RequestMethod.GET) public void getKeJianCount1(String startTime,String endTime,ModelMap model,HttpServletRequest request,HttpServletResponse response) { String urlPath = request.getScheme()+"://"+request.getServerName()+request.getContextPath(); XSSFWorkbook rwb1 = new XSSFWorkbook(); while(true) { if(rwb1.getNumberOfSheets() != 0){ rwb1.removeSheetAt(0); } else{ break; } } try { String bank2 = request.getParameter("bank2"); String[] keJianTypeArray = bank2.split(","); XSSFSheet ws1 = rwb1.createSheet("课件统计"); ws1.addMergedRegion(new CellRangeAddress(0, 1, 0, 0)); ws1.addMergedRegion(new CellRangeAddress(0, 1, 1, 1)); ws1.addMergedRegion(new CellRangeAddress(0, 1, 2, 2)); int count = 0;//行 XSSFRow row1 = ws1.createRow(count); row1.createCell(0).setCellValue("课件类别"); row1.createCell(1).setCellValue("课件标题"); row1.createCell(2).setCellValue("课件作者"); row1.createCell(3).setCellValue("APP"); count++; XSSFRow row4 = ws1.createRow(count); row4.createCell(3).setCellValue("总点击次数"); if(keJianTypeArray != null){ int keJianTypeArrayLength = keJianTypeArray.length; //1、遍历model_id 数组 for (int i = 0; i < keJianTypeArrayLength; i++) { int modelId = Integer.valueOf(keJianTypeArray[i]); String keJianType = modelId == 40?"CSCO年会 2016": modelId == 31?"CSCO年会 2017":modelId == 42 ?"BOA 2017":modelId == 39?"CSCO指南会 2017":"CSCO免疫肿瘤高峰论坛 2017"; //根据modelId查询某个课件类别的总点击量 int tatolClickNum = dataService.countKeJianTatolNumByModelId(modelId,startTime,endTime); count++; XSSFRow row2 = ws1.createRow(count); row2.createCell(0).setCellValue(keJianType); row2.createCell(1).setCellValue("所有课件总和"); row2.createCell(3).setCellValue(tatolClickNum); //2、根据model_id 来查询资源集合 List<Data> dataList = dataService.getDataListByModelId(modelId); //3、遍历得到的资源集合 得到统计数据 if(dataList != null && dataList.size() != 0){ for (Data data : dataList) { Integer dataId = data.getDataId(); String keJianTitle = data.getTitle(); String keJianAuthor = data.getAuthor(); //4、根据dataId统计被点击的次数 int clickNum = dataService.countKeJianNumByDataId(dataId,startTime,endTime); count++; XSSFRow row3 = ws1.createRow(count); row3.createCell(0).setCellValue(keJianType); row3.createCell(1).setCellValue(keJianTitle); row3.createCell(2).setCellValue(keJianAuthor); row3.createCell(3).setCellValue(clickNum); } } } } String filePath = request.getSession().getServletContext().getRealPath("files/execl"); File conFile = new File(filePath); //目录结构 if(!conFile.exists()) { conFile.mkdir(); } String fileName = "keJianCount.xlsx"; File file = new File(filePath+"/"+fileName); if(file.exists()) { file.delete(); } else{ file.createNewFile(); } FileOutputStream fout = new FileOutputStream(file); rwb1.write(fout); fout.close(); JSONObject jsonObject = new JSONObject(); jsonObject.accumulate("url",urlPath+"/files/execl/"+fileName); writejson(response, jsonObject.toString()); } catch (Exception e) { e.printStackTrace(); } } }
工具类:
public class HttpUtils { public static String httpGet(String urlAddress,Map<String, String> paramMap){ if(paramMap==null){ paramMap = new HashMap<String, String>(); } String [] params = new String[paramMap.size()]; int i = 0; for(String paramKey:paramMap.keySet()){ String param = paramKey+"="+paramMap.get(paramKey); params[i] = param; i++; } return httpGet(urlAddress, params); } public static String httpGet(String urlAddress,String []params){ URL url = null; HttpURLConnection con =null; BufferedReader in = null; StringBuffer result = new StringBuffer(); try { String paramsTemp = ""; for(String param:params){ if(param!=null&&!"".equals(param.trim())){ paramsTemp+="&"+param; } } if(paramsTemp.length() >0){ urlAddress = urlAddress+"?"+paramsTemp.substring(1,paramsTemp.length()); } System.out.println(urlAddress); url = new URL(urlAddress); con = (HttpURLConnection) url.openConnection(); con.setUseCaches(false); con.setRequestProperty("Cookie", ""); con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("User-agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0"); in = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8")); while (true) { String line = in.readLine(); System.out.println(line); if (line == null) { break; } else { result.append(line); } } } catch (IOException e) { e.printStackTrace(); }finally{ try { if(in!=null){ in.close(); } if(con!=null){ con.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return result.toString(); } }
工具类的测试方法:
public static void main(String[] args) { HashMap<String, String> param = new HashMap<String, String>(); param.put("id", "7102"); try { param.put("giveName", URLEncoder.encode("ghris zhang","utf-8")); } catch (Exception e) { e.printStackTrace(); } param.put("language", "en"); String result = HttpUtils.httpPost("http://reg.csco.org.cn/reg2015/io/check.asp", param); System.out.println(result); /*String enResult = ""; byte[] b = Base64.decodeFast("eyJpZCI6MTcwMCwibmFtZSI6IgAAACIsImdpdmVOYW1lIjoiAAAAIiwiZmFtaWx5TmFtZSI6InpoYW5ndGllYmlhbyIsIm1vYmlsZSI6IjE1MjIxNjUzNDAwIiwiZW1haWwiOiIyNzY3MzkyNzFAcXEuY29tIiwiYWRkcmVzcyI6IgAAAAAiLCJqb2JUaXRsZSI6IgAAIiwiRGVwYXJ0bWVudCI6IgAAIiwicGF5U3RhdGUiOjAsImlzbWVtYmVyIjowLCJzdGF0ZSI6MSwibXNnIjoiT0sifQ=="); List<Byte> bList = new ArrayList<Byte>(); try{ for (byte c : b) { if(c != 0){ bList.add(c); } } byte [] b1 = new byte[bList.size()]; for (int i = 0;i < bList.size();i++) { b1[i] = bList.get(i); } enResult = new String(b1, "utf-8"); JSONObject json = JSONObject.fromObject(enResult); System.out.println(json); }catch(Exception e) { e.printStackTrace(); } */ }
宽容别人,就是肚量;谦卑自己,就是份量;合起来,就是一个人的质量。