Spring MVC @Bean("simpleView") public View myCustomView() { return new MyCustomView(); }
Original Post
import org.springframework.web.servlet.View;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; import java.util.Map;
public class MyCustomView implements View {
@Override public String getContentType() { return "text/html"; }
@Override public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType(getContentType()); PrintWriter writer = response.getWriter(); writer.println("This is my custom dummy view.<br/>"); writer.println("<h3>Model attributes</h3>"); for (Map.Entry<String, ?> entry : model.entrySet()) { writer.printf("%s = %s<br/>", entry.getKey(), entry.getValue()); } } }
Original Post
import com.itextpdf.io.font.FontConstants; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; import org.springframework.stereotype.Component; import org.springframework.web.servlet.view.AbstractView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.Map;
@Component("reportView") public class ReportPdfView extends AbstractView {
@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Content-Disposition", "attachment; filename=myReport.pdf"); Report report = (Report) model.get("report");
//IText API PdfWriter pdfWriter = new PdfWriter(response.getOutputStream()); PdfDocument pdf = new PdfDocument(pdfWriter); Document pdfDocument = new Document(pdf);
//title Paragraph title = new Paragraph(report.getName()); title.setFont(PdfFontFactory.createFont(FontConstants.HELVETICA)); title.setFontSize(18f); title.setItalic(); pdfDocument.add(title);
//date Paragraph date = new Paragraph(report.getDate().format( DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL))); date.setFontSize(16f); pdfDocument.add(date);
//content Paragraph content = new Paragraph(report.getContent()); pdfDocument.add(content);
pdfDocument.close(); } }
Original Post
import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.export.HtmlExporter; import net.sf.jasperreports.export.SimpleExporterInput; import net.sf.jasperreports.export.SimpleHtmlExporterOutput; import org.springframework.stereotype.Component; import org.springframework.web.servlet.view.AbstractView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.util.List; import java.util.Map;
@Component("forexView") public class CurrencyRateHtmlView extends AbstractView { private JasperReport currencyRatesReport;
@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("text/html"); List<CurrencyRate> rates = (List<CurrencyRate>) model.get("todayCurrencyRates"); //data source JRDataSource dataSource = getDataSource(rates); //compile jrxml template and get report JasperReport report = getReport(); //fill the report with data source objects JasperPrint jasperPrint = JasperFillManager.fillReport(report, null, dataSource); //export to html HtmlExporter exporter = new HtmlExporter(DefaultJasperReportsContext.getInstance()); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getWriter())); exporter.exportReport(); }
private JRDataSource getDataSource(List<CurrencyRate> rates) { JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(rates); return dataSource; }
public JasperReport getReport() throws JRException { if (currencyRatesReport == null) {//compile only once lazily InputStream stream = getClass().getResourceAsStream("/rates.jrxml"); currencyRatesReport = JasperCompileManager.compileReport(stream); } return currencyRatesReport; } }
Original Post
@Bean("currencyRateView") public View jsonView() { MappingJackson2JsonView view = new MappingJackson2JsonView(); return view; }
Original Post
|
|