配置好web.xml和struts.xml文件后,创建一个java文件继承ActionSupport

public class Mchart extends ActionSupport{@Overridepublic String execute() throws Exception {ActionContext ac=ActionContext.getContext();ac.getSession().put("flag", "1");DefaultPieDataset dpd = new DefaultPieDataset();dpd.setValue("Android开发", 25);dpd.setValue("Java 开发", 25);dpd.setValue("测试", 45);dpd.setValue("UI设计", 10);JFreeChart chart = ChartFactory.createPieChart("某公司人员组织数据图", dpd, true,false, false);chart.setTitle(new TextTitle("某公司人员组织数据图", new Font("宋体", Font.BOLD+ Font.ITALIC, 20)));chart.setBorderVisible(false);chart.setBackgroundImageAlpha(0.0f);LegendTitle legend = chart.getLegend(0);// 设置Legendlegend.setItemFont(new Font("宋体", Font.BOLD, 14));PiePlot plot = (PiePlot) chart.getPlot();// 设置Plotplot.setNoDataMessage("当前数据为空!");plot.setLabelFont(new Font("隶书", Font.BOLD, 16));plot.setLabelBackgroundPaint(null);plot.setLabelOutlinePaint(null);plot.setOutlinePaint(SystemColor.WHITE);plot.setLabelShadowPaint(SystemColor.WHITE);plot.setBackgroundPaint(SystemColor.WHITE);plot.setBackgroundImageAlpha(0.0f);plot.setLabelGenerator(new StandardPieSectionLabelGenerator(("{0}:{2}"), NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));String root = ServletActionContext.getServletContext().getRealPath("/tubiao" + File.separator + "data.png");try {ChartUtilities.saveChartAsPNG(new File(root), chart, 520, 250);} catch (IOException e) {e.printStackTrace();} System.out.println("asfsdf");return "success";}}

之后,在Jsp文件中直接调用图片位置就可以了。

<% if(session.getAttribute("flag")==null || session.getAttribute("flag").equals("")) {  String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":"+ request.getServerPort() + path + "/";response.sendRedirect(basePath + "pa/loading.action");   } %>the demo jsp title!

运行效果图如下:

丑了点,可以自己调整图片大小。