<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(2191) 評論(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 簡單。

    主站蜘蛛池模板: 国产精品免费无遮挡无码永久视频| 毛片免费全部播放无码 | 亚洲日本va午夜中文字幕一区| 国产精品视频免费观看| 亚洲日韩精品无码专区加勒比☆| 免费在线一级毛片| 中文字幕天天躁日日躁狠狠躁免费| 亚洲AV成人精品一区二区三区| 亚洲人成人网站色www| 岛国片在线免费观看| 国产成人无码区免费内射一片色欲 | 精品久久洲久久久久护士免费| 中文字幕乱码系列免费| 日韩亚洲不卡在线视频中文字幕在线观看 | 91久久成人免费| 成人在线免费视频| 亚洲中文字幕在线无码一区二区| 亚洲不卡无码av中文字幕| 88av免费观看入口在线| 又大又硬又粗又黄的视频免费看 | 免费在线视频你懂的| 免费在线观看一区| 亚洲五月丁香综合视频| 亚洲av之男人的天堂网站| 国产jizzjizz视频免费看| 永久在线免费观看| 男人天堂免费视频| 曰批全过程免费视频免费看| 91亚洲国产成人久久精品| 亚洲精品国产美女久久久| 国产成人高清精品免费鸭子| 99视频全部免费精品全部四虎| 成在线人视频免费视频| 国产亚洲精品2021自在线| 亚洲无吗在线视频| 亚洲欧洲日产国码二区首页| 亚洲精品乱码久久久久久久久久久久 | 国产无遮挡裸体免费视频| 国产成人免费高清激情视频| 免费播放一区二区三区| 国产性生大片免费观看性|