Let's go inside

          this blog is deprecated as a result of laziness.
          posts - 59, comments - 2, trackbacks - 0, articles - 0

          Spring JFreeChart 學習筆記

          Posted on 2006-08-28 16:57 Earth 閱讀(2192) 評論(0)  編輯  收藏 所屬分類: Java

          在上一個示例 (spring struts ibatis) 的基礎上引入 JFreeChart 組件。

          問題:統計各部門員工的人數,并用餅狀或柱狀圖顯示出來。

          PieChart.jpg
          BarChart3D.jpg
          ?
          要把 JFreeChart 引入到工程中來,有以下 3 步:

          1.? lib 中加入 jcommon-1.0.0.jar, jfreechart-1.0.0.jar

          2.? 寫一個 servlet : ChartServlet.java

          3.? 在頁面中調用這個 servlet 輸出圖形 .

          首先以文本的方式

          ?????? < table >

          ?????????? < c:forEach var = "countMap" items = " ${employeeCounts}">

          ????????????? < tr >

          ????????????????? < td >

          ???????????????????? < c:out value = " ${countMap.DEPT_NAME}"></c:out>

          ????????????????? </ td >

          ????????????????? < td >

          ???????????????????? < c:out value = " ${countMap.EMP_COUNT}"></c:out>

          ????????????????? </ td >

          ????????????? </ tr >

          ?????????? </ c:forEach >

          ?????? </ table >

          再以圖形的方式

          < IMG src = " < c:url value = "/chart?type=pie" /> " alt = "by Department" >

          < IMG src = " < c:url value = "/chart?type=bar3d" /> " alt = "by Department" >

          ?

          學習筆記:

          1.? 起初我嘗試以 IoC 的方式將 Service 注入到 Servlet 中,不成功,后來查資料解決, 詳見: http://www.jactiongroup.net/spring/viewtopic.php?p=6443

          ?

          2. 第一步是寫 Servlet

          ?1?public?class?ChartServlet?extends?HttpServlet?{?
          ?2????????private?EmployeeService?empService;?
          ?3?
          ?4????????@Override?
          ?5????????protected?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?
          ?6??????????????????????throws?ServletException,?IOException?{?
          ?7???????????????doPost(request,?response);?
          ?8????????}?
          ?9?
          10????????@Override?
          11????????protected?void?doPost(HttpServletRequest?request,?
          12??????????????????????HttpServletResponse?response)?throws?ServletException,?IOException?{?
          13???????????????WebApplicationContext?wac?=?WebApplicationContextUtils?
          14?
          15?????????????????????????????.getRequiredWebApplicationContext(getServletContext());?
          16???????????????empService?=?(EmployeeService)?wac.getBean("employeeService");?
          17???????????????String?chartType?=?request.getParameter("type").toUpperCase();?
          18???????????????response.setContentType("image/jpeg");?
          19???????????????JFreeChart?chart?=?getChart(chartType);?
          20???????????????ChartUtilities.writeChartAsJPEG(response.getOutputStream(),?100,?chart,?
          21?
          22?????????????????????????????400,?300,?null);?
          23????????}?
          24?
          25????????public?JFreeChart?getChart(String?type)?{?
          26???????????????List?list?=?empService.getEmployeeCounts();?
          27???????????????JFreeChart?chart?=?null;?
          28???????????????if?(type.indexOf("PIE")?==?0)?{?
          29??????????????????????DefaultPieDataset?data?=?new?DefaultPieDataset();?
          30??????????????????????Iterator?it?=?list.iterator();?
          31??????????????????????while?(it.hasNext())?{?
          32?????????????????????????????Map?m?=?(Map)?it.next();?
          33?????????????????????????????data.setValue((String)?m.get("DEPT_NAME"),?(Long)?m?
          34?
          35???????????????????????????????????????????.get("EMP_COUNT"));?
          36?
          37??????????????????????}?
          38??????????????????????if?("PIE".equals(type))?{?
          39?????????????????????????????chart?=?ChartFactory.createPieChart("PieChart",?data,?false,?
          40?
          41???????????????????????????????????????????false,?false);?
          42??????????????????????}?else?{?
          43?????????????????????????????chart?=?ChartFactory.createPieChart3D("PieChart3D",?data,?
          44?
          45???????????????????????????????????????????false,?false,?false);?
          46??????????????????????}?
          47???????????????}?else?{?
          48??????????????????????DefaultCategoryDataset?data?=?new?DefaultCategoryDataset();?
          49??????????????????????Iterator?it?=?list.iterator();?
          50??????????????????????while?(it.hasNext())?{?
          51?????????????????????????????Map?m?=?(Map)?it.next();?
          52?????????????????????????????data.addValue((Long)?m.get("EMP_COUNT"),?"Department",?
          53?
          54???????????????????????????????????????????(String)?m.get("DEPT_NAME"));?
          55??????????????????????}?
          56?
          57??????????????????????if?("BAR".equals(type))?{?
          58?????????????????????????????chart?=?ChartFactory.createBarChart("BarChart",?"Department",?"Employee",?
          59?
          60???????????????????????????????????????????data,?PlotOrientation.VERTICAL,?false,?false,?false);?
          61??????????????????????}?else?{?
          62?????????????????????????????chart?=?ChartFactory.createBarChart3D("BarChart3D",?"Department",?
          63?
          64???????????????????????????????????????????"Employee",?data,?PlotOrientation.VERTICAL,?false,?false,?
          65?
          66???????????????????????????????????????????false);?
          67??????????????????????}?
          68???????????????}?
          69???????????????return?chart;?
          70????????}?
          71?
          72?}?
          73?
          74?


          其中會用到Service層中新增加的接口List list = empService.getEmployeeCounts();

          提示這個方法不存在,利用 Eclipse Ctrl+1 功能 , 逐層添加

          service -> serviceimpl -> dao - > daoimpl, 即快速又不易出錯。

          ?

          值得注意的是:

          第33行 , 這里要強制轉換成 Long 型,因為 iBATIS count( ) 函數的返回值為 Long 類型 .

          ?

          如果要在 Servlet 中使用 Spring 提供的 Bean 配置文件,需要用到 WebApplicationContextUtils ,不過先要在 web.xml 中添加一個 Listener 才可以使用。

          ??? <context-param>

          ?????? <param-name> contextConfigLocation </param-name>

          ?????? <param-value>

          ?????????? classpath:spring.xml </param-value>

          ??? </context-param>

          ??? <listener>

          ?????? <listener-class>

          ??? ??? org.springframework.web.context.ContextLoaderListener

          </listener-class>

          ??? </listener>

          ?

          最后一步是在 iBATIS Mapping 文件中加入 SQL 語句。。。。。。。

          ??? <select id= "getEmployeeCounts" resultClass= "java.util.HashMap" cacheModel= "employeesCache" >

          ??????? SELECT

          ??????? d.name as DEPT_NAME,

          ?????? count(e.id) as EMP_COUNT

          ??????? FROM employee e, department d

          ??????? WHERE e.dept_id = d.id

          ?????? GROUP BY d.name

          ??? </select> ?

          通過兩次 DEMO ,發現 iBATIS 算是不錯的東東, 配置比 Hibernate 簡單,使用上又比 JDBC 簡單。

          主站蜘蛛池模板: 洪泽县| 新闻| 承德县| 启东市| 岳阳县| 呼和浩特市| 抚远县| 福泉市| 独山县| 浏阳市| 梧州市| 义马市| 张家港市| 津市市| 武安市| 平塘县| 桐乡市| 施甸县| 泌阳县| 天镇县| 沅江市| 大厂| 崇明县| 永德县| 台湾省| 修水县| 丹棱县| 萨嘎县| 澎湖县| 廊坊市| 黑龙江省| 甘孜县| 射洪县| 叶城县| 马龙县| 涟源市| 贺州市| 四平市| 北辰区| 潞西市| 梧州市|