Struts2.1之Jfreechart

    只说基本使用,以饼图为例。其余参考jfreechart的demo即可。首先项目中引入jcommon-*.jar和jfreechart-*.jar,这两个是必须的。项目中加入struts2中的struts 2 jfreechart libraries,主要是使用程序集中的返回类型即org.apache.struts2.dispatcher.ChartResult。

在struts.xml中配置返回类型

<result-types>
   <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
  </result-types>

 

在struts2中使用jfreechart就不用demo中的servlet了,去掉servlet配置。首先些action

public class PieImage extends ActionSupport {

 
 private JFreeChart chart;
 
 public JFreeChart getChart() {
  return chart;
 }

 public void setChart(JFreeChart chart) {
  this.chart = 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对象,注意struts.xml中action的reuslt是根据ValueStack中的属性找到图形对象的,下面加入值栈即可
        chart = ChartFactory.createPieChart("Pie Chart", data, true,true, false);
       
        return SUCCESS;
 }
}

action写完了,配置mapping。

<action name="test" class="PieImage">
   <result type="chart">
    <param name="width">600</param>
    <param name="height">400</param>
   </result>
  </action> 

注意不要有返回页面,那里使用jfreechart生成的图片就引用action的mapping。

页面中如果要引用上面生成的图片,在页面中加入代码

<img src="test"/>就ok了。

 

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值