Tambahkan dependency
XML
Copy
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>io.github.jpenren</groupId>
<artifactId>thymeleaf-spring-data-dialect</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10.1</version>
</dependency>
Membuat file .jrxml menggunakan Jaspersoft Studio
Copy file ke resource/reports
tambahkan beanΒ JasperReportsViewResolverΒ ke konfigurasiweb
Java
Copy
@Bean
public JasperReportsViewResolver getJasperReportsViewResolver() {
JasperReportsViewResolver resolver = new JasperReportsViewResolver();
resolver.setPrefix("classpath:/reports/");
resolver.setSuffix(".jrxml");
resolver.setViewNames("report_*");
resolver.setViewClass(JasperReportsMultiFormatView.class);
resolver.setOrder(0);
return resolver;
}
buat controllernya
Java
Copy
@Controller
public class BugReportController {
@Autowired private BugDao dao;
@RequestMapping("/bug")
public ModelAndView generateAllBugReport(ModelAndView m, @RequestParam(value = "format", required = false) String format) {
Iterable < Bug > data = dao.findAll();
m.addObject("dataSource", data);
m.addObject("tanggalUpdate", new Date());
m.addObject("format", "pdf");
if (format != null && !format.isEmpty()) {
m.addObject("format", format);
}
m.setViewName("report_bug");
return m;
}
}