【问题标题】:How to display the pdf document for user inputs only using ireports(jasper reports) and jsp servlet [duplicate]如何仅使用 ireports(jasper 报告)和 jsp servlet 为用户输入显示 pdf 文档 [重复]
【发布时间】:2015-12-03 06:01:39
【问题描述】:
<form class="form-horizontal" class="form-inline" id="divfundtransferownaccounts" method="post" action="FundTransferToOwnAccounts" onsubmit="return passwordCheck(document.getElementById('password').value)" style="margin-left: px;margin-right: 5px;">
                                <div class="form-group">
                                    <label class="control-label col-sm-5">Enter your password To Proceed the Transaction*</label>
                                    <div class="col-sm-6">
                                        <input  name="confirmfacnumber" id="idconfirmfacnumber" class="form-control input-sm">
                                        <input  name="confirmtacnumber" id="idconfirmtacnumber" class="form-control input-sm">
                                        <input  name="confirmcurrencyamount" id="idcurrencyamount" class="form-control input-sm">
                                        <input  name="confirmcurrencytype" id="idcurrencytype" class="form-control input-sm">
                                        <input name="confirmfacnarration" id="idconfirmfacnarration" class="form-control input-sm">
                                        <input  name="confirmtacnarration" id="idconfirmtacnarration" class="form-control input-sm">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-xs-offset-5 col-xs-10">
                                        <button type="submit" class="btn btn-primary " style="padding-left: 25px;padding-right: 25px;">Proceed</button>
                                        <button type="button" class="btn btn-primary " style="padding-left: 25px;padding-right: 25px;" onClick="goBack()">Back</button>    
                                    </div>
                                </div>
                            </form>

这是 html 表单。现在我想使用 jasper 报告将用户输入到 pdf 报告中。我正在使用 ireport 设计器 5.6.0。我应该使用哪些库以及我应该如何这样做才能获得report.i 仅将 jsp servlet 用于 Web 应用程序,没有任何框架。谢谢。

【问题讨论】:

  • 这很广泛(您需要一个指南来设置您的、jsp servlet、用于报告生成的 java 代码、如何开发 jrxml 代码 ecc)。您需要了解 jasper 报告的工作原理(下载 JasperSoftStudio 之类的 IDE),如何使用 jsf servlet(导出为 pdf),当您对 .jrxml 或 java 有特定问题时,请发布您正在使用的代码,你的输出是什么,你想要的结果是什么

标签: jsp servlets jakarta-ee jasper-reports


【解决方案1】:

在这里我找到了答案,伙计们。感谢您的回复。 首先,我将向您展示文件夹结构。

enter image description here

首先您必须将上面显示的库添加到您的应用程序中。 那么这是jsp文件代码。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap-theme.css"/>
    <link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap-theme.min.css"/>
    <link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap.min.css"/>
    <title>Jasper Report Example</title>
</head>
<body>
    <form class="form-horizontal" class="form-inline" method="post" action="GetUserData" style="margin-left: px;margin-right: 5px;">
        <div style="padding-bottom: 25px;"></div>
        <div class="form-group">
            <label class="control-label col-sm-3">First Name*</label>
            <div class="col-sm-6">
                <input  name="firstname" id="idfirstname" class="form-control input-sm">
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-3">Last Name*</label>
            <div class="col-sm-6">
                <input  name="lastname" id="idlastname" class="form-control input-sm">
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-3">Email*</label>
            <div class="col-sm-6">
                <input  name="email" id="idemail" class="form-control input-sm">
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-3">Phone Number*</label>
            <div class="col-sm-6">
                <input  name="phonenumber" id="idphonenumber" class="form-control input-sm">
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-3">NIC*</label>
            <div class="col-sm-6">
                <input  name="nic" id="idnic" class="form-control input-sm">
            </div>
        </div>

        <div class="form-group">
            <div class="col-xs-offset-5 col-xs-10">
                <button type="submit" class="btn btn-primary " style="padding-left: 25px;padding-right: 25px;">Sumbit</button>
            </div>
        </div>
    </form> 
</body>

正如您在文件夹结构中看到的,您必须创建 pojo 类。 这是 UserDetails.java pojo 类。

public class UserDetails {
private String firstname;
private String lastname;
private String email;
private String phonenumber;
private String nic;

/**
 * @return the firstname
 */
public String getFirstname() {
    return firstname;
}

/**
 * @param firstname the firstname to set
 */
public void setFirstname(String firstname) {
    this.firstname = firstname;
}

/**
 * @return the lastname
 */
public String getLastname() {
    return lastname;
}

/**
 * @param lastname the lastname to set
 */
public void setLastname(String lastname) {
    this.lastname = lastname;
}

/**
 * @return the email
 */
public String getEmail() {
    return email;
}

/**
 * @param email the email to set
 */
public void setEmail(String email) {
    this.email = email;
}

/**
 * @return the phonenumber
 */
public String getPhonenumber() {
    return phonenumber;
}

/**
 * @param phonenumber the phonenumber to set
 */
public void setPhonenumber(String phonenumber) {
    this.phonenumber = phonenumber;
}

/**
 * @return the nic
 */
public String getNic() {
    return nic;
}

/**
 * @param nic the nic to set
 */
public void setNic(String nic) {
    this.nic = nic;
}

}

在创建简单的 pojo 类之后,现在您必须创建控制器类。如文件夹结构中所见,report.controller 包包含控制器类 GetUserData.java servlet 文件。

这是 GetUserData.java servlet 文件代码。

public class GetUserData extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, JRException {
    //response.setContentType("application/pdf");
    //PrintWriter out = response.getWriter();

    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String phonenumber = request.getParameter("phonenumber");
    String nic = request.getParameter("nic");

    HashMap parametermap = new HashMap();
    parametermap.put("firstname", firstname);
    parametermap.put("lastname", lastname);
    parametermap.put("email", email);
    parametermap.put("phonenumber", phonenumber);
    parametermap.put("nic", nic);

    GenerateReport generatereportinstance = new GenerateReport();
    byte[] outputarray = generatereportinstance.GenerateReport(request.getServletContext(),parametermap);
    OutputStream outStream = response.getOutputStream();
    response.setHeader("Content-Disposition", "filename=UserDetailsReport.pdf");

    response.setContentType("application/pdf");
    response.setContentLength(outputarray.length);
    outStream.write(outputarray, 0, outputarray.length);

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        processRequest(request, response);
    } catch (JRException ex) {
        Logger.getLogger(GetUserData.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        processRequest(request, response);
    } catch (JRException ex) {
        Logger.getLogger(GetUserData.class.getName()).log(Level.SEVERE, null, ex);
    }
}

现在是最后的编码部分。在包report.utility包中创建业务逻辑。 这是 GenerateReport.java 类文件代码。

public class GenerateReport {
JasperPrint jasperPrint = null;
public byte[] GenerateReport(ServletContext servletContext, HashMap parametermap) {
    byte[] outputFile = null;
    try {
        try {
            String file = servletContext.getRealPath("/WEB-INF/report/UserDetails.jasper");
            System.out.println("Generating PDF Report");
            JasperPrint jasperPrint =JasperFillManager.fillReport(file,parametermap,new JREmptyDataSource());

            outputFile =JasperExportManager.exportReportToPdf(jasperPrint);
            System.out.println("User Details.pdf has been generated!");

        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return outputFile;
}

}

现在你要做更多的事情。创建报告是下一个。 我使用 iReport 5.6.0. 创建了 UserDeatails.jasper 报告。 使用 iReport 创建报表的基本结构后, 右击报表转到-->属性-->语言(在语言中选择java) 其他明智的报告可能会出现错误。 然后将report.and编译好的jasper文件编译到项目中,如文件夹结构所示。

我将添加碧玉报告的图像。

enter image description here

这就是所有的家伙。再次感谢您的回复。

【讨论】:

  • 考虑接受你自己的答案...
猜你喜欢
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 2011-06-04
  • 2010-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多