Im new to JSF/Primefaces. I already have searched for answers several weeks and could not find nothing.
Im developing an Inventory System (customized for an IT Department).
I have several views with the same/related problem, I think:
Issue # 1: When I submit a new value via a form with one inpuText and one selectOneMenu and the datatable get refreshed, the new value is added the first time, but when I select another item en submit the values again it adds the previous value of the selectOneMenu. Note: Only in the DataTable, since in the data base the correct record is saved.
I have tried changing the ViewScoped to RequestScope, it appears to be solved, but now, when I try to edit a row it adds it as a new value instead. so that the Datatable shows the row I tried to edit and the edited as a new one.
See the image:
JSF Page: Subcategory View
<h:form id="subcategory">
<p:growl id="message" autoUpdate="true" showDetail="true"/>
<p:fieldset id="subcategoryFS" legend="New Sub-Category" toggleable="true" collapsed="true">
<h:panelGrid columns="1" cellpadding="2">
<p:selectOneMenu id="cboCategory"
value="#{subcategoryController.category.categoryId}"
style="width: 100%">
<f:selectItem itemLabel="Select Category" itemValue="" noSelectionOption="true"/>
<f:selectItems value="#{subcategoryController.categoryList}"
var="subcat"
itemLabel="#{subcat.categoryName}"
itemValue="#{subcat.categoryId}"/>
<p:ajax event="change" listener="#{subcategoryController.categoryChange()}"/>
</p:selectOneMenu>
<p:inputText id="Sub_Category_Name"
value="#{subcategoryController.subcategory.subcategoryName}"
required="true"
style="width: 250px;"/>
<p:commandButton id="newSubCatCombtn"
value="Add"
icon="ui-icon-plus"
actionListener="#{subcategoryController.save()}"
style="width: 100%"
update="subcategory, :subCatDTForm:subCatDT"/>
</h:panelGrid>
<p:watermark for="Sub_Category_Name" value="Sub-Category Name" id="Sub_Category_NameWM"/>
</p:fieldset>
</h:form>
<h:form id="subCatDTForm">
<p:contextMenu for="subCatDT">
<p:menuitem value="Edit" icon="ui-icon-pencil"
oncomplete="PF('subCatDialog').show()"
update=":subCatDialogT"/>
<p:menuitem value="Delete"
icon="ui-icon-close"
actionListener="#{subcategoryController.remove()}"
update="subCatDT"/>
</p:contextMenu>
<p:dataTable id="subCatDT" var="subcatDataTable" value="#{subcategoryController.subcategoryList}"
rowKey="#{subcatDataTable.subcategoryId}"
selectionMode="single"
selection="#{subcategoryController.selectedSubcategory}"
emptyMessage="No subcategories recorded yet">
<p:column headerText="ID">
<h:outputText value="#{subcatDataTable.subcategoryId}" />
</p:column>
<p:column headerText="Sub-Category">
<h:outputText value="#{subcatDataTable.subcategoryName}" />
</p:column>
<p:column headerText="Category">
<h:outputText value="#{subcatDataTable.categoryId.categoryName}"/>
</p:column>
</p:dataTable>
</h:form>
Category Bean:
@Named
@ViewScoped
public class CategoryController implements Serializable {
@EJB
CategoryFacadeLocal categoryEJB;
@Inject
private Category category;
@Inject
private Category selectedCategory;
private List<Category> categoryList;
private List<Category> tempcategoryList;
@PostConstruct
public void init() {
categoryList = categoryEJB.findAll();
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public List<Category> getCategoryList() {
return categoryList;
}
public void setCategoryList(List<Category> categoryList) {
this.categoryList = categoryList;
}
public Category getSelectedCategory() {
return selectedCategory;
}
public void setSelectedCategory(Category selectedCategory) {
this.selectedCategory = selectedCategory;
}
public void save() {//Guarda la categoria
try {
tempcategoryList = categoryEJB.findCategoryByName(category); // Carga la lista si esta category ya existe en la base de datos.
if (tempcategoryList.isEmpty()) { // Si la lista esta vacia, guarda la nueva categoria.
categoryEJB.create(category); // Guarda esta categoria en la base de datos.
init(); // Recarga la lista con la base de datos despues de agregar la nueva categoria.
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "Info", "Category: " + category.getCategoryName() + " created"));
category.setCategoryName(""); // limpia los CategoryName en la vista.
category.setCategoryDetail(""); // limpia los CategoryDetail en la vista.
} else {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_WARN, "Info", "Category: " + category.getCategoryName() + " created"));
}
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_FATAL, "Info", "Category: " + category.getCategoryName() + " not created"));
}
}
Issue # 2: Something related is happening when I try to edit a row with a dialog. The selectOneMenu's are not loading the values of the selected row:
See the image:
Dialog to edit the values of a saved category
SubCategory Bean:
@Named
@ViewScoped
public class SubcategoryController implements Serializable {
@EJB
private SubcategoryFacadeLocal EJBsubcategory;
@EJB
private CategoryFacadeLocal EJBcategory;
@Inject
private Subcategory subcategory;
@Inject
private Category category;
@Inject
private Subcategory selectedSubcategory;
@Inject
private Category selectedCategory;
private List<Category> categoryList;
private List<Subcategory> subcategoryList;
@PostConstruct
public void init() {
categoryList = EJBcategory.findAll();
subcategoryList = EJBsubcategory.findAll();
}
public Subcategory getSubcategory() {
return subcategory;
}
public void setSubcategory(Subcategory subcategory) {
this.subcategory = subcategory;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public List<Category> getCategoryList() {
return categoryList;
}
public void setCategoryList(List<Category> categoryList) {
this.categoryList = categoryList;
}
public List<Subcategory> getSubcategoryList() {
return subcategoryList;
}
public void setSubcategoryList(List<Subcategory> subcategoryList) {
this.subcategoryList = subcategoryList;
}
public Subcategory getSelectedSubcategory() {
return selectedSubcategory;
}
public void setSelectedSubcategory(Subcategory selectedSubcategory) {
this.selectedSubcategory = selectedSubcategory;
}
public Category getSelectedCategory() {
return selectedCategory;
}
public void setSelectedCategory(Category selectedCategory) {
this.selectedCategory = selectedCategory;
}
public void save() {
try {
subcategory.setCategoryId(category);
EJBsubcategory.create(subcategory);
this.init();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "Info", "Subcategory: " + subcategory.getSubcategoryName() + " saved"));
subcategory.setSubcategoryName("");
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_FATAL, "Warning", "Subcategory: " + subcategory.getSubcategoryName() + " not saved"));
}
}
public void edit() {
try {
selectedSubcategory.setCategoryId(selectedCategory);
EJBsubcategory.edit(selectedSubcategory);
this.init();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "Info", "Subcategory: " + selectedSubcategory.getSubcategoryName() + " updated"));
selectedSubcategory.setSubcategoryName("");
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_FATAL, "Warning", "Subcategory: " + selectedSubcategory.getSubcategoryName() + " not updated"));
}
}
public void remove() {
try {
EJBsubcategory.remove(selectedSubcategory);
this.init();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "Info", "Subcategory: " + selectedSubcategory.getSubcategoryName() + " deleted"));
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_FATAL, "Warning", "Subcategory: " + selectedSubcategory.getSubcategoryName() + " not deleted"));
}
}
These issues are happeing in every View of the web app. I would appreciate if you guys could help me. What am I doing wrong? Many Thanks in advance.