struts2使用插件(jfreechart图表插件)

1.导入jar包:[jcommon-1.0.20.jar],[jfreechart-1.0.16.jar],[struts2-jfreechart-plugin-2.3.15.1.jar](struts2整合第三方插件的jar包,这里针对jfreechart,其他的去struts2下载的lib文件夹里找plugin结尾的jar包)

2.使用第三方插件的时候,在它们的jar包中肯定会提供一个struts-plugin.xml文件(这里再回顾下struts2加载配置文件的顺序,struts-default.xml--->struts-plugin.xml--->struts.xml),我们只要继承这个文件里他们定义的包,如果它有自定义的结果集,则使用它的结果集,写上他定义的参数即可。


struts2-jfreechart-plugin-2.3.15.1.jar中的struts-plugin.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!--
/*
 * $Id: struts-plugin.xml 1221225 2011-12-20 12:22:28Z jogep $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
-->
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
    
<struts>
    <package name="jfreechart-default" extends="struts-default">
    
    	<result-types>
    		<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult">
    			<param name="height">150</param>
    			<param name="width">200</param>
    		</result-type>
    	</result-types>
    </package>

</struts>


例子:

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<!-- 自动加载 -->
    <constant name="struts.devMode" value="true" />
    <!-- 使用简单主题 -->
    <constant name="struts.ui.theme" value="simple"/>  
    <!-- 继承上面那个struts-plugin.xml文件定义的包 -->
    <package name="myChart" extends="jfreechart-default">
    	<action name="myChart" class="com.xxc.action.GetChartAction">
    		<result type="chart"><!-- 开始我还以为这里这样写,那么返回success的时候就没法跳转到一个页面了,其实大可不必,只要在jsp里img标签的src属性写这个action路径就可以显示了 -->
    			<param name="height">450</param>
    			<param name="width">700</param>
    		</result>
    	</action>
    </package>
</struts>

Action

public class GetChartAction extends ActionSupport implements Serializable{
	private JFreeChart chart;

	public String execute() throws Exception {
		ValueAxis xAxis = new NumberAxis("年度");
		ValueAxis yAxis = new NumberAxis("产值");
		XYSeries xySeries = new XYSeries("绿豆");
		xySeries.add(0,300);
		xySeries.add(1,200);
		xySeries.add(2,400);
		xySeries.add(3,500);
		xySeries.add(4,600);
		xySeries.add(5,500);
		xySeries.add(6,800);
		xySeries.add(7,1000);
		xySeries.add(8,1100);
		XYSeriesCollection xyDataset = new XYSeriesCollection(xySeries);
		XYPlot xyPlot = new XYPlot(xyDataset,xAxis,yAxis,new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES_AND_LINES));
		chart = new JFreeChart(xyPlot);
		return SUCCESS;
	}

	public JFreeChart getChart() {
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}
}

显示的页面

<body>
    <s:url action="myChart" var="url"></s:url>
    <img alt="图表" src="<s:property value="#url"/>">
</body>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值