Spring MVC和JFreeChart(在视图中显示图表)

时间:2012-06-20 10:55:17

标签: spring-mvc jfreechart

在我的Spring web MVC应用程序中,我打算在我的jsp视图上显示一些JFree图表,我不知道该怎么做, 因为第一个想法我没有为我工作,生成一个图像,然后从文件夹中检索它。

现在我想,控制器是否能够直接回溯图像? 假设这是我的服务界面:

public interface ReportingService {
    Image getChart() ;
}

我可以做这样的事情吗?如何从我的jsp中调用这个图像?

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

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

2 个答案:

答案 0 :(得分: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" />

中形成

它将动态创建和呈现图表。

答案 1 :(得分:0)

我在我的项目中广泛使用了Jfreechart。我使用CEWOLF框架将它们放在JSP中。 http://cewolf.sourceforge.net/new/index.html这是一个开源框架,它提供直接的JSP标记以将图表放入页面

如果您有许多图表可供使用,那么使用此框架是值得的 对于您的查询,据我所知,您所引用的图像必须是JFreechart图像而不是Java applet图像。你必须使用字节流转换来转换它。