2012-06-20 63 views
0

在我的Spring Web MVC應用程序中,我打算在jsp視圖上顯示一些JFree圖表,我不知道如何執行此操作, 自第一個想法i沒有爲我工作,生成一個圖像,然後從文件夾中檢索它。Spring MVC和JFreeChart(在視圖中顯示圖表)

現在我想,是控制器能夠直接重畫圖像嗎? 想這是我的服務接口:

public interface ReportingService { 
    Image getChart() ; 
} 

我可以做這樣的事情,我怎麼能叫這個形象從我的jsp?

@Controller 
public class ReportingController { 
    @Autowired 
    private ReportingService reportingService ; 

    @RequestMapping("/reports") 
    public Image handleReports(){ 
     return reportingService.getChart() ; 
    } 
} 

回答

0

我在項目中廣泛使用了Jfreechart。我使用CEWOLF框架將它們放入JSP中。 http://cewolf.sourceforge.net/new/index.html這是一個開源框架,它提供了直接的JSP標籤來把圖表放在頁面中

如果你有很多圖表可以使用,它的價值在於使用這個框架。
對於您的查詢,據我記住,您所指的圖片必須是JFreechart圖片而不是java applet圖片。你將不得不使用字節流轉換來轉換它。

+0

cewolf很有趣,我只是需要一個如何在基於Spring MVC的應用程序中使用它的例子。 – elaich

1

,你可以嘗試這個

@RequestMapping("/reports") 

public void handleReports(HttpServletResponse response){ 

response.setContentType("image/jpeg"); 

OutputStream out = response.getOutputStream(); 

writeChartAsJPEG(out , reportingService.getChart() , 400, 400); 

} 

現在,形式JSP頁面<img src="/reports" />

它將創建和呈現上飛圖。