先看xml配置文件,如下: <action name="userInfo" class="com.test.action.UserAction" method="info">
<result name="success" type="chart">
<param name="height">400</param>
<param name="width">800</param>
</result>
</action> 这里生成的图表高度与宽度已指定了,分别为400与800,而实际上高度与宽度只有生成图表时才能准确的确定,
故这样写法是不好的,现我已在action中确定的高与宽,那这里配置要如何写呢?
我的写法好像不对,如下: <action name="userInfo" class="com.test.action.UserAction" method="info">
<result name="success" type="chart">
<param name="height">${height}</param>
<param name="width">${width}</param>
</result>
</action> 后来把${height}改为:%{#height}和height="${height}"均出错了,异常信息主要如下:
严重: Unable to set parameter [height] in result of type [org.apache.struts2.dispatcher.ChartResult]
Caught OgnlException while setting property 'height' on type 'org.apache.struts2.dispatcher.ChartResult'. - Class: ognl.OgnlRuntime
File: OgnlRuntime.java
Method: callAppropriateMethod
Line: 810 - ognl/OgnlRuntime.java:810:-1注:我在action已正常定义了height与width字段,并且有get,set方法了,类型为字符串(类型改为整型也不行)
我怀疑这样做法可以吗?真的不能在action中指定高宽么?应该可以吧?请有过这方面经验的高人指教,谢了.
<result name="success" type="chart">
<param name="height">400</param>
<param name="width">800</param>
</result>
</action> 这里生成的图表高度与宽度已指定了,分别为400与800,而实际上高度与宽度只有生成图表时才能准确的确定,
故这样写法是不好的,现我已在action中确定的高与宽,那这里配置要如何写呢?
我的写法好像不对,如下: <action name="userInfo" class="com.test.action.UserAction" method="info">
<result name="success" type="chart">
<param name="height">${height}</param>
<param name="width">${width}</param>
</result>
</action> 后来把${height}改为:%{#height}和height="${height}"均出错了,异常信息主要如下:
严重: Unable to set parameter [height] in result of type [org.apache.struts2.dispatcher.ChartResult]
Caught OgnlException while setting property 'height' on type 'org.apache.struts2.dispatcher.ChartResult'. - Class: ognl.OgnlRuntime
File: OgnlRuntime.java
Method: callAppropriateMethod
Line: 810 - ognl/OgnlRuntime.java:810:-1注:我在action已正常定义了height与width字段,并且有get,set方法了,类型为字符串(类型改为整型也不行)
我怀疑这样做法可以吗?真的不能在action中指定高宽么?应该可以吧?请有过这方面经验的高人指教,谢了.
String filename = ServletUtilities.saveChartAsPNG(chart, 1220, 490, null, session);
其中 1220就是宽,490就是高
我也是这样用的
filename = ServletUtilities.saveChartAsJPEG(chart, 800, 500, null, session);
String filename = ServletUtilities.saveChartAsPNG(chart, 600, 400, null, session);
这个方法是生成图片后,再调用图片显示的,这跟struts2没多大关系,直接在jsp中写就行了.
我现在要在strust2中设置,调用action后,直接返回chart对象到页面去显示的,必须要在xml中参数中对高宽进行设置的.两者不一样的,虽这个是可以指定图片大小,但我不用这个.下面的大家不用再提这个了.后来我直接把参数设置, <param name="height">400</param>
<param name="width">800</param>
去掉了,发现其默认生成的图表大小很小,如果能够改变默认的大小,我想在action中就可以解决了,参数这里可以不用设置了.现在问题是如何设置默认生成的图表大小?
String filename = ReportBarcategory.create(session,new PrintWriter(out), cp2);//生成文件名
String graphURL = request.getContextPath()+ "/servlet/DisplayChart?filename=" + filename;页面使用:
<img src="<%= graphURL %>" width=<%=width %> height=<%=height %> border=0 usemap="#<%= filename %>">
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,true, false);
return SUCCESS;
} public JFreeChart getChart() {
return chart;
} public void setChart(JFreeChart chart) {
this.chart = chart;
}
}OK!至此代码已经全部贴完。
输入访问 http://localhost:8080/Struts2JFreeChart/jfreechart/JFreeChartAction.action
显示结果如下:--------------------------------------------------------------------------------补充
以上生成的图片是PNG格式的图片,如果需要自定义图片格式的话(好像只能支持JPG和PNG格式),那么自己写一个ChartResult继承自StrutsResultSupport,见代码:
package com.tangjun.struts2.chartresult;import java.io.OutputStream;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;import com.opensymphony.xwork2.ActionInvocation;public class ChartResult extends StrutsResultSupport { /**
*
*/
private static final long serialVersionUID = 4199494785336139337L;
//图片宽度
private int width;
//图片高度
private int height;
//图片类型 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;
}}如此的话还需要小小的修改一下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-type name="chart" class="com.tangjun.struts2.chartresult.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>
-->
<result type="chart">
<param name="width">400</param>
<param name="height">300</param>
<param name="imageType">jpg</param>
</result>
</action>
</package>
</struts>
OK!显示的效果是一样的,只是图片格式不一样,当然这里面你可以做更多操作!
protected String height; //修改类型
protected String width; //修改类型
@Override
protected void doExecute(String arg0, ActionInvocation invocation)
throws Exception {
JFreeChart chart =(JFreeChart) invocation.getStack().findValue("chart");
//add by _zZ begin
ValueStack stack = invocation.getStack();
height = conditionalParse(height, invocation);
width = conditionalParse(width, invocation);
imageType = conditionalParse(imageType, invocation);
//add by _zZ end
HttpServletResponse response = ServletActionContext.getResponse();
OutputStream os = response.getOutputStream(); //add by _zZ begin
int h = Integer.parseInt(height);
int w = Integer.parseInt(width);
//add by _zZ end if("jpeg".equalsIgnoreCase(imageType) || "jpg".equalsIgnoreCase(imageType))
ChartUtilities.writeChartAsJPEG(os, chart, w, h);
else if("png".equalsIgnoreCase(imageType))
ChartUtilities.writeChartAsPNG(os, chart, w, h);
else
ChartUtilities.writeChartAsJPEG(os, chart, w, h);
os.flush();
}