Jasperreport之与Springboot整合方式2

环境:

jdk1.8

Jaspersoft Studio 6.5.0

Jasperreport 6.5.0

Springboot 1.5.x

 

pom.xml

<!-- jasperreports-->
<dependency>
  <groupId>net.sf.jasperreports</groupId>
  <artifactId>jasperreports</artifactId>
  <version>6.5.0</version>
</dependency>
<dependency>
  <groupId>net.sf.jasperreports</groupId>
  <artifactId>jasperreports-fonts</artifactId>
  <version>6.0.0</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>itextpdf</artifactId>
  <version>5.5.0</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>itext-pdfa</artifactId>
  <version>5.5.0</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>itext-asian</artifactId>
  <version>5.2.0</version>
</dependency>
<dependency>
  <groupId>cn.lesper</groupId>
  <artifactId>iTextAsian</artifactId>
  <version>3.0</version>
</dependency>
<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>2.4.11</version>
</dependency>
<!-- jasperreports end-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
</dependency>

 

 

增加一个JasperReportConfig

package com.zns.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView;
import org.springframework.web.servlet.view.jasperreports.JasperReportsViewResolver;

@Configuration
@EnableWebMvc
public class JasperReportConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public JasperReportsViewResolver getJasperReportsViewResolver(){
        JasperReportsViewResolver resolver = new JasperReportsViewResolver();
        resolver.setPrefix("classpath:jasperreport/"); //src/main/resources/jasperreport,将jrxml放入该目录
        resolver.setSuffix(".jrxml");
        resolver.setReportDataKey("datasource");
        resolver.setViewClass(JasperReportsMultiFormatView.class);
        resolver.setOrder(0);
        return resolver;
    }
}

 

 

控制器部分代码

public List<Product> getProductList() {
    List<Product> list = new ArrayList<>();
    for (int i = 1; i <= 20; i++) {
        list.add(new Product(String.valueOf(i), "名称" + i));
    }
    return list;
}

@RequestMapping(path = "/pdfPreview", method = RequestMethod.GET)
public ModelAndView pdfPreview(final ModelMap modelMap) {
    JRDataSource dataSource = new JRBeanCollectionDataSource(getProductList());
    modelMap.put("format", "pdf");
    modelMap.put("datasource", dataSource);
    modelMap.put("Parameter1", "hello 你好");
    return new ModelAndView("test", modelMap);
}

 

上一篇:JasperReports首次使用的坑


下一篇:JasperReport中文打印问题