使用 Struts2 在网页中显示 JFreeChart
我使用的是Struts2。我需要在网页中显示 JFreeChart。有人可以帮助我吗?
编辑:它以二进制格式显示。
public String execute() throws Exception {
System.out.println("Refresh bar Chart");
response.setContentType("image/png");
OutputStream outstream = response.getOutputStream();
try {
JFreeChart chart = getChartViewer();
ChartUtilities.writeChartAsPNG(outstream, chart, 500, 300);
System.out.println("Created bar Chart");
return SUCCESS;
} finally {
outstream.close();
response.flushBuffer();
}
}
I am using Struts2. I need to display JFreeChart in a web page. Can any body help me on that?
Edit: it is getting displayed in binary format.
public String execute() throws Exception {
System.out.println("Refresh bar Chart");
response.setContentType("image/png");
OutputStream outstream = response.getOutputStream();
try {
JFreeChart chart = getChartViewer();
ChartUtilities.writeChartAsPNG(outstream, chart, 500, 300);
System.out.println("Created bar Chart");
return SUCCESS;
} finally {
outstream.close();
response.flushBuffer();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将图表转换为图像并将其包含在 HTML 文件中。
You can convert your charts to images and include them in your HTML files.
JSP/Struts2/whatever-MVC 页面总是以 HTML 形式结束。要以 HTML 格式显示图像,您需要
元素或通常用于呈现 HTML
元素的任何 MVC 组件。要定位图像,您需要让其
src
属性指向返回图像的有效 URL。这可以是静态或动态资源,客户端看不到差异。在 JSP/Servlet 环境中,通常的做法是让 Servlet 使用
web.xml
中的url-pattern
监听特定 URL,例如/images/ *
并让它根据请求参数或路径信息创建/加载/查找图像,并将图像沿着一组正确的响应标头写入响应的输出流。您可以在此处找到一个基本示例。
JSP/Struts2/whatever-MVC pages always ends up as HTML. To display images in HTML, you need the
<img>
element or whatever MVC component you'd normally to use to render a HTML<img>
element. To locate an image, you need to let itssrc
attribute point to a valid URL which returns the image. This can be a static or dynamic resource, the client doesn't see the difference.In a JSP/Servlet environment, the normal practice is to let a Servlet listen on the particular URL using
url-pattern
inweb.xml
such as/images/*
and let it create/load/find the image based on request parameters or pathinfo and write the image to the outputstream of the response along a correct set of response headers.You can find a basic example here.
有一个插件:
http://struts.apache.org/2.x/docs/ jfreechart-plugin.html
There is a plugin:
http://struts.apache.org/2.x/docs/jfreechart-plugin.html