Struts2+JFreeChart

下面以边帖图片和代码的方式来讲解Struts2与JFreeChart的整合。
     搭建环境:首先帖一张工程的目录结构以及所需的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,truefalse);
        
        
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,如需转载请自行联系原博主。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278949.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STM32的FLASH ID加密

#define FLASH_ID_OFFSET 30000 //任意定义一个数 //把地址直接减去或者加上一个数是不要程序中直接出现这个地址 volatile u32 Flash_ID_addr[3]{ 0x1FFFF7E8 - FLASH_ID_OFFSET, 0x1FFFF7EC FLASH_ID_OFFSET, 0x1FFFF7F0 - FLASH_ID_OFFSET }; /**读取STM32 FLASH ID*…

linux c视频如何加水印,如何在Kdenlive的视频上进行水印 | MOS86

如果你这些东西被称为水印。他们So&#xff0c;你如何在Linux中创建水印&#xff1f;嗯&#xff0c;你这可能是Linux上最强大的开源视频编辑器。Installation如果您尚未安装Kdenlive&#xff0c;您应该可以在包裹管理器中找到它。在Ubuntu中&#xff0c;您还可以使用命令:sudo …

mac触控板手势无法使用_如何在iPad上使用触控板手势

mac触控板手势无法使用Apple苹果Apple’s new floating Magic Keyboard case for the iPad Pro looks fantastic, but you don’t need to spend $299 to use a trackpad. Simply connect a Magic Trackpad or a third-party multi-touch trackpad to get access to all of iPa…

02.并发编程(2)Thread类源码分析

概述 在说线程之前先说下进程&#xff0c;进程和线程都是一个时间段的描述&#xff0c;是CPU工作时间段的描述。 进程&#xff0c;是并发执行的程序在执行过程中分配和管理资源的基本单位&#xff0c;是一个动态概念&#xff0c;竟争计算机系统资源的基本单位。每一个进程都有一…

安装SQLserver2008

双击点击setup&#xff0c;以管理员身份运行&#xff1b; 点击安装-》全新SQLServer独立安装或向现有安装添加功能 选择下一步&#xff0c;然后点击具有高级服务的express版本&#xff0c;点击下一步&#xff1b; 点击选择我接受许可条款&#xff0c;然后继续点击下一步&#x…

如何在没有Word的情况下打开Microsoft Word文档

Microsoft Word is part of Microsoft Office and requires an up-front purchase or a Microsoft 365 subscription. If you’re using a computer without Word installed, there are other ways to view that DOCX or DOC file. Microsoft Word是Microsoft Office的一部分&a…

redhat9Linux解压gz,linux (redhat9)下subversion 的安装

搞了一个subversion 花费了我两天的时间&#xff0c;其间虽然有干其他的事情&#xff0c;但是来来回回的装&#xff0c;搞的我是一点脾气都没有了&#xff0c;俗话说不经历风雨真的见不到彩虹。就是这个意思. 原本本的下来一.准备好安装包打算使用apache来浏览subversion &…

数组去重的4种方法(Which one is the fastest???嘻嘻嘻....)

<!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8"> <title>Document</title> </head> <body> <input type"button" value"数组去重1" οnclick"show()"&g…

flask中的模型

1.什么是模型   模型&#xff0c;是根据数据库中表的结构而创建出来的class。每一张表对应到编程语言中&#xff0c;就是一个class表中的每一个列对应到编程语言中就class中的一个属性。 2.ORM的三大特征   1.数据表(table)到编程类(class)的映射     数据库中的每一张…

windows复制文件路径_如何在Windows 10上复制文件的完整路径

windows复制文件路径Sometimes, it’s handy to copy the full path of a file or folder in Windows 10 to the clipboard. That way, you can paste the path into an open or upload dialog quickly without having to browse for it the file. Luckily, there’s an easy w…

用c语言复制字符串的元音字母,急求:编写程序,将一个字符串中的元音字母复制到另一个字符串,然后输出。...

#include#includevoid str(char a[100],char b[100]){int i0, j0;while(a[i]!\0)//\0代表ASCLL码0的字符&#xff0c;即是一个空操作符也就是是结束符;{if(a[i]a||a[i]e||a[i]i||a[i]o||a[i]u){b[j]a[i];j;}i;}}int main(){char a[100];char b[100];gets(a);str(a,b);puts(b);r…

05 替换空格

题目描述&#xff1a; 请实现一个函数&#xff0c;将一个字符串中的每个空格替换成“%20”。例如&#xff0c;当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 解题思路有&#xff1a; #判断字符串是否为空&#xff0c;判断length是否大于0。 #记录空格的数…

超链接禁用_在Microsoft Word 2003和2007中禁用自动超链接

超链接禁用If you can’t stand the automatic hyperlinking in Microsoft Word, you might be hard-pressed to find the right place to disable it in Office 2007, since all the settings are hidden so well compared to previous versions. 如果您无法在Microsoft Word中…

c语言如何创建虚拟串口,模拟串口的C语言源程序代码

本程序是模拟串口硬件机制写的&#xff0c;使用时可设一定时中断&#xff0c;时间间隔为1/4波特率&#xff0c;每中断一次调用一次接收函数&#xff0c; 每中断4次调用一次发送函数,不过.对单片机来说时钟并须要快.要知道9600的波特率的每个BIT的时间间隔是104us.而单片机中断一…

webjars管理静态资源

webjars用途简单解释 : 利用Servlet3协议规范中,包含在JAR文件/META-INF/resources/路径下的资源可以直接被web访问到这一原理&#xff0c;将前端静态资源打成jar包方便管理 静态资源打jar包 新建maven工程&#xff0c; 将需要打包的静态资源放入src/main/resources中 2. ma…

Windows Intellij环境下Gradle的 “Could not determine Java version from ‘9.0.1’”的解决方式...

当我导入Gradle项目初试Java spring的时候&#xff0c;遇到下面报错: Gradle complete project refresh failed Error:Could not determine java version from 9.0.1. 参考这篇 http://www.ddiinnxx.com/solving-not-determine-java-version-9-0-1-gradle-intellij-macosx/ 进行…

如何计算iPhone和Apple Watch上的步数

Khamosh PathakKhamosh PathakAccording to conventional wisdom, 10,000 steps a day equals a healthy life. No matter what your target is, though, you’ll need a reliable way to count your steps. The good news is you can do so on your iPhone or Apple Watch! 根…

在c语言中load,一道题理清Objective-C中的load和initialize

Objective-C中有两个方法比较特殊&#xff0c;他们会在Runtime时根据情况自动调用&#xff0c;下面我们简单分析一下调用时机以及使用场景~一般在iOS初中级面试时偶尔会被问到load和initialize方法&#xff0c;我出了一道题&#xff0c;估计会搞晕很多人。大家来看一下下面的程…

018.Zabbix维护时间和模板导入

一 维护时间 在某些正常业务维护期间&#xff0c;不需要进行告警&#xff0c;可添加维护时间。二 维护时间添加 2.1 维护 参数描述Name维护名称Maintenance type两种维护类型可选:With data collection - 依旧收集数据No data collection - 暂停收集数据Active since维护周期开…

本地服务器下的局域网安全吗_本地安全认证服务器

本地服务器下的局域网安全吗Today a reader had a very good question about lsass.exe is the Microsoft security management process for domain access and local security policies. Simply put it manages who logs on to your PC and/or Server. There are a few viru…