I'm investigating PrimeFaces recently. I try to create editable DataTable. I take some code from demos and created my own Facelets file and managed bean.
products.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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<!-- some headers -->
</h:head>
<h:body>
<h:form>
<p:dataTable id="products" var="product" value="#{productsBean.products}" editable="true" style="margin-bottom: 20px; width: 1000px;">
<f:facet name="header">Products</f:facet>
<p:ajax event="rowEdit" listener="#{productsBean.onRowEdit}" />
<p:ajax event="rowEditCancel" listener="#{productsBean.onRowCancel}" />
<p:column headerText="Nazwa">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{product.name}" /></f:facet>
<f:facet name="input"><p:inputText value="#{product.name}" style="width:100%" label="Nazwa"/></f:facet>
</p:cellEditor>
</p:column>
<!-- more columns... -->
<p:column style="width:32px">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
ProductsBean.java:
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.RowEditEvent;
// more imports...
#SessionScoped #ManagedBean
public class ProductsBean implements Serializable {
private static final long serialVersionUID = -501520863695260180L;
#EJB
private ProductDao productDao;
#EJB
private UnitDao unitDao;
private List<Product> products;
private List<Unit> units;
#PostConstruct
private void init() {
products = productDao.findAll();
units = unitDao.findAll();
}
public void onRowEdit(RowEditEvent event) {
System.out.println("onRowEdit");
}
public void onRowCancel(RowEditEvent event) {
System.out.println("onRowCancel");
}
public List<Product> getProducts() {
return products;
}
public List<Unit> getUnits() {
return units;
}
}
Unfortunatelly, onRowEdit(RowEditEvent) is never invoked, only my table gets red. I don't get any other feedback. onRowCancel(RowEditEvent) is invoked correctly. What am I doing wrong or what I am missing? Thanks for your help.
Related
I thought I'd try and get a primefaces datagrid working with my JSF code, but I always got "no results found". So I'm trying to set up a simple example but I get the same error messages so I must be doing something fundamentally wrong.
I have the following backing bean:
#ManagedBean
#ViewScoped
public class CarBean {
private static final Logger logger = Logger.getLogger("datagrid.ejb.CarBean");
private List<Car> cars;
public CarBean() {
cars = new ArrayList<Car>();
cars.add(new Car("myModel",2005,"ManufacturerX","blue"));
logger.log(LogLevel.INFO, "added car");
//add more cars
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
}
And the following xhtml page:
<!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<p:dataGrid var="car" value="#{carBean.cars}" columns="3"
rows="12"layout="grid">
<p:column>
<p:panel header="#{car.model}">
<h:panelGrid columns="1">
<p:graphicImage value="/images/cars/#{car.manufacturer}.jpg"/>
<h:outputText value="#{car.year}" />
</h:panelGrid>
</p:panel>
</p:column>
</p:dataGrid>
</h:body>
</html>
I can see from the logs and debugger that the CarBean is instantiated but still I get the no records found error. Any ideas?
Thanks,
Zobbo
I am having problems using Dates with the same name in different backing beans:
Backing bean 1:
import java.io.Serializable;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class MenuLink1BB implements Serializable {
private static final long serialVersionUID = 4070538070773061768L;
private String value1;
private Date value2;
#PostConstruct
public void init() {
value1 = "value 1_1";
value2 = new Date();
}
public String loadView() {
return "/test/menuLink1";
}
public void testMenuLink1(String value) {
System.out.println(value);
}
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public Date getValue2() {
return value2;
}
public void setValue2(Date value2) {
this.value2 = value2;
}
}
Backing bean 2:
import java.io.Serializable;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class MenuLink2BB implements Serializable {
private static final long serialVersionUID = -5872644335654136327L;
private Date value2;
private String value3;
#PostConstruct
public void init() {
value2 = new Date();
value3 = "value 2_3";
}
public String loadView() {
return "/test/menuLink2";
}
public void testMenuLink2(String value) {
System.out.println(value);
}
public Date getValue2() {
return value2;
}
public void setValue2(Date value2) {
this.value2 = value2;
}
public String getValue3() {
return value3;
}
public void setValue3(String value3) {
this.value3 = value3;
}
}
View 1:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/template/template.xhtml">
<ui:define name="bodyContent">
<h:form id="form">
<p:panelGrid columns="1">
<h:outputText value="BackingBean 1 Value 1: #{menuLink1BB.value1}" />
<h:outputText value="BackingBean 1 Value 2: #{menuLink1BB.value2}" />
<h:outputText value="BackingBean 2 Value 2: #{menuLink2BB.value2}" />
<h:outputText value="BackingBean 2 Value 3: #{menuLink2BB.value3}" />
</p:panelGrid>
</h:form>
</ui:define>
</ui:composition>
View 2:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/templates/template.xhtml">
<ui:define name="bodyContent">
<h:form id="form">
<p:panelGrid columns="1">
<h:outputText value="BackingBean 2 Value 2: #{menuLink2BB.value2}" />
<h:outputText value="BackingBean 2 Value 3: #{menuLink2BB.value3}" />
<h:outputText value="BackingBean 1 Value 1: #{menuLink1BB.value1}" />
<h:outputText value="BackingBean 1 Value 2: #{menuLink1BB.value2}" />
</p:panelGrid>
</h:form>
</ui:define>
</ui:composition>
When I load any view it show the following ajax error:
Firefox:
TypeError: (intermediate value).exec(...) is null
Chrome:
Uncaught TypeError: Cannot read property '0' of null
And finally, the template.xhml file:
<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">
<h:head>
</h:head>
<h:body>
<p:panelGrid>
<p:row>
<p:column>
<h:form id="menuForm">
<p:panelMenu id="menuPM">
<p:submenu label="Menu">
<p:menuitem action="#{menuLink1BB.loadView}" value="Menu link 1" />
<p:menuitem action="#{menuLink2BB.loadView}" value="Menu link 2" />
</p:submenu>
</p:panelMenu>
</h:form>
</p:column>
<p:column>
<ui:insert name="dialogs" />
<p:messages autoUpdate="true" closable="true" redisplay="false" />
<ui:insert name="bodyContent" />
</p:column>
</p:row>
</p:panelGrid>
</h:body>
</f:view>
</html>
This happens only using fields of type java.util.Date When I have two attributes of type Date in two backing beans with the same name
It was fixed since primefaces 5.1
This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 6 years ago.
Can anyone tel me why command button/link is not getting fired. This welcomePage() should be invoked on button press. I checked the managed property "value" is displaying on the execution, but button or link is not getting invoked.
My loginBean:
import java.io.IOException;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
#ManagedBean(name = "loginBean", eager = true)
#RequestScoped
public class loginBean implements Serializable {
private String value = "This text shows, bean is working ";
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
#ManagedProperty(value="#{name}")
public static String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#ManagedProperty(value="#{password}")
public static String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String welcomePage()
{
System.out.println("welcome user according to user name");
return "customerGreeting.xhtml";
}
}
My index.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:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<f:view contentType="text/html">
<h:head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Facelet Title</title>
</h:head>
<h:body>
<ui:composition>
<div align ="center">
<h:form id="form" align="center">
<h:panelGrid id="panel" columns="2" >
<f:facet name="header">
<h:outputText value="Login here:"/>
</f:facet>
<h:outputLabel value="Username" />
<p:inputText id="name" value="#{loginBean.name}" />
<h:outputLabel value="Password" />
<p:password id="password" value="#{loginBean.password}" />
<f:facet name="footer">
<h:panelGroup style="display:block; text-align:center">
<p:commandButton id="submit1" value="Submit1" actionListener="#{loginBean.welcomePage}" />
<p:commandLink actionListener="#{loginBean.welcomePage}">
<h:outputText id= "submit2" value="Submit2" />
</p:commandLink>
</h:panelGroup>
</f:facet>
</h:panelGrid>
#{loginBean.value}
</h:form>
</div>
</ui:composition>
</h:body>
</f:view>
</html>
Any help ??
you should use action instead of actionListener, because action needs to return a string for navigation rules
drag, drop and sorting working fine for me but filtering not working properly(filter working only once). If any one gets solutions means kindly help me.
index.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:p="http://primefaces.org/ui">
<h:head>
<script type="text/javascript">
function handleDrop(event, ui)
{
var droppedCar = ui.draggable;
droppedCar.fadeOut('fast');
}
</script>
</h:head>
<h:body>
<h:form id="carForm">
<p:dataTable id="availableCars" var="car" value="#{Report.field1}">
<p:column sortBy="#{car.values}" filterBy="#{car.values}" headerText="Properties">
<h:outputText id="dragIcon" value="#{car.values}" />
<p:draggable for="dragIcon" revert="true" />
</p:column>
</p:dataTable>
<p:fieldset id="selectedCars" style="margin-top:20px">
<p:outputPanel id="dropArea">
<h:outputText value="!!!Drop here!!!" rendered="#{empty Report.field2}" style="font-size:24px;" />
<p:dataTable var="car" value="#{Report.field2}" rendered="#{not empty Report.field2}">
<p:column headerText="Color">
<h:outputText value="#{car.values}" />
</p:column>
</p:dataTable>
</p:outputPanel>
</p:fieldset>
<p:droppable for="selectedCars" tolerance="touch" activeStyleClass="ui-state-highlight" datasource="availableCars" onDrop="handleDrop">
<p:ajax listener="#{Report.onCarDrop}" update="dropArea availableCars" />
</p:droppable>
</h:form>
</h:body>
</html>
and backbeans are
tempReport.java
public class tempReport
{
public String values;
public tempReport(String values)
{
this.values=values;
}
public String getValues()
{
return values;
}
public void setValues(String values)
{
this.values=values;
}
}
ReportGenerator.java
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.event.DragDropEvent;
#ManagedBean(name="Report")
#RequestScoped
public class ReportGenerator implements Serializable
{
static List<tempReport> field1=new ArrayList<tempReport>(),field2=new ArrayList<tempReport>();
static int i=1;
public ReportGenerator()
{
if(i==1)
{
field1=new ArrayList<tempReport>();
field2=new ArrayList<tempReport>();
field1.add(new tempReport("one"));
field1.add(new tempReport("two"));
field1.add(new tempReport("three"));
field1.add(new tempReport("four"));
field1.add(new tempReport("five"));
field1.add(new tempReport("six"));
i++;
}
}
public List<tempReport> getField1()
{
return field1;
}
public void setField1(List<tempReport> field1)
{
this.field1=field1;
}
public List<tempReport> getField2()
{
return field2;
}
public void setField2(List<tempReport> field2)
{
this.field2=field2;
}
public void onCarDrop(DragDropEvent ddEvent)
{
tempReport car = ((tempReport) ddEvent.getData());
int x=0;
for(tempReport item: field2)
{
if(item==car)
x=1;
}
if(x==0)
field2.add(car);
}
}
Thank you
I am back to primefaces 3.3.1, because its fine working in 3.3.1 not working in 3.4 and 3.4.1
Thank you
Halo
My name is Sergie,I working on my school project using jsf spring and jpa to built the school automation system. i am learning jsf.
I need your help, Three header
City | School | Add/Remove
<c:column>
<f:facet name="header">
<c:outputText value="City" />
</f:facet>
<c:outputText id="ukrCity" value="" " />
</c:column>
<c:column>
<f:facet name="header">
<c:outputText value="School" />
</f:facet>
<c:inputText id="school" value=""
maxlength="12" " />
</c:column>
<c:column>
<f:facet name="header">
<c:outputText value="Add/Remove" />
</f:facet>
<c:selectBooleanCheckbox
id="addremove"
value=""
rendered="" />
</c:column>
City | School | Add/Remove
Київ "textbox" "checkbox"
Харків "textbox" "checkbox"
Cities are populated from City class
public class UkrCity {
private List<A> ukrCities;
public List<A> getUkrCities() {
return ukrCities;
}
public void setUkrCities(final List<A> ukrCities) {
this.ukrCities= ukrCities;
}
private void allCities() {
//add all cities in a list
ukrCities.add("Київ");
ukrCities.add("Харків");
}
}
how to show ukrcities on xhtml page under City and blank textbox and checkbox under school and add/remove tav.
thank you
sorry my bad english.
I have refactored your code. Here is the UkrCity Class.
package com.example;
import java.io.Serializable;
public class UkrCity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public UkrCity(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here is the backing bean
package com.example;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class TableBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private ArrayList<UkrCity> cities = new ArrayList<UkrCity>(Arrays.asList(new UkrCity("Київ"),new UkrCity("Харків") ));
public ArrayList<UkrCity> getCities() {
return cities;
}
}
Here is your index.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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>UkrCities Table</title>
</h:head>
<h:body>
<h:form>
<h:dataTable value="#{tableBean.cities}" var="city">
<h:column>
<f:facet name="header">City</f:facet>
<h:outputText value="#{city.name}" />
</h:column>
<h:column>
<f:facet name="header">School</f:facet>
<h:inputText value="" />
</h:column>
<h:column>
<f:facet name="header">Add/Remove</f:facet>
<h:selcectBooleanCheckbox value="" onclick="submit()" />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>