【问题标题】:Unable to implement ModelDriven or ServletRequestAware in Struts 2 Action Class using JFree无法使用 JFree 在 Struts 2 Action Class 中实现 ModelDriven 或 ServletRequestAware
【发布时间】:2013-02-20 16:45:01
【问题描述】:

我正在尝试根据用户输入生成图表。我在 Struts 2 框架中使用 JFree Charts。在图表生成动作类中,我无法实现ModelDriven 概念;我也无法从HttpServletRequest 对象中检索参数值。 .

如果我通过实现ModelDrivenServletRequestAware 调用图表生成操作类,它可以正常工作,但它会在下一页显示图表。我需要根据用户输入生成图表。

我没有成功搜索有关 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;
    }
}

【问题讨论】:

  • 请为您的操作发布任何相关代码,包括您的 XML。
  • 在我的 Action 类中只是实现 ModelDriven 并且在 struts.xml 文件中,包标签扩展了 jfreechart-default
  • @JoshDM 你能看看我已经发布了 d 鳕鱼

标签: struts2 jfreechart


【解决方案1】:

基于这个 jfree 图表,有很多例子, 我只是给这些链接检查一下。

First: 实现任何 jfree 图表除了servlet request awareservlet response aware 之外什么都不需要,request 用于接收用户的请求,response 用于将输出提供给用户。您希望使用'ModelDriven' interface ( it gains the extra ability to transfer the form data into the object automatically)

只需使用此链接。

Create chart and Display them dynamically in JSP.

Create area chart in JSP page using JFreeChart.

Creating Pie Charts with JFreeChart

Already discussion are there in stack overflow only

This is official site it will help full for you

【讨论】:

  • 创建图表并显示它们不是问题,所有需要的是通过某种形式从用户那里获取价值,然后在我遵循 struts2 mvc 框架的动作类中检索它们,而你的例子都没有显示跟随。
  • K.使用这个链接jfree chart.这里你必须修改,在action类中实现servlet requestaware并用这个代码替换。例如:data.setValue("One", new Double(43.2)); 在这里而不是 doublerequest.getParameter("your string"); 它看起来像 data.setValue("One",request.getParameter("your string")) 它会 100% 工作
猜你喜欢
  • 2012-07-30
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
相关资源
最近更新 更多