가장 먼저 해야할것은 Vo부터 선언해야합니다.
@Data
public class TestVo {
private String test_date;
private String name;
private String status;
private String no1;
private String no2;
private String memo;
}
그 이후 Controller부분 입니다.
@Component("testReport")
@SuppressWarnings("unchecked")
public class TestReport extends AbstractView {
@Autowired
TestReportService testReportService;
@Override
protected void testModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws BizException, Exception {
ServletOutputStream os = null;
try {
log.debug(">>>레포트 생성 START");
// jasper 위치
InputStream inputStream = new ClassPathResource("report/test.jasper").getInputStream();
List<TestVo> testList = testReportService.getTestInfo(model);
// allParams
Map<String, Object> allParams = new HashMap<>();
allParams.put("list", testList);
// data source
JRDataSource dataSource = testList.size() > 0 ? getDataSource(testList) : new JREmptyDataSource(1);
log.debug(">>>>>>>>>> fill report START");
JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, allParams, dataSource);
log.debug(">>>>>>>>>> fill report END");
JRPdfExporter exporter = new JRPdfExporter(DefaultJasperReportsContext.getInstance());
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
os = response.getOutputStream();
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(os));
response.setContentType("application/pdf");
String fileName = URLEncoder.encode("테스트", "UTF-8").replace("+", "%20");
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setMetadataTitle("테스트");
response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + ".pdf\"");
configuration.setPdfVersion(PdfVersionEnum.VERSION_1_7);
exporter.setConfiguration(configuration);
log.debug(">>>>>>>>>> export pdf START");
exporter.exportReport();
log.debug(">>>>>>>>>> export pdf END");
} catch(BizException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST); // 400 오류
log.error(e.getMessage(), e);
} catch(NullPointerException | IOException e) {
log.error(e.getMessage(), e);
} catch(Exception e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
log.error(e.getMessage(), e);
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch(IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
private JRDataSource getDataSource(List<TestVo> rates) {
return new JRBeanCollectionDataSource(rates);
}
}
Service 부분 입니다.
public List<TestVo> getTestInfo(Map<String, Object> paraMap) {
return testReportMapper.slctTestInfo(paraMap);
}
Mapper 부분 입니다.
public List<TestVo> slctTestInfo(Map<String, Object> paraMap);
xml 부분에서는 resultType만 Vo로 해주면 됩니다.
<select id="slctTestInfo" parameterType="hashmap" resultType="com.kadra.asms.report.web.vo.CertCancelVo">
</select>
'개발자 > Java' 카테고리의 다른 글
| 일반적 For문과 향상된 For문 (1) | 2025.06.13 |
|---|---|
| [Java] Web에서 서버에 저장된 PDF 보여주기 OR PDF 다운로드 (1) | 2025.01.16 |
| POI를 이용한 대용량 엑셀 다운로드(SXSSF 방식 + sqlSessionFactory + ResultHandler + VO 활용) (1) | 2024.10.18 |
| [Java] 숫자, 금액을 한글로 변환 (0) | 2024.06.26 |
| [Java] Collections.frequency() 사용법 (0) | 2024.06.13 |