how to create chart in java web application - jsf-2

I am new to Java Web. I am using netbeans 7.0.1 and glassfish 3 to develop a web application. I am almost through with all data input and Now I want to create reports & graph. Requirement is to create charts (pie/bar/line based on user selection) with dynamic data (from database / datatable user can apply filters on data). I tried using Primefaces but it gives below error with simple example given in primeface show case:
javax.faces.event.AbortProcessingException:
/pages/analyst/analyse.xhtml #65,151
actionListener="#{analysis.bar()}": java.lang.NoClassDefFoundError:
org/primefaces/component/chart/series/ChartSeries
I have a jsf page that holds datatable backed by managedbean of viewscope. below is my code:
page
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<body>
<ui:composition template="./../../WEB-INF/Templates/VUTemplate.xhtml">
<ui:define name="menu">
<h:form id="home">
<p:menu>
<p:menuitem value="Home Page" url="../home.xhtml" />
</p:menu>
</h:form>
</ui:define>
<ui:define name="content">
<h:form id="form" prependId="false">
<p:growl id="growl" showDetail="true"/>
<p:barChart id="basic" value="#{analysis.categoryModel}" legendPosition="ne"
title="Basic Bar Chart" min="0" max="200" style="height:300px"/>
<p:dataTable id="table" var="var" widgetVar="wbTable"
value="#{analysis.list}"
paginator="true" rows="10"
paginatorPosition="bottom"
emptyMessage="No data found with given criteria"
>
<f:facet name="header">
World Bank Open Data Analysis
</f:facet>
<p:column filterBy="#{var.regionFullName}" sortBy="#{var.regionFullName}"
headerText="Region" footerText="Enter starting characters" >
<h:outputText value="#{var.regionFullName}" />
</p:column>
<p:column filterBy="#{var.countryFullName}" sortBy ="#{var.countryFullName}"
headerText="Country" footerText="Enter starting characters" >
<h:outputText value="#{var.countryFullName}" />
</p:column>
<p:column filterBy="#{var.indcatorTypeName}" sortBy ="#{var.indcatorTypeName}"
headerText="Indicator Type" footerText="Enter starting characters" >
<h:outputText value="#{var.indcatorTypeName}" />
</p:column>
<p:column filterBy="#{var.indicatorName}" sortBy ="#{var.indicatorName}"
headerText="Indicator" footerText="contains" filterMatchMode="contains">
<h:outputText value="#{var.indicatorName}" />
</p:column>
<p:column filterBy="#{var.year}" sortBy ="#{var.year}"
headerText="Year" footerText="starts with">
<h:outputText value="#{var.year}" />
</p:column>
<p:column filterBy="#{var.dataValue}" sortBy ="#{var.dataValue}"
headerText="Data Value" footerText="starts with" >
<h:outputText value="#{var.dataValue}" />
</p:column>
</p:dataTable>
<h:panelGrid id="command" columns="6" cellpadding="4" >
<p:commandButton id="line" value="Line" actionListener="#{analysis.line()}" process="#this :form:table" update="result table" >
</p:commandButton>
<p:commandButton id="bar" value="Bar" actionListener="#{analysis.bar()}" process="#this :form:table" update="result table" >
</p:commandButton>
</h:panelGrid>
<p:messages id="result" showDetail="true" autoUpdate="true"/>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
backbean
import ejb.VwdataFacade;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import model.Vwdata;
import org.primefaces.component.chart.series.ChartSeries;
import org.primefaces.model.chart.CartesianChartModel;
#ManagedBean
#ViewScoped
public class Analysis implements Serializable{
private List<Vwdata> list;
private #EJB VwdataFacade wsvc; //entity services
private CartesianChartModel categoryModel;
public Analysis() {
}
public CartesianChartModel getCategoryModel() {
return categoryModel;
}
#PostConstruct
public void init() {
list = wsvc.findAll();
}
public List<Vwdata> getList() {
// list = wsvc.findAll();
return list;
}
public void line(){
}
public void bar(){
createCategoryModel();
}
private void createCategoryModel() {
categoryModel = new CartesianChartModel();
ChartSeries yaxis = new ChartSeries();
yaxis.setLabel("Label");
yaxis.set("2004",120);
yaxis.set("2005",130);
yaxis.set("2006",140);
yaxis.set("2007",110);
categoryModel.addSeries(yaxis);
}
}
If I place call createCategoryModel in the constructor or init method, I cant even view the page which otherwise was working perfectly without chart code. Any help will be highly appreciated. Thanks

java.lang.NoClassDefFoundError: org/primefaces/component/chart/series/ChartSeries
The mentioned class was introduced in PrimeFaces 3.0 and was thus not available in PrimeFaces 2.x and older.
Given the fact that the PrimeFaces 3.0 specific XML namespace http://primefaces.org/ui and all the <p:xxx> tags in your code did apparently not result in a Facelets compile error, that can only mean that you've both PrimeFaces 2.x and 3.x libraries in your webapp's runtime classpath wherein the 2.x one got precedence in classloading.
Get rid of the offending old PrimeFaces 2.x library from your webapp's runtime classpath and this problem should then disappear.

Related

Jsf2 - Richfaces4 - Data scroller actionListener not called with datatable

To make it short, in a jsf page, I have 2 rich:dataTable.
The first one is loaded within the onLoad event of the page.
The second one is loaded when a row is clicked within the first.
Both tables have a rich:dataScroller.
Everything is working fine except that I don't find how to make working the actionListener of the dataScroller.
Schematically I have
<rich:collapsiblePanel>
<h:form>
<h:panelGroup id="table1">
<rich:dataTable>
<f:facet name="header">
</f:facet>
<rich:column>
</rich:column>
<f:facet name="footer">
<rich:dataScroller reRender="table1,table2" page="1" actionListener="#{mybean.ac}"/>
</f:facet>
</rich:dataTable>
</h:panelGroup>
<h:panelGroup id="table2">
<rich:dataTable rendered="#{myBean.rowSelected != null}">
<f:facet name="header">
</f:facet>
<rich:column>
</rich:column>
<f:facet name="footer">
<rich:dataScroller reRender="table1" page="1" />
</f:facet>
</rich:dataTable>
</h:panelGroup>
</h:form>
</rich:collapsiblePanel>
In my bean I have
Long rowSelected;
public void ac()
{
rowSelected = null;
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
}
But nothing to do, never succeeded to make it work.
I also tried without any différence
public void ac(ActionEvent ae) - using javax.faces.event.ActionEvent
and
action="#{mybean.ac}"
<rich:dataScroller> has neither action nor actionListener attributes. If you're using RichFaces 4.3 you can use scrollListener. See the docs.

DataTable export is not working in Primefaces 4.0

I want to export a data table in csv,pdf format in primefaces 4.0 .For that I used following code :
<p:dataTable var="valuesTable" value="#{userBean.groupResultList}"
paginator="false" id="valuesTable" resizableColumns="true"
rendered="#{not empty userBean.groupResultList}">
<p:column headerText="Group" id="grp"> #{valuesTable.groupName}</p:column>
<p:column headerText="Technical Knowledge" id="tk">
#{valuesTable.tkValue}
</p:column>
<p:column headerText="Project Management" id="pm">
#{valuesTable.pmValue}
</p:column>
<p:column headerText="Growth" id="ga">
#{valuesTable.gaValue}
</p:column>
<f:facet name="footer">
<h:commandLink>
<p:graphicImage value="/images/xml.jpeg" width="20" height="20" />
<p:dataExporter target="valuesTable" type="xml" fileName="Data_XML" />
</h:commandLink>
</f:facet>
</p:dataTable>
Data table displays correct data in each cell when I run the application.
But when I try to export it in csv or pdf format, rather then exporting actual numbers (or values of the object) it is exporting "#{valuesTable.groupName}" "#{valuesTable.tkValue}" ,"#{valuesTable.pmValue}" ,"#{valuesTable.gaValue}" for each object in csv file.
CSV file Content :
#{valuesTable.groupName} #{valuesTable.tkFormatedValue} #{valuesTable.pmFormatedValue} #{valuesTable.gaFormatedValue}
Data Table Content :
Group Name Technical Knowledge Project Management Growth
India .8 .7 1.0
I don not know why it is exporting the data table in this way..
Please Help
Thanks
I had a similar problem once with the dataTable exporter.
You have to use an outputText like this:
<h:outputText value="#{valuesTable.tkValue}"/>
inside all the column tags, in order to see the values properly.
Your column would become:
<p:column headerText="Technical Knowledge" id="tk">
<h:outputText value="#{valuesTable.tkValue}"/>
</p:column>
Replace all your columns and try that!
Hope it helps!
And also I think the headers are not ok. You have to use an f:facet tag like this:
<p:column id="tk">
<f:facet name="header">
<h:outputText value="Technical Knowledge" />
</f:facet>
<h:outputText value="#{valuesTable.tkValue}"/>
</p:column>
Now you should also see the headers in the export CSV file.
DataTable export is not working in Primefaces 4.0
Step 1: You need to add jars for exporting csv ,pdf file .
itext-1.1.4.jar
Step 2: DataExplorar.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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:p="http://primefaces.org/ui">
<p:panel header="Generate Report ">
<div>
<h:commandLink>
<p:graphicImage value="/images/pdf.png"
style="width:30px; hight:30px;" />
<p:dataExporter type="pdf" target="tb1"
fileName="fileName" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="/images/excel.png"
style="width:25px; hight:30px;" />
<p:dataExporter type="xls" target="tb1"
fileName="fileName" />
</h:commandLink>
</div>
</p:panel>

Datatable don't refresh

Good evening from Sweden!
I try to get a primefaces datatable to refresh. I tried various hints and solutions but nothing is working.
The button triggers a search in the DB and if I refresh the paga manually or if I use the tables sorting-function the result is shown.
Means the code in the backing bean works. But no automatic refresh directly after the button is clicked..
The bean scope is Session.
Here is the page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<ui:composition template="/templates/BasicTemplate.xhtml">
<ui:define name="content">
<h:form id="src_res">
<h2>Sök klass,undervisningsgrupp eller individ</h2>
<b>Sökord</b>: <h:inputText id="sw" value="#{smsb.searchword}"/>
<p:outputLabel value="Lärare"></p:outputLabel>
<h:selectOneListbox size="1" id="teacher" value="#{smsb.searchteacher}" >
<f:selectItems value="#{smsb.teachers}"/>
</h:selectOneListbox>
<p:commandButton action="#{smsb.searchgrp_user}" value="Sök!" update="src_list"/>
<p:dataTable id="src_list" value="#{smsb.searchresult}" var="obj" >
<p:column width="20%" sortBy="#{obj.groupname}">
<f:facet name="header" >
<h:outputText value="Beteckning"/>
</f:facet>
<h:outputText value="#{obj.groupname}"/>
</p:column>
<p:column width="10%">
<f:facet name="header" >
<h:outputText value="typ"/>
</f:facet>
<h:outputText value="#{obj.klass}"/>
</p:column>
<p:column width="10%" sortBy="#{obj.teacher}">
<f:facet name="header" >
<h:outputText value="lärare"/>
</f:facet>
<h:outputText value="#{obj.teacher}"/>
</p:column>
</p:dataTable>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
Any help or hints?
With regards
Ralf
EDIT
Hello and thanx for the offer to help!
As you guessed searchresult is a list and the Getter is defined by
List<sbasUserList> searchresult;
public List<sbasUserList> getSearchresult() {
return searchresult;
}
search_grp is a method of the backing bean:
public String searchgrp_user(){
//FacesContext context = FacesContext.getCurrentInstance();
ObjectContainer db = this.connBD("sms");
ObjectSet<sbasUserList> res;
this.searchresult = new ArrayList<sbasUserList>() ;
Query query=db.query();
query.constrain(sbasUserList.class);
query.descend("groupname").constrain(this.searchword).like();
query.descend("type").constrain('P').or(query.descend("type").constrain('M'));
query.descend("teacher").constrain(searchteacher).contains();
res = query.execute();
this.searchresult.addAll(res);
System.out.println(this.searchresult.size());
db.close();
return "";
}
The table is filled with the right dat if i update manually the browser. (F5) but certainly would like to have this done automatically.
Sorry folks! The glassfish server I was using wasn't setup right. It showed not all of the errors in the log file. I did a complete new setup of the whole IDE (new Eclipse, new glassfish server, thanx BalusC for the incredible tutorial!!!) and I found all errors. Now it works like it should!
Regards
Ralf

Left Layout disappears whenever datatable is added within the center Pane of primeFaces Layout

Im using Jsf 2.0 and primefaces exstensions component pe:layout to create a page with left navigation menu bar ,bottom footer and center pane for content everthing works fine but when ever i add primefaces datatable (p:datatable) in the center pane the header and footer layouts are preserved but the left pane disappears and the center pane occupies the entire page. I tried setting width and height of the datatable but nothing seems to work. The page displays correctly without datatable. Any help would be greatly appreciated.
Please find below the code with datatable:
<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"
xmlns:pe="http://primefaces.org/ui/extensions">
<ui:composition template="/templates/showcaseLayout.xhtml">
<ui:define name="centerContent">
<f:facet name="header">
<h:outputText value="Jan Sampark"/>
</f:facet>
<h:panelGroup layout="block" styleClass="centerContent">
Welcome to the Menu Page.
<p:growl id="messages" showDetail="true"/>
</h:panelGroup>
<p:tabView id="tabView">
<p:tab id="tab1" title="Godfather Part I">
<h:panelGrid columns="2" cellpadding="10">
<p:graphicImage id="tab1Img" value="/images/godfather/godfather1.jpg" />
<h:outputText id="tab1Text"
value="The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
His beloved son Michael has just come home from the war, but does not intend to become part of his father's business. T
hrough Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family, kind and benevolent to those who give respect,
but given to ruthless violence whenever anything stands against the good of the family." />
</h:panelGrid>
</p:tab>
<p:tab id="tab2" title="Godfather Part II">
<h:panelGrid columns="2" cellpadding="10">
<p:graphicImage id="tab2Img" value="/images/godfather/godfather2.jpg" />
<h:outputText id="tab2Text" value="Francis Ford Coppola's legendary continuation and sequel to his landmark 1972 film, The_Godfather, parallels the young Vito Corleone's rise with his son Michael's spiritual fall, deepening The_Godfather's depiction of the dark side of the American dream.
In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills his family. Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy,
killing the local Black Hand Fanucci after he demands his customary cut of the tyro's business. With Fanucci gone, Vito's communal stature grows."/>
</h:panelGrid>
</p:tab>
<p:tab id="tab3" title="Godfather Part III">
<h:panelGrid columns="2" cellpadding="10">
<p:graphicImage id="tab3Img" value="/images/godfather/godfather3.jpg" />
<h:outputText id="tab3Text" value="After a break of more than 15 years, director Francis Ford Coppola and writer Mario Puzo returned to the well for this third and final story of the fictional Corleone crime family.
Two decades have passed, and crime kingpin Michael Corleone, now divorced from his wife Kay has nearly succeeded in keeping his promise that his family would one day be completely legitimate."/>
</h:panelGrid>
</p:tab>
</p:tabView>
<h:panelGroup layout="block" styleClass="centerContent">
<p:dataTable var="car" value="#{tableBean.carsSmall}" id="carList" editable="true" style="table-layout: fixed"
scrollable="true" scrollHeight="250" scrollWidth="300">
<f:facet name="header">
In-Cell Editing
</f:facet>
<!-- <p:ajax event="rowEdit" listener="#{tableBean.onEdit}" update=":form:messages" /> -->
<!-- <p:ajax event="rowEditCancel" listener="#{tableBean.onCancel}" update=":form:messages" /> -->
<p:column headerText="Model" >
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.model}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{car.model}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Manufacturer">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.manufacturer}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{car.manufacturer}" >
<f:selectItems value="#{tableBean.manufacturers}"
var="man"
itemLabel="#{man}"
itemValue="#{man}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Color">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.color}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{car.color}" >
<f:selectItems value="#{tableBean.colors}"
var="color"
itemLabel="#{color}"
itemValue="#{color}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Options">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:panelGroup>
</ui:define>
<ui:define name="docuTable">
<ui:include src="/sections/shared/documentation.xhtml">
<ui:param name="tagName" value="timeline"/>
</ui:include>
</ui:define>
</ui:composition>
</html>

JSF Bean won't set

This is very confusing. I'm working on a simple app for a class and followed the examples but the damn Bean property just won't set. Code follows:
Bean code:
import java.io.Serializable;
import javax.faces.bean.*;
import javax.faces.event.*;
#ManagedBean(name="dist") // or #Named("dist")
#SessionScoped
public class DistanceConvertBean implements Serializable{
private static final double MILESTOKM = 1.609344;
private static final double KMTOMILES = 0.62137;
private double input;
private String convertTo;
private String outputString;
Eclipse generated getters and setters
Index.xhtml code:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h:panelGrid columns="3">
<h:outputText value="Distance:"/>
<h:inputText id="inDist" value="#{dist.input}" required="true">
<f:convertNumber maxFractionDigits="2"/>
<f:validateDoubleRange maximum="10000"/>
</h:inputText>
<h:message for="inDist"/>
<h:outputText value="Convert To:"/>
<h:form>
<h:selectOneRadio value="#{dist.convertTo}" required="true">
<f:selectItem itemValue="km" itemLabel="Kilometer"/>
<f:selectItem itemValue="miles" itemLabel="Miles"/>
</h:selectOneRadio>
</h:form>
<h:form>
<h:commandButton value="Convert" action="blah"/>
</h:form>
</h:panelGrid>
</h:body>
</html>
Blah.xhtml code:
<h:body>
<h:outputText value="#{dist.input}"/>
</h:body>
So I put in 10 in the InputText field in Index.xhtml and press the command button to go to blah.xhtml. The value in blah is still 0.0.
I'm using Tomcat 7 and Eclipse Heilos and created a dynamic web project. I do have all the JSF jars in WEB-INF/libs. I don't understand why the bean properties are not being set. This look exactly like those newbie codes you find in COREJSF examples. Web.xml is using 2.5 for servlet so I shouldn't need a face-config.xml.
Your <h:inputText> is not inside a <h:form> element.
Put the <h:inputText> inside the same <h:form> as the command button:
<h:form>
<h:inputText id="inDist" value="#{dist.input}" required="true">
<f:convertNumber maxFractionDigits="2"/>
<f:validateDoubleRange maximum="10000"/>
</h:inputText>
<h:message for="inDist"/>
<h:commandButton value="Convert" action="blah"/>
</h:form>
The same applies to your <h:selectOneRadio>. Put it inside the <h:form> where the commandButton is.

Resources