锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
I am rendering a chart to the output stream. Instead of streaming it directly to the response.out, I create a ChartResult , and let webwork do the chaining for me.
I generate the chart in one class, and I render it out in another class, efftectively decoupling the view from the actions. You can easily render it out to file or some view other than a web response.out if you wish.
Configuration
xwork.xml -result-types definitions
<result-types>
聽聽 <result-type name="chart" class="myapp.webwork.extensions.chartResult"/>
</result-types>
xwork.xml -action definitions
<action name="viewModerationChart" class="myapp.webwork.actions.ViewModerationChartAction">
聽聽 <result name="success" type="chart">
聽聽聽聽聽 <param name="width">400</param>
聽聽聽聽聽 <param name="height">300</param>
聽聽 </result>
</action>
Source Codes
My result class search for a "chart" in the ValueStack and renders it out...
public class ChartResult implements聽 Result{
聽聽 private int width;
聽聽 private int height;
聽聽 public void execute(ActionInvocation invocation)throws Exception{
聽聽聽聽聽聽 JFreeChart chart = (JFreeChart)invocation.getStack().findValue("chart");
聽聽聽聽聽聽 HttpServeletResponse response = ServeletActionContext.getResponse();
聽聽聽聽聽聽 OutputStream os = response.getOutputStream();
聽聽聽聽聽聽 ChartUtilities.writeChartAsPNG(os,chart,width,height);
聽聽聽聽聽聽 os.flush();
聽聽 }
聽聽聽聽聽
聽聽 public void setHeight(int height){
聽聽聽聽 this.height=height;
聽聽 }
聽聽
聽聽 public void setWidth(int width){
聽聽聽聽 this.width=width;
聽聽 }
}
public class ViewModerationChartAction extends ActionSupport{
聽聽 private JFreeChart chart;
聽聽
聽聽 public String execute() throws Exception{
聽聽聽聽聽 //chart creation logic ...
聽聽聽聽聽 XYSeries dataSeries = new XYSeries(new Integer(1));
聽聽聽聽聽 for(int i=0;i<=100;i++){
聽聽聽聽聽聽聽聽 dataSeries.add(i,RandomUtils.nextInt());
聽聽聽聽聽 }
聽聽聽聽聽
聽聽聽聽聽 XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);
聽聽聽聽聽 ValueAxis xAxis = new NumberAxis("Raw Marks");
聽聽聽聽聽 ValueAxis yAxis = new NumberXxis("Moderated Marks");
聽聽聽聽聽
聽聽聽聽聽 chart = new JFreeChart(
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 "Moderation Function",
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 JFreeChart.DEFAULT_TITLE_FONT,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 new XYPlot(
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 xyDataset,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 xAxis,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 yXis,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 new
聽聽聽聽聽聽聽聽聽聽 StandardXYItemRenderer(StandardXYItemRender.LINES),
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 false聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 );
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 ...
聽聽聽聽聽 )
聽聽 }
聽聽
聽聽 public JFreeChart getChart(){
聽聽聽聽聽 return chart;
聽聽 }
}