Directly on the code:
The custom type code is as follows:
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;
Picture width
private int width;
Picture height
private int height;
Picture Type 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;
}
}
Where chart is the variable in action:
public class Jfreechartaction extends Actionsupport {
For Chartresult call->actioninvocation.getstack (). Findvalue ("chart")
Private Jfreechart chart;
........
}
Struts.xml file:
<package name= "jfreechartdemonstration" extends= "Struts-default"
Namespace= "/jfreechart" >
<!--custom return type--
<result-types>
<result-type name= "Chart" class= "Com.jfreechart.ChartResult" ></result-type>
</result-types>
<!--all kinds of graphics--
<action name= "jfreechart-*" class= "com.jfreechart.JFreeChartAction" method= "{1}" >
<result type= "Chart" >
<param name= "width" >400</param>
<param name= "Height" >300</param>
<param name= "ImageType" >jpg</param>
</result>
</action>
</package>