gpt4 book ai didi

java - 如何将jsp中描述的字段或值传递给jasper报表,以便它可以以PDF格式显示数据

转载 作者:行者123 更新时间:2023-12-01 10:07:45 25 4
gpt4 key购买 nike

PDF error message

我使用 Eclipse,下面是代码的一部分

<!--menu.jsp-->
<h1>Pass parameter to jasper report(CI_ID)</h1>
<form action = "call.jsp" target="_blank">
<input type= "text" name = "CI_ID" value = ""/><br/>
<input type="submit" value="Print"/>
</form>

<!--call.jsp-->
<%Connection conn = null;
int id=Integer.parseInt(request.getParameter("CI_ID"));
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","1234");
File reportFile = new File(application.getRealPath("//Report.jasper"));
Map parameters = new HashMap();
parameters.put("CI_ID",id);

byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,conn);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes, 0 , bytes.length);
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e){
e.printStackTrace();
}%>

我在 Jasper 报告上预览没有任何问题,但是当我使用 JSP 将参数传递给 Eclipse 上的 Jasper 报告时,我收到此错误“文件不以 '%PDF-' 开头。” adobe reader 的弹出窗口。

我尝试了以下解决方案,但仍然不起作用

1)更改为

response.setContentType("application/pdf");

response.setContentType("application/x-pdf");

2)更新adobe reader

3)导入jasper所有相关的.jar

最佳答案

首先,您拥有 session 属性和 request.getParameters,以便您可以将这些参数发送到 jasper 报告。我将展示一个示例以便更好地理解。我有一个jsp页面,例如搜索数据.jsp

 <form action="searchResult.jsp" method="POST">
<table border="0" width="" cellspacing="15" cellpadding="1">
<thead>
<tr>
<th>Search Using:</th>
<th><select name="search">
<option value=""> Select</option>
<option value="Company Name">Company Name</option>
<option value="Contact Person">Contact Person</option>
<option value="Phone">Phone Number</option>
</select></th>


<th><input type="text" name="search_tf" /></th>
<th><input type="submit" value=" " class="search_btn"/></th>
</tr>
</thead>
<tr>
<td></td><td></td><td></td><td></td>
<td><small>(Blank Search will result in Full Search Result)</small></td>
</tr>
</table>
</form>

单击提交按钮后,因为我的操作为“searchResult.jsp”,它将转到搜索结果.jsp

在searchResult.jsp页面中,我使用了request.getParameter,以便在前一个jsp页面中输入的数据现在将进入当前jsp页面,然后我使用 session 属性,以便它可以在进一步的jsp页面,直到 session 结束。如下所示

Search Result for <u><%= request.getParameter("search_tf")%></u> in <u><%= request.getParameter("search")%></u> is :

Object o2 = session.getAttribute("email");
String email = o2.toString();
String category = request.getParameter("category");
String search = request.getParameter("search");
session.setAttribute("search",search);
String search_tf = request.getParameter("search_tf");
session.setAttribute("search_tf",search_tf);

然后是我的sql查询,我给出了一个sql查询的例子

if(search.equals("")&& search_tf.equals("")){
sqlPgintn="SELECT SQL_CALC_FOUND_ROWS Id,Company_name,Contact_Person,Address,Phone,Company_Email,Review,Status,Lead_Date,Lead_Details,Lead_Value,followup_Date,Category from marketing_database.lead limit "+iPagNo+","+iSwRws+"";
}
else if(search.equals("Company Name"))
{
sqlPgintn="SELECT SQL_CALC_FOUND_ROWS Id,Company_name,Contact_Person,Address,Phone,Company_Email,Review,Status,Lead_Date,Lead_Details,Lead_Value,followup_Date,Category from marketing_database.lead where Company_Name ='"+search_tf+"' limit "+iPagNo+","+iSwRws+"";

现在我使用 javascript 做到了这一点

<script type="text/javascript">
function generateReport() {
var e = document.getElementById("idOfYourSelectYouNeedToAddedIt");
var strPage = e.options[e.selectedIndex].value;
window.open(strPage);
return false; //This make you stay on this page;
//return true; //Set the action tag in the form to the page you like to go to!
}
</script>
<center> <form name="myForm" onsubmit="return generateReport()">
<select id = "idOfYourSelectYouNeedToAddedIt">
<option value=''> Generate Report </option>
<option value='samplePDF.jsp'> PDF</option>
<option value='sampleDOC.jsp'> DOC </option>
<option value='sampleXLS.jsp'> XLS</option>
<option value='sampleXLSX.jsp'> XLSX </option>
</select>
<br/>

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

当我点击假设“samplePDF.jsp”时,它会转到该页面,我就这样做了示例PDF.jsp

<%
Connection conn = null;
Object o2 = session.getAttribute("email");
String email = o2.toString();
String category=(String)session.getAttribute("category");
String status=(String)session.getAttribute("status");
String startDate=(String)session.getAttribute("startDate");
String endDate=(String)session.getAttribute("endDate");

try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
String sql = "select * from lead where Email = '" + email + "' AND Status LIKE '%" + status + "%' AND Category LIKE '%" + category + "%' AND STR_TO_DATE(`Lead_Date`, '%d-%m-%Y') BETWEEN '"+startDate+"' AND '"+endDate+"'";
ServletContext context = request.getServletContext();
String fullPath = context.getRealPath("/WEB-INF/reports/report10.jrxml");
InputStream input = new FileInputStream(new File(fullPath));
JasperDesign jasperDesign = JRXmlLoader.load(input);

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

System.out.println("Creating JasperPrint Object");
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("sql",sql);
byte[] bytes = JasperRunManager.runReportToPdf(jasperReport, map, conn);

response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes, 0, bytes.length);
outStream.flush();
outStream.close();
conn.close();
}
catch(Exception e)
{e.printStackTrace();}

%>

关于java - 如何将jsp中描述的字段或值传递给jasper报表,以便它可以以PDF格式显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36328542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com