java - JasperReports 与 Maven 和 Eclipse 示例

标签 java maven jasper-reports

我只想使用 JasperReport+Maven+Eclipse 生成一个简单的 pdf 报告。

一段时间以来,我一直在浏览一个简单的示例,但找不到。

  1. JasperReport 站点似乎没有任何 Maven 教程
  2. Maven 2 JasperReports 插件在生命周期配置未涵盖的 Eclipse 插件执行中给出以下错误:org.codehaus.mojo:jasperreports-maven-plugin:1.0- beta-2:编译报告
  3. 其他站点上的大多数示例都是使用 Ant 和 Jasper 的

最佳答案

它是一个获取数据的小报告。

这是你的 Controller

@RequestMapping(value ="/BMIbyage",method = RequestMethod.GET)
public ModelAndView BMIbyage(ModelAndView modelAndView, ModelMap model, Map<String, Object> map,HttpServletRequest request,
        @RequestParam("syear") int syear,@RequestParam("eyear") int eyear,HttpSession session){
    String childid = (String) session.getAttribute("childid");

    List<AdmissionSummery> asum = new ArrayList<AdmissionSummery>();

     List<PatientDetails> patientdetail = patientService.listpatientbmi();

    for(int i=syear;i<=eyear;i++){
         asum.add(new AdmissionSummery());
        int no=0;
        for(int j=0;j<patientdetail.size();j++){


            int dob = Integer.parseInt(patientdetail.get(j).getDateofbirth().substring(0, 4));

            float bmi = patientService.listPatient(patientdetail.get(j).getChildid()).get(0).getBmi();


            if(dob ==i && (bmi<18.5)){
                no=no+1;

                asum.get(asum.size()-1).setNooftime(no);

            }
            System.out.println("no"+no);
            System.out.println("j"+j);
            asum.get(asum.size()-1).setDuration(syear+" "+"-"+" "+eyear);
            asum.get(asum.size()-1).setYear(i);


            System.out.println("BMI"+bmi);

        }



        System.out.println("i"+i);
    }

      JRDataSource datasource = new JRBeanCollectionDataSource(asum);   

        model.addAttribute("datasourcebmireportA", datasource);

        model.addAttribute("format", "pdf");

        modelAndView = new ModelAndView("pdfReportViewaddsummeryA", model);



    return modelAndView;


}

这样的设计器页面 enter image description here

这是您的设计器 xml

<title>
    <band height="122" splitType="Stretch">
        <rectangle>
            <reportElement x="4" y="100" width="197" height="20"/>
        </rectangle>
        <textField>
            <reportElement x="193" y="71" width="166" height="19"/>
            <textElement>
                <font size="14" isBold="true"/>
            </textElement>
            <textFieldExpression class="java.lang.String"><![CDATA[$F{duration}]]></textFieldExpression>
        </textField>
        <staticText>
            <reportElement x="5" y="9" width="526" height="37"/>
            <textElement textAlignment="Center">
                <font size="14" isBold="true"/>
            </textElement>
            <text><![CDATA[Paediatric Professional Unit -Ward 11
       Teaching Hospital Jaffna]]></text>
        </staticText>
        <line>
            <reportElement x="5" y="92" width="550" height="1" forecolor="#CCCCCC"/>
            <graphicElement>
                <pen lineWidth="2.0"/>
            </graphicElement>
        </line>
        <staticText>
            <reportElement x="5" y="102" width="51" height="20"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font size="12" isBold="true"/>
            </textElement>
            <text><![CDATA[Year]]></text>
        </staticText>
        <staticText>
            <reportElement x="5" y="53" width="526" height="46"/>
            <textElement>
                <font size="14" isBold="true"/>
            </textElement>
            <text><![CDATA[
        BMI value < 18.5kg/m2 -]]></text>
        </staticText>
        <staticText>
            <reportElement x="59" y="102" width="142" height="20"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font size="12" isBold="true"/>
            </textElement>
            <text><![CDATA[Number of Admission]]></text>
        </staticText>
        <line>
            <reportElement x="56" y="100" width="1" height="20"/>
        </line>
    </band>
</title>
<pageHeader>
    <band splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band splitType="Stretch"/>
</columnHeader>
<detail>
    <band height="21">
        <rectangle>
            <reportElement x="4" y="0" width="197" height="20">
                <printWhenExpression><![CDATA[$F{year}!=0]]></printWhenExpression>
            </reportElement>
        </rectangle>
        <textField>
            <reportElement x="56" y="2" width="145" height="17"/>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <textFieldExpression class="java.lang.Integer"><![CDATA[$F{nooftime}]]>           </textFieldExpression>
        </textField>
        <textField>
            <reportElement x="5" y="1" width="51" height="20">
                <printWhenExpression><![CDATA[$F{year}!=0]]></printWhenExpression>
             </reportElement>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <textFieldExpression class="java.lang.Integer"><![CDATA[$F{year}]]>      </textFieldExpression>
        </textField>
        <line>
            <reportElement x="55" y="1" width="1" height="19">
                <printWhenExpression><![CDATA[$F{year}!=0]]></printWhenExpression>
            </reportElement>
        </line>
    </band>
</detail>
<summary>

这是你的 JSP

<tr>
    <td  ><a href="#"><div class="box">
        <form action="BMIbyage">
        <table width="754">
        <tr>
        <td width="190">
        <font color="black">BMI value < 18.5kg/m2</font></td>

        <td width="124"></td>

        <td width="82">
    <select name="syear"> 
    <option value="Pleaseselect">----</option>
                <%for(int j=1980;j<=2030;j++)
                {
                    %>
                    <option value="<%=j%>"><%=j%></option>
                    <%
                    }
                    %>
    </select>
    </td>
    <td width="20"> to </td>
  <td width="178">
    <select name="eyear"> 
    <option value="Pleaseselect">----</option>
                <%for(int j=1980;j<=2030;j++)
                {
                    %>
                    <option value="<%=j%>"><%=j%></option>
                    <%
                    }
                    %>
    </select>
    </td>
    <td width="108"><input type="submit" value="Generate"></td>
    </tr>
    </table>
        </form>

    </div></a></td>
    </tr>

这是 jasper-views.xml

<bean id="pdfReportViewaddsummeryA"
class="org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView"
 p:url="/WEB-INF/Reports/BMIReport.jasper"
 p:reportDataKey="datasourcebmireportA"
 />

这里是 pom.xml

<!--    report
     -->
    <dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>3.7.6</version>
    <type>jar</type>
    <scope>compile</scope>
    <exclusions>
        <exclusion>
            <artifactId>commons-collections</artifactId>
            <groupId>commons-collections</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-beanutils</artifactId>
            <groupId>commons-beanutils</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-digester</artifactId>
            <groupId>commons-digester</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-logging</artifactId>
            <groupId>commons-logging</groupId>
        </exclusion>
    </exclusions>
</dependency>

希望对你有帮助

关于java - JasperReports 与 Maven 和 Eclipse 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24791298/

相关文章:

java - 按键(日期,字符串)对 map 进行排序

java - 如何将多个 JasperPrint 对象组合成一个具有混合页面方向的报告

java - Jasper Report : Document root element "jasperReport", 必须匹配 DOCTYPE root "null"

java - 如何在 JasperReports 中将字段从父列表传递到子列表?

java - 解析 Microsoft CRM Web API 返回的 Odata 值

java - 如何获取当前的 GPS 位置?

java - 如何抑制 Maven "Unable to find resource"消息?

java - 部署到 JBoss 7 的 Resteasy war 提示找不到 "org.resteasy.plugins.server.servlet.ResteasyBootstrap"

java - JSF 2.0 启动时出错

java - 如何通过使用 auto-ddl=update 更改 JPA 中的实体来更改表