如何将2个或更多参数从jsp页面传递给jasper报告

时间:2015-10-12 11:10:31

标签: java jsp jasper-reports

enter image description here

我有一个搜索条件,如上所示,还有一个文本字段。我在这里使用jasper报告。所以我在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>

1 个答案:

答案 0 :(得分: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}%'