zoukankan      html  css  js  c++  java
  • (转)Struts2+JFreeChart 环境搭建个基本用法!

    正题
         下面以边帖图片和代码的方式来讲解Struts2JFreeChart的整合。
         搭建环境:首先帖一张工程的目录结构以及所需的jar包。注意:如果你不打算自己写ChartResult的话只需要引入struts2-jfreechart-plugin-2.0.6.jar(这个在struts-2.0.6-all.zip可以找到了):
             

    注意:除去struts2需要的基本包外,Jfreechart所用到的包就是最下面的那三个!

    1.依次帖web.xml、struts.xml、struts.properties和struts-jfreechart.xml几个配置文件的代码:
            web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>
                org.apache.struts2.dispatcher.FilterDispatcher
            </filter-class>
        </filter>
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>

            struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC 
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
        "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>
        <include file="struts-jfreechart.xml" />
    </struts>

            struts.properties

    struts.ui.theme=simple

            struts-jfreechart.xml 

    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <package name="jFreeChartDemonstration" extends="struts-default"
            namespace="/jfreechart">
            <result-types>
                <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
            </result-types>
            <action name="JFreeChartAction" class="com.tangjun.struts2.JFreeChartAction">
                  <result type="chart"> 
                       <param name="width">400</param>
                       <param name="height">300</param>
                </result>
            </action>
        </package>
    </struts>

            说明:这里只需要说明下struts-jfreechart.xml,这里直接调用已经写好的类ChartResult,这个类是继承自 com.opensymphony.xwork2.Result,传入生成图片大小的参数width和height就可以了。

           2.
    新建JFreeChartAction继承ActionSupport,生成JFreeChart对象并保存到chart中,注意这个名称是固定的。

    package com.tangjun.struts2;

    import com.opensymphony.xwork2.ActionSupport;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.JFreeChart;
    import org.jfree.data.general.DefaultPieDataset;

    public class JFreeChartAction extends ActionSupport {

        /**
         * 
         */
        private static final long serialVersionUID = 5752180822913527064L;

        //供ChartResult调用->ActionInvocation.getStack().findValue("chart")
        private JFreeChart chart;
        
        @Override
        public String execute() throws Exception {
            //设置数据
            DefaultPieDataset data = new DefaultPieDataset();
            data.setValue("Java"new Double(43.2));
            data.setValue("Visual Basic"new Double(1.0));
            data.setValue("C/C++"new Double(17.5));
            data.setValue("tangjun"new Double(60.0));
            //生成JFreeChart对象
            chart = ChartFactory.createPieChart("Pie Chart", data, true,truefalse);
            
            return SUCCESS;
        }

        public JFreeChart getChart() {
            return chart;
        }

        public void setChart(JFreeChart chart) {
            this.chart = chart;
        }
    }


    OK!至此代码已经全部贴完。
    输入访问 http://localhost:8080/Struts2JFreeChart/jfreechart/JFreeChartAction.action
    显示结果如下:

  • 相关阅读:
    Linux内存分析
    mysql 分表
    安装YCM
    c/c++ 之静态库
    ubuntu20 宽带连接
    数据对齐
    计算机中浮点数的表示
    整数的表示
    信息的储存
    SparseTable ST表
  • 原文地址:https://www.cnblogs.com/com-wushuang/p/4944574.html
Copyright © 2011-2022 走看看