JFreeChart Struts2组件报IllegalStateException

本文探讨了在Struts2+Spring+Hibernate(S2SH)框架中使用JFreeChart组件生成图表时遇到的IllegalStateException异常。通过调整ChartResult.java源文件和struts2-dispatcher的异常处理逻辑,最终解决了Servlet.service()方法抛出异常的问题。

在s2sh框架中应用了JFreeChart 组件,使用了struts2-jfreechart -plugin,jfreechart 生 成图片的时候在服务器后台会产生如下的错误,

但是图片却能够正常的显示出来。

严重: Servlet.service() for servlet default threw exception java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407) at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:770) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:505) at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:574) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2018) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:651) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:676) at java.lang.Thread.run(Thread.java:595)

这个问题困扰了我很久,当请求JFreeChart 图片时突然中断或者第一次请求图片还没完 成就发生第二次请求时就会抛出这个异常。
网上解释说是产生图片过程中发生了io异常,修改struts plugins包里ChartResult.java 源文件。

... HttpServletResponse response = ServletActionContext.getResponse(); //OutputStream os = response.getOutputStream();//原来是这么写的 OutputStream os = null; try { os = response.getOutputStream();//挪到try里面 // check the type to see what kind of output we have to produce if ("png".equalsIgnoreCase(type)) { response.setContentType("image/png"); ChartUtilities.writeChartAsPNG(os, chart, getIntValueFromString(width), getIntValueFromString(height)); } else if ("jpg".equalsIgnoreCase(type) || "jpeg".equalsIgnoreCase(type)) { response.setContentType("image/jpg"); } else throw new IllegalArgumentException(type + " is not a supported render type (only JPG and PNG are)."); } catch (IOException e) {//加上异常捕获 } finally { if (os != null) os.flush(); } ...

经测试问题依旧存在 ,查看struts2源码采用如下办法解决,在 org.apache.struts2.dispatcher.Dispatcher 770行左右:

try { // WW-1977: Only put errors in the request when code is a 500 error if (code == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) { // send a http error response to use the servlet defined error handler // make the exception availible to the web.xml defined error page request.setAttribute("javax.servlet.error.exception", e); // for compatibility request.setAttribute("javax.servlet.jsp.jspException", e); } // send the error response response.sendError(code, e.getMessage()); } catch (IOException e1) { // we're already sending an error, not much else we can do if more stuff breaks } catch (IllegalStateException e2) { //添加IllegalStateException 异常捕捉 }

就哦了。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值