I have created a complexe dataTable for my DiplomeBean and it shows Correctly
The deal is whene i select a row from the list ,nothing happend
I want whene i select a row it shows me the elements of the diplome.
My Bean
package com.beans;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.bo.DiplomeBo;
import com.converter.DiplomeDataModel;
import com.model.Collaborateur;
import com.model.Diplome;
public class DiplomeBean {
public Integer idDiplome;
public String ecole;
public String typeEcole;
public String typeDiplome;
public Integer promotion;
private Set<Collaborateur> collaborateurs = new HashSet<Collaborateur>(0);
public Diplome selectedDiplome;
public Diplome getSelectedDiplome() {
return selectedDiplome;
}
public void setSelectedDiplome(Diplome selectedDiplome) {
this.selectedDiplome = selectedDiplome;
}
public Integer getIdDiplome() {
return idDiplome;
}
public void setIdDiplome(Integer idDiplome) {
this.idDiplome = idDiplome;
}
private DiplomeBo diplomeBo;
public String getEcole() {
return ecole;
}
public void setEcole(String ecole) {
this.ecole = ecole;
}
public String getTypeEcole() {
return typeEcole;
}
public void setTypeEcole(String typeEcole) {
this.typeEcole = typeEcole;
}
public Integer getPromotion() {
return promotion;
}
public void setPromotion(Integer promotion) {
this.promotion = promotion;
}
public Set<Collaborateur> getCollaborateurs() {
return collaborateurs;
}
public void setCollaborateurs(Set<Collaborateur> collaborateurs) {
this.collaborateurs = collaborateurs;
}
public void setDiplomeBo(DiplomeBo diplomeBo) {
this.diplomeBo = diplomeBo;
}
public String getTypeDiplome() {
return typeDiplome;
}
public void setTypeDiplome(String typeDiplome) {
this.typeDiplome = typeDiplome;
}
public String AddDiplome(){
Diplome diplome =new Diplome();
diplome.setEcole(getEcole());
diplome.setPromotion(getPromotion());
diplome.setTypeDiplome(getTypeDiplome());
diplome.setTypeEcole(getTypeEcole());
diplomeBo.addDiplome(diplome);
clearForm();
return "Ajout Bien Fait !!";
}
public List<Diplome> getAllDiplome(){
return diplomeBo.findAllDiplome();
}
private void clearForm(){
this.setEcole("");
this.setPromotion(0);
this.setTypeEcole("Choisir type..");
this.setTypeEcole("Choisir type..");
}
}
My 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: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>
<body>
<f:view>
<h:outputLink value="Admin/default.xhtml">Go to your app</h:outputLink>
<h:form>
<p:dataTable value="#{diplome.getAllDiplome()}" var="d" paginator="true" rows="10" rowKey="diplome.idDiplome"
selection="#{diplome.selectedDiplome}" selectionMode="single"
onRowSelectUpdate="display" onRowSelectComplete="diplomeDialog.show()">
<f:facet name="header">
Diplome Liste
</f:facet>
<p:column sortBy="#{d.idDiplome}" filterBy="#{d.idDiplome}">
<f:facet name="header">
<h:outputText value="Model" />
</f:facet>
<h:outputText value="#{d.idDiplome}" />
</p:column>
<p:column sortBy="#{d.ecole}" filterBy="#{d.ecole}">
<f:facet name="header">
<h:outputText value="Ecole" />
</f:facet>
<h:outputText value="#{d.ecole}" />
</p:column>
</p:dataTable>
<p:dialog header="Diplome Detail" widgetVar="diplomeDialog" resizable="false"
width="200" showEffect="explode" hideEffect="explode">
<h:outputText value="id:" />
<h:outputText value="#{diplome.selectedDiplome.idDiplome}" />
<h:outputText value="Ecole:" />
<h:outputText value="#{diplome.selectedDiplome.ecole}" />
</p:dialog>
</h:form>
</f:view>
</body>
</html>
Thank's :)
Perhaps try modifying it slightly to be more like the Primefaces showcase example. For example, try something like this:
...
<h:form id="form">
...
<p:dataTable value="#{diplome.getAllDiplome()}" var="d" paginator="true" rows="10"
rowKey="diplome.idDiplome" selection="#{diplome.selectedDiplome}" selectionMode="single">
<p:ajax event="rowSelect" update=":form:display" oncomplete="PF('diplomeDialog').show()" />
...
</p:dataTable>
...
<p:dialog header="Diplome Detail" widgetVar="diplomeDialog" resizable="false"
width="200" showEffect="explode" hideEffect="explode">
<h:panelGrid id="display">
...
</h:panelGrid>
</p:dialog>
</h:form>
...
I can't test this right now, but it seems like it should work since it is essentially the same as the showcase's example.
Related
The problem is that overridden method load() in my LazyDataModel implementation is being called twice every time I try to filter the table.
My LazyDataModel implementation:
public class LazyPostDataModel extends LazyDataModel<Post> {
private List<Post> data;
private final PostService postService;
public LazyPostDataModel(PostService postService) {
this.postService = postService;
data = new ArrayList<>();
}
#Override
public Post getRowData(String rowKey) {
Long id = Long.valueOf(rowKey);
for (Post p : data) {
if (p.getId().equals(id))
return p;
}
return null;
}
#Override
public Object getRowKey(Post post) {
return post.getId();
}
#Override
public List<Post> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
PostFilter filter = new PostFilter(filters, first, pageSize);
FilteredDataModel<Post> postDataModel = postService.findFilteredList(filter);
data = postDataModel.getData();
this.setRowCount(postDataModel.getCount().intValue());
return data;
}
}
My xhtml View:
<?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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition>
<p:panel header="Posts to Review">
<ui:include src="ModerationPostContextMenu.xhtml" />
<p:dataTable id="ModerationPostTable" value="# {moderationPostController.posts}" var="post" lazy="true" rows="25" widgetVar="ModerationPostTable"
paginator="true" rowKey="#{post.id}" selectionMode="single" selection="#{moderationPostController.selected}"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" >
<p:ajax event="rowSelect" update="#form:contextMenu" />
<p:ajax event="rowUnselect" update="#form:contextMenu" />
<p:column headerText="Id">
<h:outputText value="#{post.id}"/>
</p:column>
<p:column headerText="Creation Date">
<h:outputText value="#{post.creationDate}"/>
</p:column>
<p:column headerText="Author Id">
<h:outputText value="#{post.author.id}"/>
</p:column>
<p:column headerText="Author Nickname">
<h:outputText value="#{post.author.nickname}"/>
</p:column>
<p:column headerText="Text">
<h:outputText value="#{post.text}" />
</p:column>
<p:column headerText="Media" width="200">
<p:graphicImage value="#{mediaController.buildUrl(post.media)}"
width="200" height="200"/>
</p:column>
<p:column headerText="Status" filterBy="#{post.moderationApproved}">
<f:facet name="filter">
<p:selectOneButton onchange="PF('ModerationPostTable').filter()">
<f:converter converterId="javax.faces.Boolean" />
<f:selectItem itemLabel="Approved" itemValue="true" />
<f:selectItem itemLabel="Rejected" itemValue="false" />
<f:selectItem itemLabel="Review" itemValue="" />
</p:selectOneButton>
</f:facet>
<h:outputText value="#{post.moderationApproved}" />
</p:column>
</p:dataTable>
</p:panel>
</ui:composition>
</html>
And the controller
#Named("moderationPostController")
#SessionScoped
#Transactional(Transactional.TxType.REQUIRED)
public class ModeratioPostController extends BaseController implements Serializable {
#Inject PostService postService;
private LazyPostDataModel posts;
// Selection
private Post selected;
#Override
protected void initSpecific() {
posts = new LazyPostDataModel(postService);
}
// Actions
public void approve() {
if (selected == null) return;
Post post = postService.find(selected.getId());
post.setModerationApproved(Boolean.TRUE);
postService.edit(post);
}
public void reject() {
if (selected == null) return;
Post post = postService.find(selected.getId());
post.setModerationApproved(Boolean.FALSE);
postService.edit(post);
}
public LazyPostDataModel getPosts() {
return posts;
}
public void setPosts(LazyPostDataModel posts) {
this.posts = posts;
}
public Post getSelected() {
return selected;
}
public void setSelected(Post selected) {
this.selected = selected;
}
}
this is because you have
filterBy="#{post.moderationApproved}"
and
<p:selectOneButton onchange="PF('ModerationPostTable').filter()">
both of these will call the filter once and cause the load method to be called twice.
Use one of them only.
Hi trying to call the method on button click, but it doesnt work i also tried onclick instead action it also doesnt work. i couldnt understand where is the mistake please help me
here is my managed bean
package com.primefaces.managedbean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import com.primefaces.domain.KenLocation;
import com.primefaces.service.ILocationService;
#ManagedBean(name = "locationbean")
#ViewScoped
public class LocationBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1101264226039168006L;
/**
*
*/
#ManagedProperty(value = "#{LocationService}")
private ILocationService locationService;
private List<KenLocation> locationList = new ArrayList<KenLocation>();
KenLocation kenLocation;
private String locationName;
private String address1;
private String address2;
private String city;
private String pincode;
private String state;
private String contactNo;
private String organisationName;
#PostConstruct
private void init() {
locationList = locationService.onLoad();
}
public void addnewlocation() {
System.out.println("I am inside newlocation");
kenLocation = new KenLocation();
kenLocation.setLocationName(locationName);
kenLocation.setAddress1(address1);
kenLocation.setAddress2(address2);
kenLocation.setCity(city);
kenLocation.setPincode(Integer.parseInt(pincode));
kenLocation.setPhone(contactNo);
System.out.println(kenLocation);
}
public String getLocationName() {
return locationName;
}
public void setLocationName(String locationName) {
this.locationName = locationName;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getOrganisationName() {
return organisationName;
}
public void setOrganisationName(String organisationName) {
this.organisationName = organisationName;
}
public ILocationService getLocationService() {
return locationService;
}
public void setLocationService(ILocationService locationservice) {
this.locationService = locationservice;
}
public List<KenLocation> getLocationList() {
return locationList;
}
public void setLocationList(List<KenLocation> a_locationList) {
locationList = a_locationList;
}
}
and JSF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:composition 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"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h5>Product List {9 Products can be added per mail}</h5>
<p:separator></p:separator>
<p:growl id="growlLocationtable"></p:growl>
<p:commandButton id="new_location" title="CreateLocation" type="button"
icon="ui-icon-document" onclick="PF('dlg1').show()" />
<p:commandButton id="edit_location" title="EditLocation"
icon=" ui-icon-pencil" />
<p:commandButton id="delete_location" title="DeleteLocation"
icon="ui-icon-trash" />
<h:form id="Locationdataform">
<h:panelGrid columns="2" id="Locationdatatablepanel" resizable="false"
size="600">
<p:dataTable id="Locationdatatable" var="odt"
value="#{locationbean.locationList}">
<p:column headerText="Location Name">
<h:outputText value="#{odt.locationName}" />
</p:column>
<p:column headerText="Address Line1">
<h:outputText value="#{odt.address1}" />
</p:column>
<p:column headerText="Address Line2">
<h:outputText value="#{odt.address2}" />
</p:column>
<p:column headerText="City">
<h:outputText value="#{odt.city}" />
</p:column>
<p:column headerText="Pincode">
<h:outputText value="#{odt.pincode}" />
</p:column>
<p:column headerText="State">
<h:outputText value="#{odt.state}" />
</p:column>
<p:column headerText="Contact No">
<h:outputText value="#{odt.phone}" />
</p:column>
<p:column headerText="Organisation Name">
<h:outputText value="#{odt.ken_Org_ID.name}" />
</p:column>
</p:dataTable>
</h:panelGrid>
</h:form>
<!-- dialog -->
<p:dialog header="Add Location" widgetVar="dlg1" minHeight="40"
modal="true">
<h:form id="addLocationForm">
<h:panelGrid columns="2" id="grid">
<h:outputLabel value="Location Name"></h:outputLabel>
<p:inputText id="txt_Name" value="#{locationbean.locationName}" />
<h:outputLabel value="Address1"></h:outputLabel>
<p:inputText id="txt_address1" value="#{locationbean.address1}" />
<h:outputLabel value="Address2"></h:outputLabel>
<p:inputText id="txt_address2" value="#{locationbean.address2}" />
<h:outputLabel value="City"></h:outputLabel>
<p:inputText id="txt_city" value="#{locationbean.city}" />
<h:outputLabel value="Pincode"></h:outputLabel>
<p:inputText id="txt_pincode" value="#{locationbean.pincode}" />
<h:outputLabel value="State"></h:outputLabel>
<p:inputText id="txt_state" value="#{locationbean.state}" />
<h:outputLabel value="Contactno"></h:outputLabel>
<p:inputText id="txt_contactno" value="#{locationbean.contactNo}" />
<h:outputLabel value="Organistaion name"></h:outputLabel>
<p:inputText id="txt_orgname"
value="#{locationbean.organisationName}" />
<p:commandButton id="addlocatin_btn" value="Save"
action="#{locationbean.addnewlocation}" type="submit" />
</h:panelGrid>
</h:form>
</p:dialog>
The Data that I have entered in the input text field of the prime faces data table is not displayed in the dialog box JSF file and Data is not displayed in the dialog box
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add/Remove Row</title>
</h:head>
<body>
<h:form id="form">
<p:dataTable id="buss" value="#{busBean.mediumBusModel}" var="bus"
selection="#{busBean.selectedBus}" selectionMode="single" >
<p:column headerText="Brand" style="width:5%">
<p:inputText value="#{bus.brand}" />
</p:column>
<p:column headerText="Model" style="width:5%">
<p:inputText value="#{bus.model}" />
</p:column>
<p:column headerText="Action" style="width:10%">
<p:commandButton actionListener="#{busBean.addBus()}" icon="ui-icon-plus" update=":form" ajax="false" title="add"/>
<p:commandButton actionListener="#{busBean.removeBus(bus)}" icon="ui-icon-minus" update=":form" ajax="false" title="remove"/>
</p:column>
<f:facet name="footer">
<p:commandButton id="viewButton" value="View" icon="ui-icon-search"
update=":form:displayBus" oncomplete="PF('singleBusDialog').show()"/>
</f:facet>
</p:dataTable>
<br/>
<p:dialog id="dialog" header="Bus Detail" widgetVar="singleBusDialog" resizable="false"
showEffect="fade" hideEffect="explode">
<h:panelGrid id="displayBus" columns="2" cellpadding="4">
<h:outputText value="Brand:" />
<h:outputText value="#{bus}" style="font-weight:bold"/>
<h:outputText value="Model:" />
<h:outputText value="#{bus}" style="font-weight:bold"/>
</h:panelGrid>
</p:dialog>
</h:form>
</body>
</html>
Below is the bean class that I have used
package com.bus.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import com.bus.bean.Bus;
#ManagedBean
#SessionScoped
public class BusBean implements Serializable {
private List<Bus> busList;
private Bus selectedBus;
private BusDataModel mediumBusModel;
public BusBean() {
busList=new ArrayList<Bus>();
busList.add(new Bus("a","b"));
mediumBusModel = new BusDataModel(busList);
//populateRandomCars(cars, 50);
}
public List<Bus> getBusList()
{
return busList;
}
public void setBusList(List<Bus> busList) {
this.busList = busList;
}
public void addBus()
{
Bus newBus=new Bus();
busList.add(newBus);
}
public Bus getSelectedBus() {
return selectedBus;
}
public void setSelectedBus(Bus selectedBus) {
this.selectedBus = selectedBus;
}
public void removeBus(Bus bus)
{
busList.remove(bus);
}
public BusDataModel getMediumBusModel() {
return mediumBusModel;
}
}
-------------------------------------------------------
package com.bus.bean;
import java.util.List;
import javax.faces.model.ListDataModel;
import com.bus.bean.Bus;
import org.primefaces.model.SelectableDataModel;
public class BusDataModel extends ListDataModel<Bus> implements SelectableDataModel<Bus> {
public BusDataModel() {
}
public BusDataModel(List<Bus> data) {
super(data);
}
#Override
public Bus getRowData(String rowKey) {
//In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data
List<Bus> buss = (List<Bus>) getWrappedData();
for(Bus bus : buss) {
if(bus.getModel().equals(rowKey))
return bus;
}
return null;
}
#Override
public Object getRowKey(Bus bus) {
return bus.getBrand();
}
}
-----------------------------------------------------------------------------------------------/
package com.bus.bean;
import java.io.Serializable;
public class Bus implements Serializable
{
public String model;
public String brand;
public Bus()
{
}
public Bus(String Model,String Brand)
{
this.model = model;
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getBrand() {
System.out.println("in set:"+brand);
return brand;
}
public void setBrand(String brand) {
System.out.println("in set:"+brand);
this.brand = brand;
}
#Override
public boolean equals(Object obj) {
if(obj == null)
return false;
if(!(obj instanceof Bus))
return false;
Bus compare = (Bus) obj;
return compare.model.equals(this.model);
}
#Override
public int hashCode() {
int hash = 1;
return hash * 31 + model.hashCode();
}
#Override
public String toString() {
return "Bus{" + "model=" + model + ", brand=" + brand + '}';
}
enter code here
}
Please let me know how to get the data that I have entered in the text field of Primefaces data table to the dialog box
You should use the selected value from the datatable.
selection="#{busBean.selectedBus}"
So , in your dialog box
<h:outputText value="Brand:" />
<h:outputText value="#{busBean.selectedBus.brand}" />
Also,
<p:commandButton id="viewButton" value="View" icon="ui-icon-search"
update=":form:displayBus,:form:dialog" oncomplete="PF('singleBusDialog').show()"/>
Updating the dialog box with value of the selected row.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add/Remove Row</title>
</h:head>
<body>
<h:form id="form">
<p:growl id="messages" showDetail="true"/>
<p:contextMenu for="buss">
<p:menuitem value="Edit Cell" icon="ui-icon-search" onclick="PF('busTable').showCellEditor();return false; "/>
</p:contextMenu>
<p:dataTable id="buss" var="bus" value="#{busBean.busModel}" editable="true" editMode="cell" widgetVar="busTable"
selection="#{busBean.selectedBus}">
<p:ajax event="cellEdit" listener="#{busBean.onCellEdit}" update=":form:messages" />
<p:column selectionMode="single" style="width:2%" />
<p:column headerText="Brand" style="width:25%">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{bus.brand}" /></f:facet>
<f:facet name="input"><p:inputText id="brandInput" value="#{bus.brand}" style="width:96%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Model" style="width:25%">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{bus.model}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput" value="#{bus.model}" style="width:96%"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Action" style="width:10%">
<p:commandButton actionListener="#{busBean.addBus()}" icon="ui-icon-plus" update=":form" ajax="false" title="add"/>
<p:commandButton actionListener="#{busBean.removeBus(bus)}" icon="ui-icon-minus" update=":form" ajax="false" title="remove"/>
</p:column>
<f:facet name="footer">
<p:commandButton id="viewButton" value="View" icon="ui-icon-search"
update=":form:buss,:form:displayBus,:form:dialog" oncomplete="PF('singleBusDialog').show()"/>
</f:facet>
</p:dataTable>
<br/>
<p:dialog id="dialog" header="Bus Detail" widgetVar="singleBusDialog" resizable="false"
showEffect="fade" hideEffect="explode">
<h:panelGrid id="displayBus" columns="2" cellpadding="4">
<h:outputText value="Brand:" />
<h:outputText value="#{busBean.selectedBus.brand}" style="font-weight:bold"/>
<h:outputText value="Model:" />
<h:outputText value="#{busBean.selectedBus.model}"/>
I have a problem that i can't solve. I have my xhtml page:
The problem is that i have a form where i insert the name and select the location of a travel. It renders an external panel "Volo" where the selectionMenu is updated with some values got from DB based on location selected. Next i want to get a value from Volo selection and update the another panel with ID="HE"(is the one called HotelEsc).
I'm in trouble with the selectionMenu in "Volo" because i can't get the value into it. Always gives an NullPointException error. Probably the problem is that i'm trying to understand how Update and render work and i am mistaking something about these operations. Hope on your help thank you.
Code of xhtml:
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Add a Default Package</title>
</h:head>
<h:body>
<h:form id="form">
<p:panel header="DefaultPackage Form">
<h:panelGrid columns="3" id="regGrid">
<h:outputLabel for="Name">Name:</h:outputLabel>
<p:inputText id="Name"
value="#{addDefaultPackageBean.defpackDTO.name}" />
<p:message for="Name" />
<h:outputLabel for="location">Locations Available:</h:outputLabel>
<h:selectOneMenu for="location"
value="#{addDefaultPackageBean.defpackDTO.location}">
<f:ajax listener="#{addDefaultPackageBean.Search()}" render="Volo" />
<f:selectItems id="location"
value="#{addDefaultPackageBean.availableLocations}" />
</h:selectOneMenu>
</h:panelGrid>
</p:panel>
<p:panel header="Voli Disponibili per la location selezionata"
id="Volo">
<h:outputLabel for="Fly">Volo:</h:outputLabel>
<h:selectOneMenu for="Fly" value="#{addDefaultPackageBean.fly}">
<f:selectItems id="Fly" value="#{addDefaultPackageBean.elelisfly}"
var="ElementDTO" itemValue="#{ElementDTO.name}"
itemLabel="#{ElementDTO.name}" />
</h:selectOneMenu>
<p:commandButton action="#{addDefaultPackageBean.sel()}" value="HE" render=":form,:form:regularGrid,:form:Volo" update=":form:Volo" process="form:regGrid,#this"></p:commandButton>
</p:panel>
<p:panel header="HotelEsc" id="HotelEscursioni">
<h:panelGrid columns="3" id="regularGrid">
<h:outputLabel for="Hotel">Hotel:</h:outputLabel>
<h:selectOneMenu for="Hotel" value="#{addDefaultPackageBean.hotel}">
<f:selectItems id="Hotel"
value="#{addDefaultPackageBean.elelishotel}" var="ElementDTO"
itemValue="#{ElementDTO.name}" itemLabel="#{ElementDTO.name}" />
</h:selectOneMenu>
<p:message for="Hotel" />
<h:outputLabel for="Escursion">Escursioni:</h:outputLabel>
<f:facet name="header">Clicca su view per vedere i dettagli</f:facet>
<p:dataTable id="Escursion" var="esc"
value="#{addDefaultPackageBean.elelisescursion}"
rowKey="#{esc.name}"
selection="#{addDefaultPackageBean.selectedEscursions}"
selectionMode="multiple">
<p:column headerText="Nome"> #{esc.name} </p:column>
<p:column headerText="Costo"> #{esc.cost} </p:column>
<p:column headerText="Data Iniziale"> #{esc.startingDate} </p:column>
<p:column headerText="Data Fine"> #{esc.endingDate} </p:column>
<f:facet name="footer">
</f:facet>
</p:dataTable>
</h:panelGrid>
</p:panel>
</h:form>
</h:body>
</html>
Code of related bean:
package beans;
import java.awt.Event;
import java.io.Serializable;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.view.ViewScoped;
import elementManagement.ElementMgr;
import elementManagementDTO.ElementDTO;
import DefaultPackageManagement.DefaultPackageMgr;
import DefaultPackageManagementDTO.DefaultPackageDTO;
#ManagedBean(name="addDefaultPackageBean") //come viene richiamato
#ViewScoped
public class AddDefaultPackageBean{
/**
*
*/
#EJB
private DefaultPackageMgr defpackMgr;
private DefaultPackageDTO defpackDTO;
private ArrayList<ElementDTO> elelisfly;
private ArrayList<ElementDTO> elelishotel;
private ArrayList<ElementDTO> elelisescursion;
private ArrayList<ElementDTO> elelis;
private ElementDTO[] selectedEscursions;
private String fly;
private String hotel;
private boolean flag=true;
private boolean flagdopo=true;
private ArrayList<String> availableLocations;
private ElementDTO flyElem;
#EJB
private ElementMgr elemMgr;
public ElementDTO[] getSelectedEscursions() {
return selectedEscursions;
}
public void setSelectedEscursions(ElementDTO[] selectedEscursions) {
this.selectedEscursions = selectedEscursions;
}
public AddDefaultPackageBean() {
defpackDTO = new DefaultPackageDTO();
}
#PostConstruct
public void init()
{
this.elelisfly=new ArrayList<ElementDTO>();
this.elelishotel=new ArrayList<ElementDTO>();
this.elelisescursion=new ArrayList<ElementDTO>();
this.setElelis(elemMgr.getAllElements());
this.availableLocations=new ArrayList<String>();
this.flyElem=new ElementDTO();
for(ElementDTO e:elelis)
{
if (this.availableLocations.contains(e.getLocation())==false)
{
this.availableLocations.add(e.getLocation());
}
}
}
public String add() {
this.AssignElemFlyFromSelection();
this.AssignElemHotelFromSelection();
this.AssignElemEscursionFromSelection();
defpackMgr.save(defpackDTO);
return "/employee/index?faces-redirect=true";
}
public void sel()
{
System.out.print("ehila" );
this.setElelis(this.elemMgr.getAllElementsByLocation(this.defpackDTO.getLocation()));
for(ElementDTO e:elelis)
{
System.out.print("elemento della location Haiti "+e.getName());
}
this.AssignElemFlyFromSelection();
System.out.print(this.fly+"Il volo selezionato per la location รจ "+this.getFlyElem().getName() );
this.elelisescursion.clear();
this.elelishotel.clear();
for(ElementDTO e:elelis)
{
if(e.getType().equals("Hotel"))
{
System.out.print("ho un hotel tra gli elementi "+e.getName() );
if(e.getStartingDate().after(this.flyElem.getStartingDate())&&((e.getEndingDate().before(this.flyElem.getEndingDate()))))
{
System.out.print("ho un hotel tra gli elementi con le date giuste"+e.getName());
this.getElelishotel().add(e);
}
}
else
{
if(e.getType().equals("Escursion"))
{
if(e.getStartingDate().after(this.flyElem.getStartingDate())&&(e.getEndingDate().before(this.flyElem.getEndingDate())))
{
this.getElelishotel().add(e);
}
}
}
}
this.setFlag(true);
this.setFlagdopo(true);
}
public DefaultPackageDTO getDefpackDTO() {
return defpackDTO;
}
public void setDefpackDTO(DefaultPackageDTO defpackDTO) {
this.defpackDTO = defpackDTO;
}
public ArrayList<ElementDTO> getElelisfly() {
return elelisfly;
}
public void setElelisfly(ArrayList<ElementDTO> elelisfly) {
this.elelisfly = elelisfly;
}
public ArrayList<ElementDTO> getElelishotel() {
return elelishotel;
}
public void setElelishotel(ArrayList<ElementDTO> elelishotel) {
this.elelishotel = elelishotel;
}
public ArrayList<ElementDTO> getElelisescursion() {
return elelisescursion;
}
public void setElelisescursion(ArrayList<ElementDTO> elelisescursion) {
this.elelisescursion = elelisescursion;
}
public String getFly() {
return fly;
}
public void setFly(String fly) {
this.fly = fly;
}
public String getHotel() {
return hotel;
}
public void setHotel(String hotel) {
this.hotel = hotel;
}
private void AssignElemFlyFromSelection()
{
for (ElementDTO elem:this.elelisfly)
{
if(elem.getName().equals(this.fly))
{
this.flyElem=elem;
}
}
}
private void AssignElemHotelFromSelection()
{
for (ElementDTO elem:this.elelishotel)
{
if(elem.getName().equals(this.hotel))
{
this.defpackDTO.getElem().add(elem);
}
}
}
private void AssignElemEscursionFromSelection()
{
for(int i=0;i<selectedEscursions.length;i++)
{
this.defpackDTO.getElem().add(selectedEscursions[i]);
}
}
public void Search(){
String s=defpackDTO.getLocation();
System.out.print("luogo scelto "+s);
this.setElelis(this.elemMgr.getAllElementsByLocation(s));
for(ElementDTO e:elelis)
{
System.out.print("aggiungo volo "+e.getName());
if(e.getType().equals("Flight"))
{
this.getElelisfly().add(e);
System.out.print("aggiungo volo "+e.getName());
}
}
this.setFlag(true);
}
public ArrayList<ElementDTO> getElelis() {
return elelis;
}
public void setElelis(ArrayList<ElementDTO> elelis) {
this.elelis = elelis;
}
public ArrayList<String> getAvailableLocations() {
return availableLocations;
}
public void setAvailableLocations(ArrayList<String> availableLocations) {
this.availableLocations = availableLocations;
}
public Boolean getFlag() {
return flag;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
public boolean isFlagdopo() {
return flagdopo;
}
public void setFlagdopo(boolean flagdopo) {
this.flagdopo = flagdopo;
}
public ElementDTO getFlyElem() {
return flyElem;
}
public void setFlyElem(ElementDTO flyElem) {
this.flyElem = flyElem;
}
}
You are somehow misunderstood the use of some attributes.
I'll post my notes on your code.
The p:commandButton inside Volo section is confusing
it doesn't have a render attribute, it's the update instead, you should remove render.
The update you have is updating the current section Volo is that what you need ?
it's processing only the first section which is regGrid, which means that the values inside Volo section won't be updated to the model inside the managedBean (causing the NullPointer), is that what you want ? don't think so.
Your "HE" button should be link this (If I understood what you want correctly)
<p:commandButton action="#{addDefaultPackageBean.sel()}"
value="HE"
update="HotelEscursioni" process="#parent">
</p:commandButton>
Hope this Helps...
After lots of attempts, i understood how render/update works. I hope the solution with the xhtml here down helps other people with the same problem:
The updates have to be done on the components that you want to refresh(for example in my situation the targets of the updates given from chosen values in selections); process has to be used to specificate which components are used to implement the action.
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Add a Default Package</title>
</h:head>
<h:body>
<h:form id="form">
<p:outputLabel for="Name">Name:</p:outputLabel>
<p:inputText id="Name"
value="#{addDefaultPackageBean.defpackDTO.name}" />
<p:message for="Name" />
<p:outputLabel for="Location">Locations Available:</p:outputLabel>
<p:selectOneMenu id="Location"
value="#{addDefaultPackageBean.defpackDTO.location}">
<f:selectItems
value="#{addDefaultPackageBean.availableLocations}" />
</p:selectOneMenu>
<p:commandButton action="#{addDefaultPackageBean.Search()}" value="Ciao" render=":form:Volo" update=":form:Volo :form"></p:commandButton>
<p:panel header="Voli Disponibili per la location selezionata"
id="Volo" rendered="#{addDefaultPackageBean.flag}">
<h:panelGrid columns="3" id="voloGrid">
<p:outputLabel for="Volare">Volo:</p:outputLabel>
<p:selectOneMenu for="Volare" value="#{addDefaultPackageBean.fly}">
<f:selectItems id="Volare" value="#{addDefaultPackageBean.elelisfly}"
var="Ciao" itemValue="#{Ciao.name}"
itemLabel="#{Ciao.name}" />
</p:selectOneMenu>
<p:commandButton actionListener="#{addDefaultPackageBean.sel()}" value="hesef" update=":form:voloGrid :form:HotelEscursioni" process=":form:Location,:form:voloGrid,#this"/>
</h:panelGrid>
</p:panel>
<p:panel header="HotelEscurs" id="HotelEscursioni" rendered="#{addDefaultPackageBean.flagdopo}">
<p:outputLabel for="Hotel">Hotel:</p:outputLabel>
<p:selectOneMenu for="Hotel" value="#{addDefaultPackageBean.hotel}">
<f:selectItems id="Hotel"
value="#{addDefaultPackageBean.elelishotel}" var="ElementDTO"
itemValue="#{ElementDTO.name}" itemLabel="#{ElementDTO.name}" />
</p:selectOneMenu>
<p:message for="Hotel" />
<p:dataTable id="Escursion" var="esc"
value="#{addDefaultPackageBean.elelisescursion}"
rowKey="#{esc.name}"
selection="#{addDefaultPackageBean.selectedEscursions}"
selectionMode="multiple">
<p:column headerText="Nome"> #{esc.name} </p:column>
<p:column headerText="Costo"> #{esc.cost} </p:column>
<p:column headerText="Data Iniziale"> #{esc.startingDate} </p:column>
<p:column headerText="Data Fine"> #{esc.endingDate} </p:column>
</p:dataTable>
</p:panel>
</h:form>
</h:body>
</html>
I have a little probleme with primefaces dataTable,
i have set a commandButton inside my table inorder to delete data from the table
but whene i choose to delete a row i found that it had deleted 2 rows not just one
this is my Bean
package com.beans;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.RowEditEvent;
import com.bo.DiplomeBo;
import com.converter.DiplomeDataModel;
import com.model.Collaborateur;
import com.model.Diplome;
public class DiplomeBean {
public Integer idDiplome;
public String ecole;
public String typeEcole;
public String typeDiplome;
public Integer promotion;
private Set<Collaborateur> collaborateurs = new HashSet<Collaborateur>(0);
public Diplome selectedDiplome;
private DiplomeBo diplomeBo;
public Diplome getSelectedDiplome() {
return selectedDiplome;
}
public void setSelectedDiplome(Diplome selectedDiplome) {
this.selectedDiplome = selectedDiplome;
}
public Integer getIdDiplome() {
return idDiplome;
}
public void setIdDiplome(Integer idDiplome) {
this.idDiplome = idDiplome;
}
public String getEcole() {
return ecole;
}
public void setEcole(String ecole) {
this.ecole = ecole;
}
public String getTypeEcole() {
return typeEcole;
}
public void setTypeEcole(String typeEcole) {
this.typeEcole = typeEcole;
}
public Integer getPromotion() {
return promotion;
}
public void setPromotion(Integer promotion) {
this.promotion = promotion;
}
public Set<Collaborateur> getCollaborateurs() {
return collaborateurs;
}
public void setCollaborateurs(Set<Collaborateur> collaborateurs) {
this.collaborateurs = collaborateurs;
}
public void setDiplomeBo(DiplomeBo diplomeBo) {
this.diplomeBo = diplomeBo;
}
public String getTypeDiplome() {
return typeDiplome;
}
public void setTypeDiplome(String typeDiplome) {
this.typeDiplome = typeDiplome;
}
public String AddDiplome() {
Diplome diplome = new Diplome();
diplome.setEcole(getEcole());
diplome.setPromotion(getPromotion());
diplome.setTypeDiplome(getTypeDiplome());
diplome.setTypeEcole(getTypeEcole());
diplomeBo.addDiplome(diplome);
clearForm();
return "Ajout Bien Fait !!";
}
public String UpdateDiplome(Diplome dip) {
diplomeBo.updateDiplome(dip);
return "Update Bien Fait";
}
public String DeleteDiplome(Diplome diplome){
diplomeBo.deleteDiplome(diplome);
clearForm();
return "";
}
public List<Diplome> getAllDiplome() {
return diplomeBo.findAllDiplome();
}
private void clearForm() {
this.setEcole("");
this.setPromotion(0);
this.setTypeEcole("");
this.setTypeEcole("");
}
public void onEdit(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Diplome Edited");
Integer i = ((Diplome) (event.getObject())).getIdDiplome();
Diplome d = new Diplome();
d.setIdDiplome(i);
d.setEcole(this.getEcole());
this.UpdateDiplome(d);
FacesContext.getCurrentInstance().addMessage(null, msg);
clearForm();
}
public void onCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage("Diplome Cancelled");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
this is my 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: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>
<body>
<f:view>
<h:outputLink value="Admin/default.xhtml">Go to your app</h:outputLink>
<h:form id="form">
<p:growl id="messages" showDetail="true" />
<p:dataTable var="dip" value="#{diplome.getAllDiplome()}" id="diplomeList"
editable="true">
<f:facet name="header">
In-Cell Editing
</f:facet>
<p:ajax event="rowEdit" listener="#{diplome.onEdit}"
/>
<p:ajax event="rowEditCancel" listener="#{diplome.onCancel}"
/>
<p:column headerText="ID" style="width:10%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{dip.idDiplome}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{dip.idDiplome}#{diplome.idDiplome}" style="width:100%" readonly="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Ecole" style="width:20%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{dip.ecole}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{diplome.ecole}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:6%">
<p:rowEditor />
</p:column>
<p:column>
<p:commandButton icon="ui-icon-trash"
title="Delete this actor"
action="#{diplome.DeleteDiplome(dip)}"
ajax="false" />
</p:column>
</p:dataTable>
</h:form>
</f:view>
</body>
</html>