JSP页面中的Jasper Reports [英] Jasper Reports in JSP page
本文介绍了JSP页面中的Jasper Reports的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在JSP页面中显示jasper报告?我正在使用iReport1.3.3工具创建
报告。我很难在JSP页面中显示jasper报告。
How to display jasper reports in JSP page? I am using iReport1.3.3 tool to create
reports. I am struggling to display jasper report in JSP page.
是否可以将ArrayList传递给jasper报告?
Is it possible to pass ArrayList to jasper reports?
我需要以PDF和EXcel格式显示报告。
I need to display the report in PDF and EXcel format.
推荐答案
<我编写了一个struts(1.1)应用程序来呈现PDF和CSV。我会在动作处理程序中执行此操作:
I have written an struts (1.1) application that renders PDFs and CSVs. I would do this in an action handler:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setContentType("application/pdf");
OutputStream out = response.getOutputStream();
try {
// generate the PDF
} finally {
out.close();
}
return null;
}
更新:将集合提供给JasperReports
UPDATE: feeding collections to JasperReports
package reports;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.Arrays;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRException;
public class CollectionDataSource implements JRDataSource {
private Iterator iterator = null;
private Object current = null;
public CollectionDataSource(Collection col) {
if (col != null) {
iterator = col.iterator();
}
}
public CollectionDataSource(Object array[]) {
this(Arrays.asList(array == null ? new Object[0] : array));
}
public boolean next() throws JRException {
if (iterator == null || !iterator.hasNext()) {
return false;
} else {
current = iterator.next();
return true;
}
}
public Object getFieldValue(JRField field) throws JRException {
if ("this".equals(field.getName())) {
return current;
} else if (current == null) {
return null;
} else {
Class<?> clazz = current.getClass();
char chars[] = field.getName().toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
String name = new String(chars);
Method method = null;
try {
method = clazz.getMethod("get" + name);
} catch (NoSuchMethodException e) {
if (field.getValueClass() == Boolean.class) {
try {
method = clazz.getMethod("is" + name);
} catch (NoSuchMethodException e1) {
}
}
}
if (method == null) {
throw new JRException("No getter for field " + name);
}
try {
return method.invoke(current);
} catch (Exception e) {
throw new JRException("Exception in getter of " + name, e);
}
}
}
}
这篇关于JSP页面中的Jasper Reports的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文