Struts2+JFreeChart

简介:
下面以边帖图片和代码的方式来讲解Struts2JFreeChart的整合。
     搭建环境:首先帖一张工程的目录结构以及所需的jar包。注意:如果你不打算自己写ChartResult的话只需要引入 struts2-jfreechart-plugin-2.0.6.jar(这个在struts-2.0.6-all.zip可以找到了):
         
       1.依次帖web.xml、struts.xml、struts.properties和struts-jfreechart.xml几个配置文件的代码:
        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 >

本文转自博客园农民伯伯的博客,原文链接:Struts2+JFreeChart,如需转载请自行联系原博主。

目录
相关文章
|
设计模式 开发框架 安全
Struts,你为何死不悔改!
上篇文章《诡异的字符串问题。。。》的问题已经解决了,我一直相信「团队力量的重要性」,虽然我不能保证加入群的每一个人都是乐于分享的同学,但我始终群里的各位同学总会慢慢被我们这种乐于分享的群氛围所影响。就以上篇文章为例,群里的 Univechige 同学专门给 IntelliJ IDEA 官方发邮件寻求原因,这便是一个好的开端,我相信有各位同学的共同维护,后面群氛围会越来越好。下面给出 IDEA 官方的答复,见下图。
162 0
Struts,你为何死不悔改!
|
Java 前端开发 数据格式
|
前端开发 Java
JavaWeb - JSP & Freemarker & Velocity 区别
JavaWeb - JSP & Freemarker & Velocity 区别
145 0
|
Web App开发 Java Apache
struts
运行流程 客户端浏览器通过HTTP请求,访问控制器,然后控制器读取配置文件,然后执行服务器端跳转,执行相应的业务逻辑,然后,在调用模型层,取得的结果展示给jsp页面,最后返回给客户端浏览器 组成部分 struts 视图 标签库 控制器 action 模型层 ActionFrom JavaBean struts maven 安装官网 : https://struts.
1001 0
|
Web App开发 XML Java
JakartaEE Struts2使用
1. Struts2下载 解压后的目录结构如下: 图1.png 从一个高水平角度看,Struts2 是一个MVC拉动的(或MVC2)框架,Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的: 操作(Actions...
1001 0
|
数据安全/隐私保护 Java Maven
|
SQL JavaScript 前端开发