views:

339

answers:

2

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.

Is it possible to pass ArrayList to jasper reports?

I need to display the report in PDF and EXcel format.

A: 

There seems to be a DefaultJasperViewer.jsp, it is mentioned on http://jasperforge.org/plugins/espforum/view.php?group_id=112&forumid=102&topicid=35938

I think it would be nicer to write a taglib. Take a look here: http://seamframework.org/Community/JasperReportsSeam This is related to JSF and Seam, but might give some inspiration.

Adriaan Koster
A: 

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;
 }

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);
            }
        }
    }
}
Maurice Perry
@ Maurice.. is it like by clicking hyperlink in jsp page, will render pdf or excel format reports
Manu
and also if you don't mind can you send me some complete sample codes for this. my email id is : [email protected] or [email protected]
Manu
is it possible to pass ArrayList to xyz.jasper(xyz is filename) template and fill it to pdf or excel format ?
Manu
@Maurice.. if you don't mind can you send me some sample code for pdf and csv format generation using servlets for jasper reports..
Manu
I don't have any: I've only used jasper reports in desktop apps.
Maurice Perry
Concerning the ArrayList question, I have added the code of a CollectionDataSource that I have written (a while ago).
Maurice Perry
Thanx Maurice for your effort in this..
Manu