Struts2与JFreeChat简单结合使用
来源:互联网 发布:三星s4软件下载 编辑:程序博客网 时间:2024/05/20 19:31
1. 首先下载jfreechat包:http://www.jfree.org/jfreechart/,copy jfreechart-1.0.13.jar & jcommon-1.0.16.jar 到web-inf/lib文件夹下面,从sturts包里面copy struts2-jfreechart-plugin-2.1.6.jar到web-inf/lib文件夹下面
2. Create JFreeChatAction class & ChartResult class:
2.1 JFreeChatAction class
package common;
import ...
public class JFreeChartAction extends ActionSupport {
//ChartResult will call this to create image->ActionInvocation.getStack().findValue("chart")
private JFreeChart chart;
private ICustomerInfoService service;
private List<RiskRank> riskRanklist;
public JFreeChartAction() {
if (service == null)
service = new CustomerInfoService();
}
public String chart() {
//get data from database
if (condition == null)
condition = new Condition();
// customerInfoservice.getCustomerInfosByCondition(condition);
long total = service.getTotalCount(condition);
List<Long> args = new ArrayList<Long>();
args.add(total);
Font font = new Font("宋体", Font.BOLD, 16);
DefaultPieDataset data = new DefaultPieDataset();
// add data to dataset
for(;;){
data.setValue(riskRank.getRankType() + ": " + count, count);
}
//create JFreeChart object (3D pie)
// 使用getText获取i18n属性
chart = ChartFactory.createPieChart3D(getText("image.title", args), data, true, true,
Locale.CHINA);
//解决中文乱码问题
//Set title to Chinese
chart.getTitle().setFont(font);
//Set plot to Chinese
((PiePlot)chart.getPlot()).setLabelFont(font);
//Set Legend to Chinese
chart.getLegend().setItemFont(font);
return SUCCESS;
}
public JFreeChart getChart() {
return chart;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}
}
2.2 ChartResult.class
package common;
import ...
/**
* @version 0.1
* @author cloudlu
*
* @deprecated Not required, since png is the best image in network
*
*/
public class ChartResult extends StrutsResultSupport {
//image width
private int width;
//image height
private int height;
//image type: just support: 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;
}
}
3. 配置struts.xml:
<package name="chart" extends="jfreechart-default, interceptors" namespace="/chart">
<!-- access permission check interceptor -->
<default-interceptor-ref name="access-default"/>
<!-- detail see: http://cwiki.apache.org/WW/jfreechart-plugin.html -->
<!-- default type is png, add new types, actually we should use png!!!
推荐使用png,不使用自定义image type
<result-types>
<result-type name="chart" class="common.ChartResult"></result-type>
</result-types>
-->
<action name="chart" class="common.JFreeChartAction" method="chart">
<result name="success" type="chart">
<param name="width">350</param>
<param name="height">250</param>
</result>
<!--
<result type="chart">
<param name="width">350</param>
<param name="height">250</param>
<param name="imageType">png</param>
</result> -->
</action>
</package>
4. 页面 jfreechat.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<img align="center" border="0" alt="<s:text name="image" />" id="indicator" src="<s:url action = 'chart' namespace='/chart' />" />
...other codes
</body>
</html>
参考文献:
[1]. http://struts.apache.org/2.x/docs/jfreechart-plugin.html
- Struts2与JFreeChat简单结合使用
- JfreeChat简单报表制作
- urlrewrite与struts2结合使用配置
- JAX-WS与Struts2的结合使用
- JSON与Struts2的结合使用
- Struts2与jQuery框架的结合使用
- jFreeChat使用方法与例子
- JFreeChat使用(一)
- jquery autocomplete 与struts2 json插件结合简单例子
- struts2与spring结合
- struts2与spring结合
- struts2与dao结合
- Struts2 与spring结合
- struts2与ajax结合
- jfreechart与struts2结合
- ext与struts2结合
- xdoclet与Ant结合使用-简单例子
- AngularJs与SpringMVC简单结合使用
- 从开始到现在
- java包装为windows后台服务
- jsp调applet检测jdk
- 给网页添加背景的方法
- VB快速查找大型文件中包含的字符串
- Struts2与JFreeChat简单结合使用
- 利用游标插入表的sql语句
- 什么是富客户端互联网应用程序?
- 给网页的按钮添加快捷键
- CS快速过关
- 如何用Javascript控制滚动条(ScrollBar)的位置(上下和左右)
- extern用法详解(转)
- java调用oracle存储过程1
- PL/SQL Developer