Foros del Web » Creando para Internet » HTML »

Servlet xls en jsf

Estas en el tema de Servlet xls en jsf en el foro de HTML en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 27/02/2015, 05:49
 
Fecha de Ingreso: diciembre-2014
Ubicación: Sevilla
Mensajes: 7
Antigüedad: 9 años, 4 meses
Puntos: 0
Sonrisa Servlet xls en jsf

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!
  #2 (permalink)  
Antiguo 27/02/2015, 05:55
 
Fecha de Ingreso: diciembre-2014
Ubicación: Sevilla
Mensajes: 7
Antigüedad: 9 años, 4 meses
Puntos: 0
Respuesta: Servlet xls en jsf

Creo que no se ha entendido bien, lo que quiero es crear archivo xls (ya lo creo) y abrirlo mediante un servlet.

Gracias!! :D
  #3 (permalink)  
Antiguo 27/02/2015, 07:17
Avatar de chichote
Colaborador
 
Fecha de Ingreso: diciembre-2004
Ubicación: Santiago - Chile
Mensajes: 1.868
Antigüedad: 19 años, 4 meses
Puntos: 145
Respuesta: Servlet xls en jsf

Icaleya, tu pregunta no corresponde al foro de html, pide a un moderador que lo ubique en el foro correspondiente.

Saludos.
__________________
http://chicho.ninja yiaaaa
  #4 (permalink)  
Antiguo 02/03/2015, 06:09
 
Fecha de Ingreso: diciembre-2014
Ubicación: Sevilla
Mensajes: 7
Antigüedad: 9 años, 4 meses
Puntos: 0
Respuesta: Servlet xls en jsf

Cita:
Iniciado por chichote Ver Mensaje
Icaleya, tu pregunta no corresponde al foro de html, pide a un moderador que lo ubique en el foro correspondiente.

Saludos.
Gracias chichote, ¿donde se lo pido al moderador?
Es que soy nueva en este foro y no se muy bien como funciona.

Gracias y un saludo.

lcaleya.

Etiquetas: java, jsf, scripts, servlet, url, xls
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:20.