【问题标题】:How to pass 2 or more parameters from jsp page to jasper report如何将 2 个或更多参数从 jsp 页面传递到 jasper 报告
【发布时间】:2016-03-24 22:32:29
【问题描述】:

我有一个如上所示的搜索条件和一个文本字段。我在这里使用碧玉报告。所以我在 ireport-5.6.0 中创建了参数,它在 ireport-5.6.0 的内部预览中工作正常,但是当我想将从 jsp 页面中选择的值发送到 ireport 并以 PDF 格式打印数据时。它不工作。仅对一个参数起作用,它不采用选择的其他两个值。这是我的 jsp 页面。

Purchase.jsp

<form action="view.jsp" method="post">
<select name="complan">
<option value="">Make a selection</option>
<option value="Company Name">Company Name</option>
<option value="Contact Person">Contact Person</option>
<option value="Phone Number">Phone Number</option>
</select>
<select name="category">
<option value=""> Make a selection </option>
<option value="company">company</option>
<option value="institution">institution</option>
<option value="hospital">hospital</option>
<option value="Others">Others</option>
</select>
<input type="text" name="search"/>
<input type="submit" value="Submit"/>
</form>

view.jsp

    <script type="text/javascript">

        function setAction(nPage){

            document.forms[0].action = nPage;
        }
    </script>
<form>
<% 
String search=request.getParameter("search");
session.setAttribute("sea",search);
String category=request.getParameter("category");
session.setAttribute("cat",category);
String complan = request.getParameter("complan");
session.setAttribute("com",complan);
%>
<select onchange="setAction(this.value)">
<option value=''> Make a selection </option>
<option value='PDF_LEAD.jsp'> PDF</option>
<option value='XLS_LEAD.jsp'> XLS </option>
<option value='DOC_LEAD.jsp'> DOC </option>
<option value='XLSX_LEAD.jsp'> XLSX </option>
</select>
<br/>

<input type="submit" value="Submit">
</form>

PDF_LEAD.jsp

<body>
<% 
Connection conn = null;
String sear=(String)session.getAttribute("sea");
String cate=(String)session.getAttribute("cat");
String comp=(String)session.getAttribute("com");
try 
{ 
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
    String jrxmlFile ="D:/dev/tools/jasper files/report10.jrxml";
    InputStream input = new FileInputStream(new File(jrxmlFile));
    JasperDesign jasperDesign = JRXmlLoader.load(input);

    System.out.println("Compiling Report Designs");
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

    System.out.println("Creating JasperPrint Object");
    Map parameters = new HashMap();
    parameters.put("complan",comp);
    parameters.put("search",sear);
    parameters.put("category",cate);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,conn);
    byte bytes[] = new byte[10000]; 
    JRPdfExporter exporter = new JRPdfExporter();
    ByteArrayOutputStream PDFStream = new ByteArrayOutputStream(); 
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, PDFStream); 
    exporter.exportReport(); 
    System.out.println("Size of byte array:"+PDFStream.size()); 
    bytes = PDFStream.toByteArray(); 
    response.setContentType("application/pdf"); 
    System.out.println("After JasperPrint = 1"); 
    response.setContentLength(bytes.length); 
    System.out.println("After JasperPrint = 2"); 
    PDFStream.close(); 
    System.out.println("After JasperPrint = 3"); 

    OutputStream outputStream = response.getOutputStream(); 
    System.out.println("After JasperPrint = 4"); 
    outputStream.write(bytes, 0, bytes.length); 
    outputStream.flush(); 
    outputStream.close(); 

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

%>
</body>

【问题讨论】:

  • 您使用了正确的参数传递方式,我认为问题出在 .jrxml 中(或者您以不同的方式调用参数),我需要查看 jrxml 文件的第一部分.此外,为了确保您的代码在设置参数之前可以正常工作,请尝试输出 sear、cate 和 comp 的值。
  • @PetterFriberg 你有没有传递2个或更多参数并生成报告的示例报告??
  • 删除 defaultValueExpression 你不能使用这里的字段,当你不传递任何参数数据时使用默认值,但在你的情况下应该是 String es "google" 所以你可以测试报告.. 基本上可以用来检查查询是否有效以及参数是否到达的默认值
  • 我看到的另一个问题是,如果你使用 =$P!{category} !如果您不使用 $P{category} ,则需要单引号您的查询 ='$P!{category}' 因为它使用了 prepardstatement
  • 这有效:,检查你的xml,如果你不能让它工作,在默认表达式中只留下一个字符串作为在我的例子中

标签: java jsp jasper-reports


【解决方案1】:

首先我要感谢@Peter Friberg,因为没有你这是不可能的。我删除了 jrxml 中的“默认值表达式”。如下。

<parameter name="category" class="java.lang.String">
        <parameterDescription><![CDATA[]]></parameterDescription>
    </parameter>
    <parameter name="search" class="java.lang.String">
        <parameterDescription><![CDATA[]]></parameterDescription>
    </parameter>
    <queryString>
        <![CDATA[select * from lead where category ='$P!{category}' and Company_Name like '$P!{search}%']]>
    </queryString>

我的 SQL 查询是

select * from lead where category ='$P!{category}' and Company_Name like '$P!{search}%'

【讨论】:

  • 很高兴您回答了它(upvote),只需将其关闭即可; )
  • 此外,当您进一步进行测试时,最好不要使用 $P!{ 因为如果将 es company 作为 goo'g 传递,您的代码将中断。解决方案是使用不带 !, $P{,您强制 jasper 报告使用准备好的语句,查询将类似于 select * from lead where category =$P{category} and Company_Name like $P{search},注意 %你需要在将它传递给jasper报告之前添加jsp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多