Reporting

Fermé
R23S - 7 sept. 2011 à 03:06
Bonjour,


je suis en train de faire une application jsf , spring , hibernate . j'ai mis un code pour générer les rapports mais justement jasperReport / ireport et jsf mais ca marche ps !

package jsf;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Driver;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
public class ReportGenerator
{
					@SuppressWarnings("rawtypes")
					public void generateReport(ActionEvent actionEvent)
					throws ClassNotFoundException, SQLException, IOException,
					JRException
					{
					//
						    String url = "jdbc:mysql://localhost/restaurants";
					        String login = "root";
					        String password ="";
					        Connection connection = null;	
						
				   //		
				   //Connection connection;
					FacesContext facesContext = FacesContext.getCurrentInstance();
					HttpServletResponse response = (HttpServletResponse)
					facesContext.getExternalContext().getResponse();
					InputStream reportStream = facesContext.getExternalContext().getResourceAsStream("/reports/DbReport.jasper");
					ServletOutputStream servletOutputStream = response
					.getOutputStream();
					//Class.forName("com.mysql.jdbc.Driver");
					//connection = DriverManager.getConnection(
					//"jdbc:mysql://localhost:3306/restaurants?" +
					//"user=root&password= ");
					//
					
					Driver monDriver = new com.mysql.jdbc.Driver();
		            DriverManager.registerDriver(monDriver);
		            connection = DriverManager.getConnection(url, login, password);
					
					//
					facesContext.responseComplete();
					response.setContentType("application/pdf");
					JasperRunManager.runReportToPdfStream(reportStream,
					servletOutputStream, new HashMap(), connection);
					connection.close();
					servletOutputStream.flush();
					servletOutputStream.close();
					}
}

face-config.xml :
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 <managed-bean>
  <managed-bean-name>reportGenerator</managed-bean-name>
  <managed-bean-class>jsf.ReportGenerator</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <from-view-id>/index.xhtml</from-view-id>
 </navigation-rule>
</faces-config>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>jasper</display-name>
  <welcome-file-list>
    
    <welcome-file>index.xhtm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>


lorsque je fait run la page index.xhtml il m'affiche une page blanche .