jfreeChart路径问题
按照网上的说法改了saveChartAsPNG()方法,写了个BarChart类,内容如下
public class BarChart extends ServletUtilities { protected static void createTempDir() { String tempDirName = "E:/myworkspace/st2/WebRoot/upload1"; if (tempDirName == null) { throw new RuntimeException("Temporary directory system property " + "(java.io.tmpdir) is null."); } // create the temporary directory if it doesn't exist File tempDir = new File(tempDirName); if (!tempDir.exists()) { tempDir.mkdirs(); } } public static String saveChartAsPNG(JFreeChart chart, int width, int height, ChartRenderingInfo info, HttpSession session) throws IOException { if (chart == null) { throw new IllegalArgumentException("Null 'chart' argument."); } createTempDir(); String prefix = ServletUtilities.getTempFilePrefix(); if (session == null) { prefix = ServletUtilities.getTempOneTimeFilePrefix(); } File tempFile = File.createTempFile(prefix, ".png", new File("E:/myworkspace/st2/WebRoot/upload1")); ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info); if (session != null) { ServletUtilities.registerChartForDeletion(tempFile, session); } System.out.println("tempFile.getName()=" + tempFile.getName()); return tempFile.getName(); }}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="org.jfree.data.general.DefaultPieDataset"%><%@ page import="org.jfree.chart.ChartFactory"%><%@ page import="org.jfree.chart.JFreeChart,org.jfree.chart.servlet.ServletUtilities,com.test.jfreechart.BarChart"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:form namespace="/"> <% DefaultPieDataset dpd = new DefaultPieDataset(); dpd.setValue("管理人员", 25); dpd.setValue("市场人员", 25); dpd.setValue("开发人员", 45); dpd.setValue("其他人员", 10); JFreeChart chart = ChartFactory.createPieChart("某公司组织结构图", dpd, true, false, false); String fileName = ""; fileName = BarChart .saveChartAsPNG(chart, 800, 600, session); System.out.println("fileName=" + fileName); String url = request.getContextPath() + "/DisplayChart?filename=" + fileName; System.out.println("url =" + url); %> <img src="<%=url%>" width="500" height="300"> </s:form> </body></html>
fileName=jfreechart-58472.png
url =/st2/DisplayChart?filename=jfreechart-58472.png
2010-9-27 14:24:17 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: No configuration found for the specified action: '/jfreeChart2.jsp' in namespace: '/'. Form action defaulting to 'action' attribute's literal value.
2010-9-27 14:24:17 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find action or result
There is no Action mapped for namespace / and action name DisplayChart. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:178)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
我有点看不懂它的警告了,它说namespace的问题,可是页面中没有用到action啊,我的web.xml里是这样配置的
<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class> org.jfree.chart.servlet.DisplayChart </servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/DisplayChart</url-pattern> </servlet-mapping>
double[] data = {85, 156, 179.5, 211, 123}; //The labels for the bar chart String[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"}; DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i = 0; i < data.length; i++) { dataset.addValue(data[i], null, labels[i]); } JFreeChart chart = ChartFactory.createBarChart3D("Weekly Server Load", "Work Week 25", "MBytes", dataset, PlotOrientation.VERTICAL, false, false, false); chart.setBackgroundPaint(new Color(0xE1E1E1)); CategoryPlot plot = chart.getCategoryPlot(); // 设置Y轴显示整数 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); CategoryAxis domainAxis = plot.getDomainAxis(); //设置距离图片左端距离 domainAxis.setLowerMargin(0.05); BarRenderer3D renderer = new BarRenderer3D(); //设置柱的颜色 renderer.setSeriesPaint(0, new Color(0xff00)); plot.setRenderer(renderer); //这里是保存图片到路径:默认是保存到tomcat的临时文件夹,你用我刚刚保存方法! //String filename = ServletUtilities.saveChartAsPNG(chart, 300, 280, null, session);