here is the full minimal example to repreduce my problem
index.xhtml :
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions"
xmlns:p="http://primefaces.org/ui" lang="en">
<h:head>
<title>Primefaces</title>
</h:head>
<h:body>
<p>
<ui:insert name="body">This is my XHTML.</ui:insert>
<h:form>
<p:messages>
</p:messages>
<p:dataTable var="car" value="#{dtBasicView.cars}">
<p:column headerText="Id">
<h:outputText value="#{car.id}" />
</p:column>
<p:column headerText="Year">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Brand">
<h:outputText value="#{car.brand}" />
</p:column>
<p:column headerText="Color">
<h:outputText value="#{car.color}" />
</p:column>
<p:column headerText="Action">
<p:menu>
<p:menuitem value="edit" icon="ui-icon-disk"
oncomplete="PF('dlg1').show();" update=":myForm:myDialog">
<f:setPropertyActionListener value="#{car}"
target="#{dtBasicView.selectedCar}" />
<p:resetInput target=":myForm:myDialog" />
</p:menuitem>
</p:menu>
</p:column>
</p:dataTable>
</h:form>
</p>
</h:body>
<ui:include src="/dialogEdit.xhtml" />
</html>
BasicView.java
Code: Select all
package org.test.pf;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="dtBasicView")
@SessionScoped
public class BasicView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Car> cars;
@ManagedProperty("#{carService}")
private CarService service;
private Car selectedCar;
@PostConstruct
public void init() {
cars = service.createCars(10);
}
public List<Car> getCars() {
return cars;
}
public void setService(CarService service) {
this.service = service;
}
public Car getSelectedCar() {
return selectedCar;
}
public void setSelectedCar(Car selectedCar) {
this.selectedCar = selectedCar;
}
}
CarService.java
Code: Select all
package org.test.pf;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
@ManagedBean(name = "carService")
@ApplicationScoped
public class CarService {
private final static String[] colors;
private final static String[] brands;
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";
brands = new String[10];
brands[0] = "BMW";
brands[1] = "Mercedes";
brands[2] = "Volvo";
brands[3] = "Audi";
brands[4] = "Renault";
brands[5] = "Fiat";
brands[6] = "Volkswagen";
brands[7] = "Honda";
brands[8] = "Jaguar";
brands[9] = "Ford";
}
public List<Car> createCars(int size) {
List<Car> list = new ArrayList<Car>();
for(int i = 0 ; i < size ; i++) {
list.add(new Car(getRandomId(), getRandomBrand(), getRandomYear(), getRandomColor(), getRandomPrice(), getRandomSoldState()));
}
return list;
}
private String getRandomId() {
return UUID.randomUUID().toString().substring(0, 8);
}
private int getRandomYear() {
return (int) (Math.random() * 50 + 1960);
}
private String getRandomColor() {
return colors[(int) (Math.random() * 10)];
}
private String getRandomBrand() {
return brands[(int) (Math.random() * 10)];
}
public int getRandomPrice() {
return (int) (Math.random() * 100000);
}
public boolean getRandomSoldState() {
return (Math.random() > 0.5) ? true: false;
}
public List<String> getColors() {
return Arrays.asList(colors);
}
public List<String> getBrands() {
return Arrays.asList(brands);
}
}
Car.java
Code: Select all
package org.test.pf;
public class Car {
private String id;
private String brand;
private int year;
private String color;
private int price;
private boolean soldState;
public Car(String id, String brand, int year, String color, int price, boolean soldState) {
super();
this.id = id;
this.brand = brand;
this.year = year;
this.color = color;
this.price = price;
this.soldState = soldState;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean getSoldState() {
return soldState;
}
public void setSoldState(boolean soldState) {
this.soldState = soldState;
}
}
dialogEdit.xhtml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui" lang="en">
<h:head>
<title>Edit</title>
</h:head>
<h:body>
<h:form id="myForm">
<p:dialog header="Edit" widgetVar="dlg1" width="200" height="200"
id="myDialog" closable="true">
<h:outputText value="#{dtBasicView.selectedCar.brand}" />
<p:selectOneMenu id="console"
value="#{dtBasicView.selectedCar.brand}" style="width:125px">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{carService.brands}" />
<p:ajax/>
</p:selectOneMenu>
</p:dialog>
</h:form>
</h:body>
</html>
when i open the dialog and change the brand value (without submitting), if i close the dialog and reopen it it keeps the changed value and not the one from the dataTable row , how can i avoid this behavior ? i need to use <p:ajax/> tag to update another component in the dialog.