Buenos días.
Llevo unos días con un problema y sigo sin solucionarlo.
Lo que quiero es pulsar un boton y que se abra o se descargue un xls con unos datos de base de datos (esto último ya lo tengo). Para ello he hecho lo siguiente:
exportarDescuentos.xhtml
<script>
function getUrlServlet(){
var url = document.baseURI;
if(!url){
url = document.URL;
}
var index = url.lastIndexOf("exportarDescuentosController.java ");
var urlDetalle = url.substring(0, index);
var xRespuestaActuacion=document.getElementById("expor tar:ofertaId").value;
urlDetalle = urlDetalle + "/exportarDescuentos.xhtml?id=" + xRespuestaActuacion;
var enlaceVerDetalle = document.getElementById("exportar:cat");
if(enlaceVerDetalle != null){
enlaceVerDetalle.href=urlDetalle;
}
enlaceVerDetalle.href=urlDetalle;
}
</script>
...
<p:commandLink type="exportar" styleClass="exportarDescuentos">
<p:graphicImage name="images/export_excel.png"/>
<h:outputText value="Exportar Descuentos"/>
<f:param name="ofertaId" value="#{ofertaDestacada.idOferta}" />
<f:param name="cat" value="#{ofertaDestacada.categoria.idCategoria}" />
</p:commandLink>
<p:commandLink onclick="getUrlServlet();" type="exportar" styleClass="exportarDescuentos" />
XLSServletAdm.java
public class XLSServletAdm extends HttpServlet{
private static final long serialVersionUID = 1128327314037179152L;
public static final Logger log = Logger.getLogger(XLSServletAdm.class);
private HttpServletRequest peticion;
private HttpServletResponse respuesta;
private ServletContext context;
private Lifecycle lifecycle;
private ServletConfig servletConfig;
private FacesContextFactory facesContextFactory;
private FacesContext facesContext;
private static final String aplicacion= "ETCIGS";
public XLSServletAdm() {
super();
}
/**
* Initialization of the servlet.
*
*/
public void init(ServletConfig conf) throws ServletException {
this.servletConfig = conf;
this.context = conf.getServletContext();
super.init(conf);
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
* llama al m�todo doPost que realiza la generaci�n del informe
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
peticion = request;
respuesta = response;
doPost(peticion, respuesta);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
String pathss = request.getContextPath();
try {
facesContextFactory =
(FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTE XT_FACTORY);
} catch (FacesException e) {
Throwable rootCause = e.getCause();
if (rootCause == null) {
throw e;
} else {
throw new ServletException(e.getMessage(), rootCause);
}
}
try {
LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_F ACTORY);
String lifecycleId =servletConfig.getServletContext().getInitParamete r(FacesServlet.LIFECYCLE_ID_ATTR);
if (lifecycleId == null) {
lifecycleId = LifecycleFactory.DEFAULT_LIFECYCLE;
}
lifecycle = lifecycleFactory.getLifecycle(lifecycleId);
} catch (FacesException e) {
Throwable rootCause = e.getCause();
if (rootCause == null) {
throw e;
} else {
throw new ServletException(e.getMessage(), rootCause);
}
}
facesContext = facesContextFactory.getFacesContext(servletConfig. getServletContext(), request, response, lifecycle);
//Me creo un exportarDescuentosController
ExportarDescuentosController exportar = new ExportarDescuentosController();
FileInputStream fileInputStream = null;
String idAux = request.getParameter("id");
byte[] xlsArchivo = null;
String nombreFicheroPdf="";
byte[] bytes=null ;
/** TODO
* el documento en array bytes
*/
exportar.exportarDescuentos();
nombreFicheroPdf= aplicacion ;
response.reset();
response.setHeader("Content-Disposition", "inline; filename="+nombreFicheroPdf+".pdf");
response.setContentType("application/pdf");
try {
ServletOutputStream sos = response.getOutputStream();
if (bytes==null){
xlsArchivo = exportar.read(exportar.exportarDescuentos());
}
sos.flush();
sos.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
Pero me falla y ya no se que hacer :(
Muchas gracias y un saludo!