dataExporter example not working when adding commandButton

UI Components for JSF
Post Reply
jaydev
Posts: 24
Joined: 05 Dec 2011, 20:06

16 Jan 2012, 22:02

Hi,

I modifed the DataExporter demo code so that it populates the DataTable when a "Submit" commandButton is clicked. The data is not exported, only the headers.

Could anyone tell me if I am not updating the correct ui component or if it is a bug?

The submit button code:

Code: Select all

<p:commandButton value="Submit" process="@form"
				actionListener="#{tableBean.submit}"
				update="testFormId,tbl" />
Here is the modified code:

xhtml:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">
<f:view content-type="text/html">
	<h:head>
		<f:facet name="first">
			<meta http-equiv="X-UA-Compatible" content="EmulateIE8" />
			<meta content='text/html; charset=UTF-8' http-equiv="Content-Type" />
			<title>Forecast</title>
		</f:facet>
		<style type="text/css">
.ui-layout-north {
	z-index: 20 !important;
	overflow: visible !important;;
}

.ui-layout-north .ui-layout-unit-content {
	overflow: visible !important;
}

.ui-widget,.ui-widget .ui-widget {
	font-size: 90% !important;
}

.label {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 11px;
	font-weight: normal;
}
</style>
		<!--
	
	<h:outputStylesheet>
	.ui-widget, .ui-widget .ui-widget {
		font-size: 50% !important;
	}
	</h:outputStylesheet>

	-->

	</h:head>

	<h:body>
		<p:layout fullPage="true">

			<p:layoutUnit position="north" size="100" header="" resizable="true"
				closable="false" collapsible="true">
			</p:layoutUnit>

			<p:layoutUnit position="south" size="50" header="Bottom"
				resizable="true" closable="false" collapsible="true"
				collapsed="true">
			</p:layoutUnit>

			<p:layoutUnit position="west" size="400" header="Inputs"
				resizable="true" closable="false" collapsible="true">
				<h:form>
				<p:commandButton value="Submit" process="@form"
				actionListener="#{tableBean.submit}"
				update="testFormId,tbl" />
			<p:ajaxStatus>
				<f:facet name="start">
					<h:graphicImage value="/images/ajaxloading.gif" />
				</f:facet>
				<f:facet name="complete">
					<h:outputText value="" />
				</f:facet>
			</p:ajaxStatus>
				</h:form>
			</p:layoutUnit>

			<p:layoutUnit position="east" size="100" header="Options"
				resizable="true" closable="true" collapsible="true" effect="drop"
				collapsed="true">
			</p:layoutUnit>


			<p:layoutUnit position="center">
			<h:form id="testFormId">
				<p:dataTable id="tbl" var="car" value="#{tableBean.carsSmall}"
					paginator="true" rows="10">
					<p:column sortBy="#{tableBean.carsSmall}"
				filterBy="#{tableBean.carsSmall}">
						<f:facet name="header">
							<h:outputText value="Model" />
						</f:facet>
						<h:outputText value="#{car.model}" />
					</p:column>

					<p:column>
						<f:facet name="header">
							<h:outputText value="Year" />
						</f:facet>
						<h:outputText value="#{car.year}" />
					</p:column>

					<p:column>
						<f:facet name="header">
							<h:outputText value="Manufacturer" />
						</f:facet>
						<h:outputText value="#{car.manufacturer}" />
					</p:column>

					<p:column>
						<f:facet name="header">
							<h:outputText value="Color" />
						</f:facet>
						<h:outputText value="#{car.color}" />
					</p:column>
				</p:dataTable>

				<h:panelGrid columns="2">
					<p:panel header="Export All Data">
						<h:commandLink>
							<p:graphicImage value="/images/excel.png" />
							<p:dataExporter type="xls" target="tbl" fileName="cars" />
						</h:commandLink>

						<h:commandLink>
							<p:graphicImage value="/images/pdf.png" />
							<p:dataExporter type="pdf" target="tbl" fileName="cars" />
						</h:commandLink>

						<h:commandLink>
							<p:graphicImage value="/images/csv.png" />
							<p:dataExporter type="csv" target="tbl" fileName="cars" />
						</h:commandLink>

						<h:commandLink>
							<p:graphicImage value="/images/xml.png" />
							<p:dataExporter type="xml" target="tbl" fileName="cars" />
						</h:commandLink>
					</p:panel>

					<p:panel header="Export Page Data">
						<h:commandLink>
							<p:graphicImage value="/images/excel.png" />
							<p:dataExporter type="xls" target="tbl" fileName="cars"
								pageOnly="true" />
						</h:commandLink>

						<h:commandLink>
							<p:graphicImage value="/images/pdf.png" />
							<p:dataExporter type="pdf" target="tbl" fileName="cars"
								pageOnly="true" />
						</h:commandLink>

						<h:commandLink>
							<p:graphicImage value="/images/csv.png" />
							<p:dataExporter type="csv" target="tbl" fileName="cars"
								pageOnly="true" />
						</h:commandLink>

						<h:commandLink>
							<p:graphicImage value="/images/xml.png" />
							<p:dataExporter type="xml" target="tbl" fileName="cars"
								pageOnly="true" />
						</h:commandLink>
					</p:panel>
				</h:panelGrid>
</h:form>
			</p:layoutUnit>

		</p:layout>

	</h:body>
</f:view>
</html>
TableBean (from PrimeFaces example but removing the data loading from the constructor to a submit() method) that is called when "Submit" is clicked.

Code: Select all

package com.ls.forecast.web.test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class TableBean implements Serializable {

	private static final long serialVersionUID = 7695579230780947616L;

	private final static String[] colors;

	private final static String[] manufacturers;

	private List<Car> carsSmall;

	static {
		colors = new String[10];
		colors[0] = "Black";
		colors[1] = "White";
		colors[2] = "Green";
		colors[3] = "Red";
		colors[4] = "Blue";
		colors[5] = "Orange";
		colors[6] = "Silver";
		colors[7] = "Yellow";
		colors[8] = "Brown";
		colors[9] = "Maroon";

		manufacturers = new String[10];
		manufacturers[0] = "Mercedes";
		manufacturers[1] = "BMW";
		manufacturers[2] = "Volvo";
		manufacturers[3] = "Audi";
		manufacturers[4] = "Renault";
		manufacturers[5] = "Opel";
		manufacturers[6] = "Volkswagen";
		manufacturers[7] = "Chrysler";
		manufacturers[8] = "Ferrari";
		manufacturers[9] = "Ford";
	}

	public TableBean() {
	}

	public void submit() {
		carsSmall = new ArrayList<Car>();
		populateRandomCars(carsSmall, 9);
	}

	private void populateRandomCars(List<Car> list, int size) {
		for (int i = 0; i < size; i++)
			list.add(new Car(getRandomModel(), getRandomYear(),
					getRandomManufacturer(), getRandomColor()));
	}

	public List<Car> getCarsSmall() {
		return carsSmall;
	}

	private int getRandomYear() {
		return (int) (Math.random() * 50 + 1960);
	}

	private String getRandomColor() {
		return colors[(int) (Math.random() * 10)];
	}

	private String getRandomManufacturer() {
		return manufacturers[(int) (Math.random() * 10)];
	}

	private String getRandomModel() {
		return UUID.randomUUID().toString().substring(0, 8);
	}

	public void setCarsSmall(List<Car> carsSmall) {
		this.carsSmall = carsSmall;
	}

}
and the Car class:

Code: Select all

package com.ls.forecast.web.test;

import java.io.Serializable;

public class Car implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4817630746105247261L;
	private String model;
	private String manufacturer;
	private String color;
	private int year;
	public Car(String model, int year, String manufacturer, String color) {
		this.model = model;
		this.year = year;
		this.manufacturer = manufacturer;
		this.color = color;
	}
	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		this.model = model;
	}
	public String getManufacturer() {
		return manufacturer;
	}
	public void setManufacturer(String manufacturer) {
		this.manufacturer = manufacturer;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
}

jaydev
Posts: 24
Joined: 05 Dec 2011, 20:06

25 Jan 2012, 22:03

No one has experienced this issue??

jaydev
Posts: 24
Joined: 05 Dec 2011, 20:06

01 Feb 2012, 17:36

For anyone interested in the solution, I had to update my @ManagedBean to @ViewScoped instead of request scope... Everything works fine now.

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

01 Feb 2012, 18:42

jaydev wrote:For anyone interested in the solution, I had to update my @ManagedBean to @ViewScoped instead of request scope... Everything works fine now.
Very good, glad you solved your issue.

By default, all of my bean/controllers are @ViewScoped, and I don't have any @RequestScoped beans at all. The benefits and pitfalls of @ViewScoped

When I added dataExporter to my JSF/PrimeFaces project, I was able to get it to work by referring to the dataExporter section/instructions in the (latest version of the) PrimeFaces user guide, and the Showcase example.
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

hiho
Posts: 6
Joined: 12 May 2012, 13:22

15 May 2012, 16:50

good morning,
I want to display an image on a web page with <p:graphicImage value="/images/cars/#{car.manufacturer}.jpg"/> but the image is not displayed.
help me please :?

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

15 May 2012, 16:57

hiho wrote:good morning,
I want to display an image on a web page with <p:graphicImage value="/images/cars/#{car.manufacturer}.jpg"/> but the image is not displayed.
help me please :?
Flagging this as off-topic. This is the 2nd reply/response that I've seen that is not related to the original forum topic. Is something wrong with phpBB that may be causing this issue, or is this clearly a (new) user error/mistake that keeps occurring?

For new users, read the following:
viewtopic.php?f=3&t=1194
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

hiho
Posts: 6
Joined: 12 May 2012, 13:22

15 May 2012, 17:07

sorry i'm new user in the forum so whrere can'i put my reply :oops:

User avatar
T.dot
Expert Member
Posts: 620
Joined: 01 Feb 2012, 15:39
Location: Vienna/Austria

15 May 2012, 17:27

For new problems create a new thread.

Go here: viewforum.php?f=3 and click on "new thread"
And as howard said please follow the posting guidelines for a new topic: viewtopic.php?f=3&t=1194

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 68 guests