I would like to have a controller method that returns a PDF from the Jrxml of a JasperReports file without using any xml configuration.
I would like to use JasperReportsPdfView. Is it even possible? I know that this can only be done with Java code, as in this blog:
http://krams915.blogspot.com/2010/12/spring-3-mvc-jasper-integration_22.html
But I believe this is possible with less code :-)
Here is an example of sample code that does not .
@RequestMapping(value = "/test/pdfreport", method = RequestMethod.GET, produces = "application/pdf")
public JasperReportsPdfView getPdf() {
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;
}
Thanks for any pointer.
Edit: 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);
}
It is important to include the package spring-context-supportin your project.
source
share