<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                SpringMVC 中視圖的頂級接口為`View`,其頂級父類為`AbstractView`,我們可以實現`View`,或者繼承`AbstractView`,或者繼承`AbstractView`的子類來實現自定義視圖。 :-: ![](https://img.kancloud.cn/f0/38/f0387181ed96ffdc1603a00d92f87498_1624x442.jpg) `AbstractView`部分子類 下面通過繼承`AbstarctPdfView`來自定義pdf視圖。步驟如下: **1. 引入itext依賴** ```xml <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> <scope>compile</scope> </dependency> ``` **2. 創建實體層** ```java @Data @AllArgsConstructor public class Student { private String name; private Integer age; private String gender; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date born; } ``` **3. 繼承AbstractPdfView類創建對應的pdf視圖** ```java public class StudentPdfView extends AbstractPdfView { @Override protected void buildPdfDocument(Map<String, Object> map, Document document, PdfWriter pdfWriter , HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { PdfPTable table = new PdfPTable(4); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); table.getDefaultCell().setBackgroundColor(Color.lightGray); // 設置字體否則對中文不顯示,在這里我使用本地的《楷體 常規》字體 BaseFont baseFont = BaseFont.createFont("c:/Windows/Fonts/simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font font = new Font(baseFont); PdfPCell pdfPCell = new PdfPCell(); pdfPCell.setPhrase(new Paragraph("名字", font)); table.addCell(pdfPCell); pdfPCell.setPhrase(new Paragraph("年齡", font)); table.addCell(pdfPCell); pdfPCell.setPhrase(new Paragraph("性別", font)); table.addCell(pdfPCell); pdfPCell.setPhrase(new Paragraph("生日", font)); table.addCell(pdfPCell); List<Student> studentList = (List<Student>) map.get("students"); for (Student student : studentList) { pdfPCell.setPhrase(new Paragraph(student.getName(), font)); table.addCell(pdfPCell); pdfPCell.setPhrase(new Paragraph(student.getAge() + "", font)); table.addCell(pdfPCell); pdfPCell.setPhrase(new Paragraph(student.getGender(), font)); table.addCell(pdfPCell); pdfPCell.setPhrase(new Paragraph(student.getBorn().toString(), font)); table.addCell(pdfPCell); } document.add(table); } } ``` **4. controller層** ```java @Controller public class StudentController { //請求地址必須以.pdf后綴 @GetMapping("/v7/pdf/student.pdf") public ModelAndView studentPdf() throws Exception { List<Student> studentList = new ArrayList<Student>(); studentList.add(new Student("張三", 25, "男", new Date())); studentList.add(new Student("李四", 29, "男", new Date())); Map<String, Object> map = new HashMap<String, Object>(1); map.put("students", studentList); StudentPdfView view = new StudentPdfView(); return new ModelAndView(view, map); } } ``` **5. 測試** 啟動項目后訪問 http://localhost:8080/mvc/v7/pdf/student.pdf ,將會看到如下效果。 ![](https://img.kancloud.cn/f2/8b/f28baa9ce72ab5196da8d20c774d4225_2560x527.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看