我正在尝试根据用户输入生成图表。我在 Struts 2 框架中使用 JFree Charts。在图表生成动作类中,我无法实现这个ModelDriven
概念;我也无法从HttpServletRequest
对象中检索参数值。.
ModelDriven
如果我用实现or调用图表生成操作类ServletRequestAware
,它可以正常工作,但是它会在下一页显示图表。我需要根据用户输入生成图表。
我没有成功搜索有关 JFree an Struts 2 的信息;任何有用的教程链接也将不胜感激。
这是我的 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>
<package name="jfree" extends="jfreechart-default">
<action name="chartAction" class="com.kogent.action.ChartAction">
<result name="success" type="chart">
<param name="width">500</param>
<param name="height">300</param>
</result>
</action>
</package>
</struts>
这是我的动作课
package com.kogent.action;
import java.util.Random;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class ChartAction extends ActionSupport implements ModelDriven<FormBean>,Preparable{
private JFreeChart chart;
private FormBean bean;
@Override
public FormBean getModel() {
// TODO Auto-generated method stub
return bean;
}
@Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
bean=new FormBean();
}
public String execute() throws Exception {
// chart creation logic...
System.out.print(bean.getCategory()+" "+bean.getChartType());
//if remove this above line my action runs fine but i require this vales from the user
XYSeries dataSeries = new XYSeries(new Integer(1));
for (int i = 0; i <= 100; i++) {
dataSeries.add(i, new Random().nextInt(50));
}
XYSeriesCollection xyDataSet = new XYSeriesCollection(dataSeries);
ValueAxis xAxis = new NumberAxis("Marks");
ValueAxis yAxis = new NumberAxis("Age");
chart =
new JFreeChart("Chart Title", JFreeChart.DEFAULT_TITLE_FONT,
new XYPlot(xyDataSet,xAxis, yAxis,
new StandardXYItemRenderer(StandardXYItemRenderer.LINES)),
false);
chart.setBackgroundPaint(java.awt.Color.white);
return super.SUCCESS;
}
public JFreeChart getChart() {
return chart;
}
}