Struts2与JFreeChat简单结合使用

来源:互联网 发布:三星s4软件下载 编辑:程序博客网 时间:2024/05/20 19:31

1. 首先下载jfreechat包:http://www.jfree.org/jfreechart/,copy jfreechart-1.0.13.jar & jcommon-1.0.16.jar 到web-inf/lib文件夹下面,从sturts包里面copy struts2-jfreechart-plugin-2.1.6.jar到web-inf/lib文件夹下面

2. Create JFreeChatAction class & ChartResult class:

2.1 JFreeChatAction class

package common;

import ...

public class JFreeChartAction extends ActionSupport {

 //ChartResult will call this to create image->ActionInvocation.getStack().findValue("chart")
 private JFreeChart chart;

 private ICustomerInfoService service;

 

 private List<RiskRank> riskRanklist;

 public JFreeChartAction() {
  if (service == null)
   service = new CustomerInfoService();
 }

 public String chart() {
  //get data from database
  if (condition == null)
   condition = new Condition();
  //  customerInfoservice.getCustomerInfosByCondition(condition);      
  long total = service.getTotalCount(condition);
  List<Long> args = new ArrayList<Long>();
  args.add(total);

  Font font = new Font("宋体", Font.BOLD, 16);
  DefaultPieDataset data = new DefaultPieDataset();
  // add data to dataset

  for(;;){
    data.setValue(riskRank.getRankType() + ": " + count, count);

  }
    //create JFreeChart object (3D pie)

  // 使用getText获取i18n属性
  chart = ChartFactory.createPieChart3D(getText("image.title", args), data, true, true,
    Locale.CHINA);

//解决中文乱码问题
  //Set title to Chinese
  chart.getTitle().setFont(font);
  //Set plot to Chinese
  ((PiePlot)chart.getPlot()).setLabelFont(font);
  //Set Legend to Chinese
  chart.getLegend().setItemFont(font);
  return SUCCESS;
 }

 public JFreeChart getChart() {
  return chart;
 }

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

}

2.2 ChartResult.class

package common;

import ...

/**
 * @version 0.1
 * @author cloudlu
 *
 * @deprecated Not required, since png is the best image in network
 *
 */

public class ChartResult extends StrutsResultSupport {

    //image width
    private int width;
    //image height
    private int height;
    //image type: just support: jpg, png
    private String imageType;
    
    @Override
    protected void doExecute(String arg0, ActionInvocation invocation) throws Exception {
        JFreeChart chart =(JFreeChart) invocation.getStack().findValue("chart");
        HttpServletResponse response = ServletActionContext.getResponse();
        OutputStream os = response.getOutputStream();
       
        if("jpeg".equalsIgnoreCase(imageType) || "jpg".equalsIgnoreCase(imageType))
            ChartUtilities.writeChartAsJPEG(os, chart, width, height);
        else if("png".equalsIgnoreCase(imageType))
            ChartUtilities.writeChartAsPNG(os, chart, width, height);
        else
            ChartUtilities.writeChartAsJPEG(os, chart, width, height);
       
        os.flush();

    }
    public void setHeight(int height) {
        this.height = height;
    }

    public void setWidth(int width) {
        this.width = width;
    }
   
    public void setImageType(String imageType) {
        this.imageType = imageType;
    }

}
3. 配置struts.xml:

 <package name="chart" extends="jfreechart-default, interceptors" namespace="/chart">

<!-- access permission check interceptor -->
  <default-interceptor-ref name="access-default"/>
   <!-- detail see: http://cwiki.apache.org/WW/jfreechart-plugin.html -->

        <!-- default type is png, add new types, actually we should use png!!!

             推荐使用png,不使用自定义image type
        <result-types>
            <result-type name="chart" class="common.ChartResult"></result-type>
        </result-types>
   -->
        <action name="chart" class="common.JFreeChartAction" method="chart">
      
              <result name="success" type="chart">
                   <param name="width">350</param>
                   <param name="height">250</param>
            </result>
           <!--
              <result type="chart">
                   <param name="width">350</param>
                   <param name="height">250</param>
                   <param name="imageType">png</param>
            </result>   -->
        </action>
 </package>

4. 页面 jfreechat.jsp:

<%@ taglib prefix="s" uri="/struts-tags" %>

<html> 

<body>

<img align="center" border="0" alt="<s:text name="image" />" id="indicator" src="<s:url action = 'chart' namespace='/chart' />" />

...other codes

</body>

</html>

 

参考文献:

[1]. http://struts.apache.org/2.x/docs/jfreechart-plugin.html

原创粉丝点击