我是在struts框架里面做的,不知道jsp页面里面为什么没生成连接,请大家帮帮忙。
action代码如下。
public final class ViewProcessOrderAction extends Action{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
DynaActionForm searchForm = (DynaActionForm) form;
String keyword = (String)searchForm.get("keyword");
Integer pageId = (Integer)searchForm.get("pageId");
HttpSession session = request.getSession(true);
List processOrdertList=new DBOperate().getMatchProcessOrders(keyword);
if (processOrdertList.size()==0){
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionError("errors.noMatch"));
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
return mapping.findForward("toWrong");
}
else{
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter(); DefaultPieDataset data = new DefaultPieDataset();
for(int i=0;i<processOrdertList.size();i++){
Order order = (Order)processOrdertList.get(i);
data.setValue(order.getTime(), order.getPrice());
} PiePlot3D plot = new PiePlot3D(data);//3D饼图
plot.setURLGenerator(new StandardPieURLGenerator("barview.jsp"));//设定链接
JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(java.awt.Color.white);//可选,设置图片背景色
chart.setTitle("订单调查表");//可选,设置图片标题
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
PrintWriter w = new PrintWriter(out);//输出MAP信息
//500是图片长度,300是图片高度
ChartUtilities.writeImageMap(w, "map0", info, false);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session); String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; request.setAttribute("filename", filename);
request.setAttribute("graphURL", graphURL);
ViewList viewList=new ViewList();
viewList.display (processOrdertList,session,pageId,keyword) ;
return (mapping.findForward("toListProcessOrder"));
}
}
}jsp代码如下 <P ALIGN="CENTER">
<%Object graphURL=request.getAttribute("graphURL"); %>
<%Object filename=request.getAttribute("filename"); %>
<img src="<%= graphURL.toString() %>" width=500 height=300 border=0 usemap="#map0">
<%= graphURL.toString()%>
</P>
先谢谢啦
action代码如下。
public final class ViewProcessOrderAction extends Action{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
DynaActionForm searchForm = (DynaActionForm) form;
String keyword = (String)searchForm.get("keyword");
Integer pageId = (Integer)searchForm.get("pageId");
HttpSession session = request.getSession(true);
List processOrdertList=new DBOperate().getMatchProcessOrders(keyword);
if (processOrdertList.size()==0){
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionError("errors.noMatch"));
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
return mapping.findForward("toWrong");
}
else{
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter(); DefaultPieDataset data = new DefaultPieDataset();
for(int i=0;i<processOrdertList.size();i++){
Order order = (Order)processOrdertList.get(i);
data.setValue(order.getTime(), order.getPrice());
} PiePlot3D plot = new PiePlot3D(data);//3D饼图
plot.setURLGenerator(new StandardPieURLGenerator("barview.jsp"));//设定链接
JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(java.awt.Color.white);//可选,设置图片背景色
chart.setTitle("订单调查表");//可选,设置图片标题
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
PrintWriter w = new PrintWriter(out);//输出MAP信息
//500是图片长度,300是图片高度
ChartUtilities.writeImageMap(w, "map0", info, false);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session); String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; request.setAttribute("filename", filename);
request.setAttribute("graphURL", graphURL);
ViewList viewList=new ViewList();
viewList.display (processOrdertList,session,pageId,keyword) ;
return (mapping.findForward("toListProcessOrder"));
}
}
}jsp代码如下 <P ALIGN="CENTER">
<%Object graphURL=request.getAttribute("graphURL"); %>
<%Object filename=request.getAttribute("filename"); %>
<img src="<%= graphURL.toString() %>" width=500 height=300 border=0 usemap="#map0">
<%= graphURL.toString()%>
</P>
先谢谢啦
false改为true试试
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session);
/*------使用printWriter将文件写出----*/
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
看到我的了吗?在writeImageMap后面是有这么一句“pw.flush()"
如果不加我的可以现实图片,但是加了,用mapping最后转发到Jsp页面就会出错,页面一片空白,你也可是试试有没有其他朋友遇到这个问题啊?请教请教
ChartUtilities.writeImageMap(w, "map0", info, false);String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session); String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; 个人觉得标红的两部分冲突,你可以改成String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
ChartUtilities.writeImageMap(w, filename, info, false);
//注意此句的位置
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
严重: Servlet.service() for servlet action threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
pw.flush(); 这样生成的图片就有热区链接
但是在struts里,PrintWriter out = response.getWriter();
然后同样的代码ChartUtilities.writeImageMap(pw, filename, info, true); pw参数是由上面那个out传过来的
pw.flush(); 这种情况就不能生成热区链接,页面转发后是个空白不过我查看了下源文件,可以看到生成map的那段代码,但是没有其他的东西综上所述还是输出流的问题,你再想想问什么?
---------------------------------------
你试试在jsp页面里面调用,肯定可以生成热区链接的我再搞搞,你也好好思考下!
呵呵,我回去也好好看看
”如果java程序都写在jsp页面就能在图片上生成链接“
这句话我纠正一下,不是所有程序都写在jsp页面,只是调用方法的那句话写在jsp页面,生成图片的方法还是封装在类文件中还有你说的那个异常,你把pw.flush()放在try块,捕获一下,就行了
靠,csdn有没有高手啊,也不露个面,对这个论坛很失望!
因为生成的map在输出的时候,编码用的是utf-8,生成map的时候不需要加任何的转换,然后你会看到热区上的带中文的参数都是一堆百分号,这个没事,接下来,你就要用request来获取这个参数,再次强调用一下,如果你项目统一用的是utf-8,那么你获取之后的值应该是正确的我的没有设置,默认的字符集是iso-8859-1,所以需要把iso-8859-1再转成utf-8
只需按下面的就ok了
String pars = request.getParameter("map中传过来的带百分号的中文参数");
par = new String(pars.getBytes("iso-8859-1"),"utf-8");par的值就变成正确的值了,呵呵,你试试
虽然所有问题都搞定了,但是放在struts调用生成图表,那个输出流和map问题还是个迷,你要是搞好了,告诉我下哦!
request.setAttribute("map1", imageMap);//无法用输出流来返回请求然后在界面中用此方式接受 ${map1}就可以。