STRUTS2 Custom Result return type

Source: Internet
Author: User

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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.