从控制器生成图表

发布于 2024-10-24 21:51:57 字数 1017 浏览 2 评论 0原文

我使用spring 3.0,在jsp中,我尝试显示数据和图表...

@Controller
@RequestMapping("/user.htm")
public class UserController {

@Autowired
private IUserService userService;

@RequestMapping(method = RequestMethod.GET)
public ModelAndView user(HttpServletRequest request,
    HttpServletResponse response) {

    ModelAndView modelAndView = new ModelAndView("user");
    modelAndView.addObject("statUser", userService.getStatUser());

    return modelAndView;
}

public void generateChart(HttpServletRequest request,
    HttpServletResponse response){
    try {
        AxisChart axisChart = userService.generateChart();
        ServletEncoderHelper.encodeJPEG13( axisChart, 1.0f, response );
    } catch (ChartDataException ex) {

    } catch (PropertyException ex) {

    } catch (IOException ex) {

    }

  }

}

在jsp中我尝试显示图表,

<img src="generateChart"/>

我可以看到信息...所以控制器的获取工作正常,但是图像从未显示,

所以我不知道我是否可以使用相同的控制器,或者需要创建一个新控制器仅用于创建图像......

有什么想法吗?

i use spring 3.0, in a jsp, i try to display data and a chart...

@Controller
@RequestMapping("/user.htm")
public class UserController {

@Autowired
private IUserService userService;

@RequestMapping(method = RequestMethod.GET)
public ModelAndView user(HttpServletRequest request,
    HttpServletResponse response) {

    ModelAndView modelAndView = new ModelAndView("user");
    modelAndView.addObject("statUser", userService.getStatUser());

    return modelAndView;
}

public void generateChart(HttpServletRequest request,
    HttpServletResponse response){
    try {
        AxisChart axisChart = userService.generateChart();
        ServletEncoderHelper.encodeJPEG13( axisChart, 1.0f, response );
    } catch (ChartDataException ex) {

    } catch (PropertyException ex) {

    } catch (IOException ex) {

    }

  }

}

in the jsp i try to display the chart with

<img src="generateChart"/>

i can see the information... so the get of the controller work fine, but the image is never displayed

so i don't know if i can use the same controller or need to create a new one only for creation of the image...

any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三寸金莲 2024-10-31 21:51:57

我用 Jfreechart 做了一些类似的事情,我最终创建了另一个图表控制器,其映射类似于 @RequestMapping("/chart.jpg") ,然后通过 。在控制器中,您必须设置 response.setHeader("Content-Type", "image/jpg");

编辑:
这是我的一个例子(它是groovy格式的,是png的,但应该有帮助)

@RequestMapping("/chart.png")
def chart(HttpServletRequest request, HttpServletResponse response){
    JFreeChart chart = ChartFactory.createTimeSeriesChart(...)
    //fill & layout the chart
    def pngChart = EncoderUtil.encode(chart.createBufferedImage(600, 400), "png")
    response.setContentType("image/png");
    response.setContentLength(pngChart.length);
    response.getOutputStream().write(pngChart);
    response.getOutputStream().close();
}

I've done something similer with Jfreechart, I ended up creating another chart controller with a mapping like @RequestMapping("/chart.jpg") and then link to it via <img src="cart.jpg"/>. In the controller then you have to set response.setHeader("Content-Type", "image/jpg");.

Edit:
Here a example of mine (it's in groovy and it's a png, but it should help)

@RequestMapping("/chart.png")
def chart(HttpServletRequest request, HttpServletResponse response){
    JFreeChart chart = ChartFactory.createTimeSeriesChart(...)
    //fill & layout the chart
    def pngChart = EncoderUtil.encode(chart.createBufferedImage(600, 400), "png")
    response.setContentType("image/png");
    response.setContentLength(pngChart.length);
    response.getOutputStream().write(pngChart);
    response.getOutputStream().close();
}
毁梦 2024-10-31 21:51:57

我正在为多个报告处理程序使用单一界面。
ModelAndView 始终是预期的结果。
我认为这是一般情况,当您需要处理不同的报告并以不同格式返回数据时。

因此,我通过扩展 AbstractView 解决了问题。
注意:代码示例从磁盘加载文件。
您必须使用自己的图像生成器和模型中表示的数据创建字节数组输出流。

创建 ModelAndView 的示例:

ModelAndView result = new ModelAndView();
result.setView(new ImageView());
result.addObject("chart_data_param1", "1");
result.addObject("chart_data_param2", "2");
result.addObject("chart_data_param3", "3");

View 类的实现:

public class ImageView extends AbstractView {

        public ImageView() {
            setContentType("image/png");
        }

        @Override
        protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
            ByteArrayOutputStream baos = createTemporaryOutputStream();
            byte[] img = FileUtils.readFileToByteArray(new File("<some-file-path>"));
            baos.write(img, 0, img.length);
            baos.close();
            writeToResponse(response, baos);
        }
    }

I am using the single interface for several report handlers.
ModelAndView is always expected as a result.
I think this is generic situation, when you need to process different reports and return data in different formats.

So, I resolved problem by extending AbstractView.
Note: Code example loads file from the disk.
You have to create byte array output stream using your own image generator and data represented in the model.

Example creating ModelAndView:

ModelAndView result = new ModelAndView();
result.setView(new ImageView());
result.addObject("chart_data_param1", "1");
result.addObject("chart_data_param2", "2");
result.addObject("chart_data_param3", "3");

Implementation of the View class:

public class ImageView extends AbstractView {

        public ImageView() {
            setContentType("image/png");
        }

        @Override
        protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
            ByteArrayOutputStream baos = createTemporaryOutputStream();
            byte[] img = FileUtils.readFileToByteArray(new File("<some-file-path>"));
            baos.write(img, 0, img.length);
            baos.close();
            writeToResponse(response, baos);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文