Struts2 Jfreechart Example Integration Tutorial

Source: Internet
Author: User

Struts2 Jfreechart Instance Integration
Objective
About STRUTS2 Introduction as well as improve and so here is not introduced, but about STRUTS2 study has the following recommendations: Struts2-showcase-2.0.6.war: This is the official with the demo (struts-2.0.6-all.zip/ Struts-2.0.6/apps directory), very comprehensive, direct deployment can be (a lot of friends Struts2 can learn very well I guess I still learn from here). Wiki-webwork: Into the door of friends should know, strust2 by Webwork2 and Struts1.x merged, but mainly to webwork2 mainly, so if you can not find Struts2 information to find webwork information to see. Max on Java blog, his blog information in the Chinese Struts2 is relatively full, written in detail. The code Project, Google-codesearch, koders: These search sites help me a lot when I can't find Chinese materials or even English articles.

About Jfreechart Introduction and so here I do not intend to introduce, a lot of Chinese information.

Business
The following pictures and code to explain the STRUTS2 and Jfreechart integration.
Build environment: First, post a project directory structure and the required jar package. Note: If you do not intend to write chartresult yourself, just introduce Struts2-jfreechart-plugin-2.0.6.jar (which is available in Struts-2.0.6-all.zip):

1. Post Web.xml, Struts.xml, Struts.properties, and struts-jfreechart.xml the code for several configuration files:
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" > </param >
< param name = "height" > </param >
</result >
</Action >
</Package >
</Struts >

Description: Here only need to explain the next struts-jfreechart.xml, where the direct call has been written class Chartresult, this class is inherited from Com.opensymphony.xwork2.Result, The parameter width and height of the generated picture size can be passed in.

2. New Jfreechartaction inherits Actionsupport, generates Jfreechart objects and saves them to chart, noting that the name is fixed. 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;

For Chartresult call->actioninvocation.getstack (). Findvalue ("chart")
Private Jfreechart chart;

@Override
Public String Execute () throws Exception {
Set data
Defaultpiedataset data = new Defaultpiedataset ();
Data.setvalue ("Java", New Double (43.2));
Data.setvalue ("Visual Basic", New Double (1.0));
Data.setvalue ("C + +", new Double (17.5));
Data.setvalue ("Tangjun", New Double (60.0));
Generating Jfreechart objects
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. This code is all posted.
Enter Access Http://localhost:8080/Struts2JFreeChart/jfreechart/JFreeChartAction.action
The results appear as follows:



Add
The image generated above is a PNG format picture, if you need to customize the picture format (as if you can only support JPG and PNG format), then write a chartresult inherited from Strutsresultsupport, see Code:

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;

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 ("

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.