如何在没有xml配置的情况下使用JasperReportsPdfView [英] How to use JasperReportsPdfView without xml config

查看:581
本文介绍了如何在没有xml配置的情况下使用JasperReportsPdfView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个控制器方法从JasperReports jrxml文件返回PDF,而不使用任何xml配置。

I would like to have a controller method returning a PDF from a JasperReports jrxml file, without using any xml configuration.

我想使用 JasperReportsPdfView 。这有可能吗?我知道它只能用这个博客中的Java代码来完成:

I would like to use a JasperReportsPdfView. Is this possible at all? I know it can be done with only Java code like in this blog:

http://krams915.blogspot.com/2010/12/spring-3-mvc-jasper-integration_22.html

但我相信一定可以使用更少的代码: - )

But I believe it must be possible with less code :-)

这里有一些示例代码可以做不幸的是

Here some example-code which does not work unfortunately.

@RequestMapping(value = "/test/pdfreport", method = RequestMethod.GET, produces = "application/pdf")
public JasperReportsPdfView getPdf() {

    // does not work like this, unfortunately
    final Person p = userService.getUserById("the id");

    final JasperReportsPdfView view = new JasperReportsPdfView();
    view.setReportDataKey("person");
    view.addStaticAttribute("person", p); // ??
    view.setUrl("report.jrxml");
    return view;
}

感谢任何指针。

编辑:这是我的解决方案:

This is my working solution:

@Autowired 
private ApplicationContext appContext;

@RequestMapping(value = "/test/pdfreport", method = RequestMethod.GET, produces = "application/pdf")
public ModelAndView getPdf() {
    final List<Map<String, Object>> users = userService.getUsers();

    final JasperReportsPdfView view = new JasperReportsPdfView();
    view.setReportDataKey("users");
    view.setUrl("classpath:report.jrxml");
    view.setApplicationContext(appContext);

    final Map<String, Object> params = new HashMap<>();
    params.put("users", users);

    return new ModelAndView(view, params);
}

包含 spring-context-support非常重要打包到你的项目。

推荐答案

这对我有用:

@Autowired private ApplicationContext appContext;
@Autowired private DataSource dataSource;

@RequestMapping(value = "/pdf", method = RequestMethod.GET)
public ModelAndView getPdf() {
    JasperReportsPdfView view = new JasperReportsPdfView();
    view.setJdbcDataSource(dataSource);
    view.setUrl("classpath:report.jrxml");
    Map<String, Object> params = new HashMap<>();
    params.put("param1", "param1 value");
    view.setApplicationContext(appContext);
    return new ModelAndView(view, params);
}

这篇关于如何在没有xml配置的情况下使用JasperReportsPdfView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆