p:columns inside ui:repeat is not working - jsf

I'm working on a project that requires a dataTable for each item of a collection, and each of those dataTables has dynamic columns.
Here's an example of what I want to do..
DocsDescriptor.java
package test;
public class DocsDescriptor {
private long id;
private String name;
public DocsDescriptor(long id, String name){
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DocsData.java
package test;
public class DocsData {
private long id;
private String value;
private DocsDescriptor descriptor;
public DocsData(long id, String value, DocsDescriptor descriptor){
this.id = id;
this.value = value;
this.descriptor = descriptor;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public DocsDescriptor getDescriptor() {
return descriptor;
}
public void setDescriptor(DocsDescriptor descriptor) {
this.descriptor = descriptor;
}
}
DocsDocument.java
package test;
import java.util.ArrayList;
import java.util.List;
public class DocsDocument {
private long id;
private List<DocsData> datas;
public DocsDocument(long id){
this.id = id;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<DocsData> getDatas() {
return datas;
}
public void setDatas(List<DocsData> datas) {
this.datas = datas;
}
public void add(DocsData data){
if(this.datas == null) this.datas = new ArrayList<DocsData>();
this.datas.add(data);
}
}
DocsDocumentType.java
package test;
import java.util.ArrayList;
import java.util.List;
public class DocsDocumentType {
private long id;
private String name;
private List<DocsDocument> documents;
public DocsDocumentType(long id, String name){
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<DocsDocument> getDocuments() {
return documents;
}
public void setDocuments(List<DocsDocument> documents) {
this.documents = documents;
}
public void add(DocsDocument document){
if(this.documents == null) this.documents = new ArrayList<DocsDocument>();
this.documents.add(document);
}
}
TestController.java
package test;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
#ConversationScoped
#Named("test")
public class TestController implements Serializable{
private static final long serialVersionUID = 2433550537340132027L;
#Inject
protected Conversation conversation;
private List<DocsDocumentType> documentTypes;
public void start(){
if(this.conversation.isTransient())
this.conversation.begin();
if(this.conversation != null)
this.conversation.setTimeout(10800000);
}
#PostConstruct
public void init(){
DocsDescriptor dePhone = new DocsDescriptor(1,"Number");
DocsDescriptor deName = new DocsDescriptor(2,"Name");
DocsDescriptor deLastName = new DocsDescriptor(3,"Last Name");
DocsDescriptor dePrice = new DocsDescriptor(4,"Product Price");
DocsDescriptor deCode = new DocsDescriptor(5,"Product Code");
DocsDescriptor deProdName = new DocsDescriptor(6,"Product Name");
DocsDocument jl = new DocsDocument(1);
jl.add(new DocsData(1,"514237797", dePhone));
jl.add(new DocsData(2,"John", deName));
jl.add(new DocsData(3,"Lennon", deLastName));
DocsDocument pm = new DocsDocument(2);
pm.add(new DocsData(4,"45312342", dePhone));
pm.add(new DocsData(5,"Paul", deName));
pm.add(new DocsData(6,"McCartney", deLastName));
DocsDocument rs = new DocsDocument(3);
rs.add(new DocsData(7,"567523534", dePhone));
rs.add(new DocsData(8,"Richard", deName));
rs.add(new DocsData(9,"Starkey", deLastName));
DocsDocument gh = new DocsDocument(3);
gh.add(new DocsData(10,"454623243", dePhone));
gh.add(new DocsData(11,"George", deName));
gh.add(new DocsData(12,"Harrison", deLastName));
DocsDocumentType identity = new DocsDocumentType(1,"Beatles");
identity.add(jl);
identity.add(pm);
identity.add(gh);
identity.add(rs);
DocsDocument iPhone = new DocsDocument(4);
iPhone.add( new DocsData(13,"iPhone 6S",deProdName));
iPhone.add( new DocsData(15,"23452340",deCode));
iPhone.add( new DocsData(16,"$650",dePrice));
DocsDocument nexus = new DocsDocument(5);
nexus.add( new DocsData(13,"Nexus 6P",deProdName));
nexus.add( new DocsData(15,"786338675",deCode));
nexus.add( new DocsData(16,"$600",dePrice));
DocsDocumentType product = new DocsDocumentType(1,"Product");
product.add(iPhone);
product.add(nexus);
this.documentTypes = new ArrayList<DocsDocumentType>();
this.documentTypes.add(identity);
this.documentTypes.add(product);
}
public List<DocsDocumentType> getDocumentTypes() {
return documentTypes;
}
public void setDocumentTypes(List<DocsDocumentType> documentTypes) {
this.documentTypes = documentTypes;
}
}
test.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>TEST</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</h:head>
<h:body>
<f:event type="preRenderView" listener="#{test.start}"/>
<h:panelGroup id="bigArea" style="width : 100%">
<ui:repeat value="#{test.documentTypes}" var="dt">
<p:panelGrid style="width : 750px">
<f:facet name="header">
<p:row>
<p:column style="width : 750px; text-align: left"><h:outputText value="#{dt.name}" /></p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<p:dataTable value="#{dt.documents}" var="doc" emptyMessage="...">
<p:columns value="#{doc.datas}" var="data">
<f:facet name="header"><h:outputText value="#{data.descriptor.name}"/></f:facet>
<h:outputText value="#{data.value}" />
</p:columns>
</p:dataTable>
</p:column>
</p:row>
</p:panelGrid>
</ui:repeat>
</h:panelGroup>
</h:body>
</html>
And here's the output:
http://picpaste.com/Captura_de_pantalla_2015-12-12_a_las_19.11.27_1-0Fd7lEtY.png
I'm using wildfly 9.0.1, primefaces-5.2
Can anybody help me out with a solution or an alternative?
Thank you all!

Value of p:columns cannot refer to var of parent dataTable. Documentation http://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml shows this moment. Actually in your model each row can contain different number of columns. You need to change model:
TableModel:
package test;
import java.util.ArrayList;
import java.util.List;
/**
* Created by kit on 14.12.2015.
*
* #author kit
*/
public class TableModel {
private String name;
private List<RowModel> rows = new ArrayList<>();
private List<ColumnModel> columns = new ArrayList<>();
/**
* Getters, Setters
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<RowModel> getRows() {
return rows;
}
public List<ColumnModel> getColumns() {
return columns;
}
}
RowModel:
package test;
import java.util.HashMap;
import java.util.Map;
/**
* Created by kit on 14.12.2015.
*
* #author kit
*/
public class RowModel<T> {
private String name;
private Map<ColumnModel, T> data = new HashMap<>();
/**
* Getters, Setters
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<ColumnModel, T> getData() {
return data;
}
}
ColumnModel:
package test;
/**
* Created by kit on 14.12.2015.
*
* #author kit
*/
public class ColumnModel<T> {
private T data;
/**
* Getters, Setters
*/
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
Modify TestController:
package test;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
#ConversationScoped
#Named("test")
public class TestController implements Serializable {
private static final long serialVersionUID = 2433550537340132027L;
#Inject
protected Conversation conversation;
private List<TableModel> tables = new ArrayList<>();
private List<DocsDocumentType> documentTypes;
public void start() {
if (this.conversation.isTransient())
this.conversation.begin();
if (this.conversation != null)
this.conversation.setTimeout(10800000);
}
#PostConstruct
public void init() {
DocsDescriptor dePhone = new DocsDescriptor(1, "Number");
DocsDescriptor deName = new DocsDescriptor(2, "Name");
DocsDescriptor deLastName = new DocsDescriptor(3, "Last Name");
DocsDescriptor dePrice = new DocsDescriptor(4, "Product Price");
DocsDescriptor deCode = new DocsDescriptor(5, "Product Code");
DocsDescriptor deProdName = new DocsDescriptor(6, "Product Name");
DocsDocument jl = new DocsDocument(1);
jl.add(new DocsData(1, "514237797", dePhone));
jl.add(new DocsData(2, "John", deName));
jl.add(new DocsData(3, "Lennon", deLastName));
DocsDocument pm = new DocsDocument(2);
pm.add(new DocsData(4, "45312342", dePhone));
pm.add(new DocsData(5, "Paul", deName));
pm.add(new DocsData(6, "McCartney", deLastName));
DocsDocument rs = new DocsDocument(3);
rs.add(new DocsData(7, "567523534", dePhone));
rs.add(new DocsData(8, "Richard", deName));
rs.add(new DocsData(9, "Starkey", deLastName));
DocsDocument gh = new DocsDocument(3);
gh.add(new DocsData(10, "454623243", dePhone));
gh.add(new DocsData(11, "George", deName));
gh.add(new DocsData(12, "Harrison", deLastName));
DocsDocumentType identity = new DocsDocumentType(1, "Beatles");
identity.add(jl);
identity.add(pm);
identity.add(gh);
identity.add(rs);
DocsDocument iPhone = new DocsDocument(4);
iPhone.add(new DocsData(13, "iPhone 6S", deProdName));
iPhone.add(new DocsData(15, "23452340", deCode));
iPhone.add(new DocsData(16, "$650", dePrice));
DocsDocument nexus = new DocsDocument(5);
nexus.add(new DocsData(13, "Nexus 6P", deProdName));
nexus.add(new DocsData(15, "786338675", deCode));
nexus.add(new DocsData(16, "$600", dePrice));
DocsDocumentType product = new DocsDocumentType(1, "Product");
product.add(iPhone);
product.add(nexus);
this.documentTypes = new ArrayList<DocsDocumentType>();
this.documentTypes.add(identity);
this.documentTypes.add(product);
// Populating tableModel
for (DocsDocumentType docsDocumentType : documentTypes) {
TableModel tableModel = new TableModel();
tableModel.setName(docsDocumentType.getName());
for (DocsDocument docsDocument : docsDocumentType.getDocuments()) {
RowModel<String> rowModel = new RowModel<>();
rowModel.setName(String.valueOf(docsDocument.getId()));
tableModel.getRows().add(rowModel);
for (DocsData docsData : docsDocument.getDatas()) {
ColumnModel<DocsDescriptor> columnModel = findColumn(tableModel, docsData.getDescriptor());
if (columnModel == null) {
columnModel = new ColumnModel<>();
columnModel.setData(docsData.getDescriptor());
tableModel.getColumns().add(columnModel);
}
rowModel.getData().put(columnModel, docsData.getValue());
}
}
tables.add(tableModel);
}
}
private ColumnModel findColumn(TableModel tableModel, DocsDescriptor descriptor) {
for (ColumnModel columnModel : tableModel.getColumns()) {
if (descriptor.equals(columnModel.getData())) {
return columnModel;
}
}
return null;
}
public List<TableModel> getTables() {
return tables;
}
}
And view:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>TEST</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</h:head>
<h:body>
<f:event type="preRenderView" listener="#{test.start}"/>
<h:panelGroup id="bigArea" style="width : 100%">
<ui:repeat value="#{test.tables}" var="dt">
<p:panelGrid style="width : 750px">
<f:facet name="header">
<p:row>
<p:column style="width : 750px; text-align: left"><h:outputText value="#{dt.name}" /></p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<p:dataTable value="#{dt.rows}" var="row" emptyMessage="...">
<p:columns value="#{dt.columns}" var="col">
<f:facet name="header"><h:outputText value="#{col.data.name}"/></f:facet>
<h:outputText value="#{row.data[col]}" />
</p:columns>
</p:dataTable>
</p:column>
</p:row>
</p:panelGrid>
</ui:repeat>
</h:panelGroup>
</h:body>
</html>

Related

Passing additional Objects to commandLink

I want to use the same View for different states of a bean. Therefore I need to Inject/pass/whatever get the current bean I want to modify, but I always get null.
I already tried it with , with #Inject, and the other solutions given in How can I pass selected row to commandLink inside dataTable or ui:repeat?
The View:
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import org.primefaces.PrimeFaces;
import de.auticon.beans.Mitarbeiter;
import de.auticon.beans.Skill;
#Named(value = "skillView")
#ViewScoped
public class SkillView implements Serializable {
private static final long serialVersionUID = -3256509521249071048L;
private Mitarbeiter employee;
private Skill skill = new Skill();
private List<String> expertises = new ArrayList<String>();
#PostConstruct
public void init() {
List<Expertise_String> expertiseObjects = Queries.findAllExpertises();
for (Expertise_String singleExpertise : expertiseObjects) {
expertises.add(singleExpertise.getExpertise() + " - " + singleExpertise.getFullNameString());
}
}
//Here employee is null, but I need the new/existing employee
public void addSkill() {
employee.getSkills().add(skill);
PrimeFaces.current().dialog().closeDynamic(null);
}
public void closeDialog() {
PrimeFaces.current().dialog().closeDynamic(null);
}
public Skill getSkill() {
return skill;
}
public void setSkill(Skill skill) {
this.skill = skill;
}
public Mitarbeiter getEmployee() {
return employee;
}
public void setEmployee(Mitarbeiter employee) {
this.employee = employee;
}
}
XHTML from which it is called for existing employees:
<h:form id="form">
<p:panelGrid columns="2" styleClass="ui-noborder">
<p:menu toggleable="true">
<p:submenu label="Verwaltung">
<p:menuitem>
<p:commandButton value="Mitarbeiter anlegen" action="#{adminView.addNewEmployee}" icon="pi pi-users"
process="#this :form:skillsDialog">
<p:ajax event="dialogReturn" listener="#{mitarbeiterView.update}" update=":form, :form:table"/>
</p:commandButton>
</p:menuitem>
</p:submenu>
</p:menu>
<p:dataTable id="table" var="row" value="#{mitarbeiterView.mitarbeiter}" liveResize="true" resizableColumns="true">
<p:column headerText="Skillset">
<p:commandLink update=":form:skillsDialog, :form:skillsDetails" oncomplete="PF('skillsDialog').show()" title="Detail"
styleClass="ui-icon pi pi-search">
<f:setPropertyActionListener value="#{row}" target="#{mitarbeiterView.selectedEmployee}" />
</p:commandLink>
<p:commandLink action="#{adminView.openNewSkillDialog(row)}" title="add" styleClass="ui-icon pi pi-plus">
<!-- <f:setPropertyActionListener value="#{row}" target="#{skillView.employee}" /> -->
</p:commandLink>
</p:column>
</p:dataTable>
</p:panelGrid>
<p:dialog id="skillsDialog" header="Skillsheet von #{mitarbeiterView.selectedEmployee.vorname} #{mitarbeiterView.selectedEmployee.name}"
showEffect="fade" widgetVar="skillsDialog" modal="true" resizable="true">
<p:outputPanel id="skillsDetails">
<p:dataTable var="skillRow" value="#{mitarbeiterView.selectedEmployee.skills}">
<p:column headerText="Skill">
<h:outputText value="#{skillRow.name}" />
</p:column>
<p:column headerText="Ausprägung">
<h:outputText value="#{skillRow.expertise} - #{skillRow.expertiseString.fullName}" />
</p:column>
<p:column headerText="Beschreibung">
<h:outputText value="#{skillRow.description}" />
</p:column>
</p:dataTable>
</p:outputPanel>
</p:dialog>
</h:form>
Second XHTML, for new employees:
<h:form id="newForm">
<h3>Skillsheet</h3>
<p:panelGrid id="skillPanel" columns="2" cellpadding="5" styleClass="ui-noborder">
<p:column style="width:50px">
<p:commandButton id="openAddSkill" process="#this" action="#{adminView.openNewSkillDialog}"
title="Neuer Skill" icon="pi pi-plus">
<!-- <f:setPropertyActionListener target="#{skillView.employee}" value="#{newEmployeeView.newEmployee}"/> -->
<p:ajax event="dialogReturn" update=":newForm :newForm:skillPanel"/>
</p:commandButton>
</p:column>
</p:panelGrid>
<p:commandButton value="Hinzufügen" id="addSkill" icon="pi pi-plus" action="#{skillView.addSkill()}"/>
</h:form>
AdminView.java:
#Named(value = "adminView")
#ViewScoped
public class AdminView implements Serializable {
private static final long serialVersionUID = 5252224062484767900L;
#Inject
private SkillView skillView;
public void addNewEmployee() {
Map<String, Object> options = new HashMap<String, Object>();
options.put("id", "newEmployeeDialogID");
options.put("widgetVar", "newEmployeeDialogVar");
options.put("resizable", false);
options.put("modal", false);
PrimeFaces.current().dialog().openDynamic("/dialogs/newEmployee", options, null);
}
public void openNewSkillDialog(Mitarbeiter employee) {
Map<String, Object> options = new HashMap<String, Object>();
options.put("resizable", true);
options.put("modal", false);
options.put("contentWidth", 420);
PrimeFaces.current().dialog().openDynamic("/dialogs/skill", options, null);
skillView.setEmployee(employee);
}
}
Skill.java:
public class Skill {
#NotNull(message = "Skill fehlt")
private String name;
#NotNull(message = "Bitte Ausprägung angeben")
private short expertise;
private String description;
private String expertiseFullname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public short getExpertise() {
return expertise;
}
public void setExpertise(short expertise) {
this.expertise = expertise;
}
public String getExpertiseFullname() {
return expertiseFullname;
}
public void setExpertiseFullname(String expertiseFullname) {
this.expertiseFullname = expertiseFullname;
}
public void setExpertise(String fullName) {
try {
this.expertise = Short.valueOf(fullName.substring(0, fullName.indexOf(" - ")));
} catch (Exception e) {
this.expertise = 0;
}
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Mitarbeiter.java:
public class Mitarbeiter {
private int id;
#NotNull(message = "Nachname fehlt")
private String name;
#NotNull(message = "Vorname fehlt")
private String vorname;
private Date entryDate;
private List<Skill> skills = new ArrayList<Skill>();
private int expertise;
public Mitarbeiter() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public List<Skill> getSkills() {
return skills;
}
public void setSkills(List<Skill> skills) {
this.skills = skills;
}
public int getExpertise() {
return expertise;
}
public void setExpertise(int expertise) {
this.expertise = expertise;
}
MitarbeiterView.java:
#Named(value = "mitarbeiterView")
#ViewScoped
public class MitarbeiterView implements Serializable {
private static final long serialVersionUID = 7924178697538784022L;
#Inject
MitarbeiterService mitarbeiterService;
private List<Mitarbeiter> mitarbeiter;
private Mitarbeiter selectedEmployee;
#PostConstruct
public void init() {
SessionConfig.initSession();
new Commons();
updateMitarbeiter();
}
private void updateMitarbeiter() {
mitarbeiter = new ArrayList<Mitarbeiter>();
List<EmployeeDTO> dtos = Queries.findAllEmployees();
for (EmployeeDTO employeeDTO : dtos) {
mitarbeiter.add(mitarbeiterService.convertToMitarbeiter(employeeDTO));
}
}
public void update() {
updateMitarbeiter();
PrimeFaces.current().ajax().update("form:table");
}
public List<Mitarbeiter> getMitarbeiter() {
return mitarbeiter;
}
public void setMitarbeiter(List<Mitarbeiter> mitarbeiter) {
this.mitarbeiter = mitarbeiter;
}
public void setSelectedEmployee(Mitarbeiter selectedEmployee) {
this.selectedEmployee = selectedEmployee;
}
public Mitarbeiter getSelectedEmployee() {
return selectedEmployee;
}
NewEmployeeView.java:
#Named(value = "newEmployeeView")
#ViewScoped
public class NewEmployeeView implements Serializable {
private static final long serialVersionUID = 789108010781037452L;
#ManagedProperty(value = "#{mitarbeiter}")
private Mitarbeiter newEmployee = new Mitarbeiter();
#PostConstruct
public void init() {
}
public Mitarbeiter getNewEmployee() {
return newEmployee;
}
public void setNewEmployee(Mitarbeiter mitarbeiter) {
this.newEmployee = mitarbeiter;
}
Calling adSkill() from the first XHTML should have the selected employee from the Datatable. In the second case, it should have a freshly created, "empty" employee, which I already provide.
I think that major cause of your problems is that skillView bean is #RequestScoped meaning that it is being constructed and destroyed on each request and thus your employee is being reinitialized/reset to null every time. Check out this accepted answer for details.
Hints that might lead you to solution:
put debug lines in at least init() and setEmployee(..) methods of skillView and observe behaviour,
change scope of skillView to #ViewScoped which will preserve employee object across multiple Ajax requests

How to refresh ViewScoped(Omnifaces) page on data change?

I have some Category model, that is organized as adjacency list. So when i am adding new one, i format current list so it has viewable hierarchy. For this i use Omnifaces ViewScoped bean. Upon loading data i format it and display it in selectOneMenu. When i add new category that has parent to it i redirect to category list. If i open form page again it doesn't display new hierarchy but puts new category on the end as if it doesn't have parent. If i restart server for example and load page again it is displayed as it should.
So my question is how do i refresh it properly since obviouslu i am not doing it right.
form.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
template="#{adminConfig.templatePath}"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:define name="title">#{categoryFormMB.title}</ui:define>
<ui:define name="description"></ui:define>
<ui:define name="body">
<f:metadata>
<f:viewParam name="id" value="#{categoryFormMB.id}" converter="javax.faces.Long"/>
<f:event type="preRenderView" listener="#{categoryFormMB.initForm()}"/>
</f:metadata>
<div class="row">
<div class="col-md-6">
<p:panel styleClass="box box-primary">
<h:form styleClass="form-horizontal">
<div class="form-group ui-flat">
<p:outputLabel
for="name"
value="#{label.categoryName}"
styleClass="control-label col-md-4"/>
<div class="col-md-8">
<p:inputText
id="name"
value="#{categoryFormMB.category.name}"
styleClass="form-control"
required="true" />
</div>
</div>
<div class="form-group ui-flat">
<p:outputLabel
for="slug"
value="#{label.categorySlug}"
styleClass="control-label col-md-4"/>
<div class="col-md-8">
<p:inputText
id="slug"
value="#{categoryFormMB.category.slug}"
styleClass="form-control"
required="true" />
</div>
</div>
<div class="form-group ui-flat">
<p:outputLabel
for="parent"
value="#{label.parentCategory}"
styleClass="control-label col-md-4"/>
<div class="col-md-8">
<p:selectOneMenu
value="#{categoryFormMB.category.parent}"
id="parent"
converter="categoryConv">
<f:selectItem
itemLabel="#{label.noParent}"
itemValue=""/>
<f:selectItems
value="#{categoryFormMB.formattedList}"
var="category"
itemValue="#{category}"
itemLabel="#{category.name}"/>
</p:selectOneMenu>
</div>
</div>
<div class="form-group ui-flat">
<p:outputLabel
for="description"
value="#{label.categoryDescription}"
styleClass="control-label col-md-4"/>
<div class="col-md-8">
<p:inputTextarea
id="description"
value="#{categoryFormMB.category.description}"
styleClass="form-control"/>
</div>
</div>
<div class="form-group ui-flat">
<p:outputLabel
for="title-tag"
value="#{label.titleTag}"
styleClass="control-label col-md-4"/>
<div class="col-md-8">
<p:inputText
id="title-tag"
value="#{categoryFormMB.category.titleTag}"
styleClass="form-control"/>
</div>
</div>
<div class="form-group ui-flat">
<p:outputLabel
for="description-tag"
value="#{label.descriptionTag}"
styleClass="control-label col-md-4"/>
<div class="col-md-8">
<p:inputTextarea
id="description-tag"
value="#{categoryFormMB.category.descriptionTag}"
styleClass="form-control"/>
</div>
</div>
<div class="form-group">
<div class="col-md-4"></div>
<div class="col-md-8">
<div class="pull-right">
<p:commandButton
value="#{label.saveCategory}"
ajax="false"
rendered="#{ ! categoryFormMB.editing}"
action="#{categoryFormMB.save()}"
styleClass="btn btn-success btn-flat"
icon="fa fa-floppy-o"/>
<p:commandButton
value="#{label.updateCategory}"
ajax="false"
rendered="#{categoryFormMB.editing}"
action="#{categoryFormMB.update()}"
styleClass="btn btn-success btn-flat"
icon="fa fa-floppy-o"/>
<p:commandButton
value="#{label.cancel}"
action="#{categoryFormMB.close()}"
styleClass="btn btn-danger btn-flat"
ajax="false"
icon="fa fa-times"
immediate="true"/>
</div>
</div>
</div>
</h:form>
<p:messages
globalOnly="false"
showDetail="true"
showSummary="false"
showIcon="true" />
</p:panel>
</div>
</div>
</ui:define>
</ui:composition>
CategoryFormMB
package com.github.cvetan.bookstore.mb.category;
import static com.github.adminfaces.template.util.Assert.has;
import com.github.cvetan.bookstore.model.Category;
import com.github.cvetan.bookstore.sb.category.CategorySBLocal;
import com.github.cvetan.bookstore.util.Redirector;
import com.github.cvetan.bookstore.util.StringRepeater;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
/**
*
* #author cvetan
*/
#Named(value = "categoryFormMB")
#RequestScoped
public class CategoryFormMB {
#EJB
private CategorySBLocal categorySB;
private List<Category> list;
private List<Category> formattedList;
private String title;
private Integer id;
private boolean editing;
private Category category;
private String message;
/**
* Creates a new instance of CategoryFormMB
*/
public CategoryFormMB() {
}
public CategorySBLocal getCategorySB() {
return categorySB;
}
public void setCategorySB(CategorySBLocal categorySB) {
this.categorySB = categorySB;
}
public List<Category> getList() {
return list;
}
public void setList(List<Category> list) {
this.list = list;
}
public List<Category> getFormattedList() {
return formattedList;
}
public void setFormattedList(List<Category> formattedList) {
this.formattedList = formattedList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public boolean isEditing() {
return editing;
}
public void setEditing(boolean editing) {
this.editing = editing;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void initForm() {
formattedList = new ArrayList<>();
getListFromDB();
formatList(list, 0);
if (has(id)) {
// TODO
} else {
title = "New category";
editing = false;
category = new Category();
message = "Category has been saved.";
}
}
private void getListFromDB() {
list = categorySB.getAll();
}
private void formatList(List<Category> list, int level) {
for (Category c: list) {
if (formattedList.contains(c)) {
continue;
}
c.setName(StringRepeater.repeat(" - ", level) + c.getName());
formattedList.add(c);
if ( ! c.getChildCategories().isEmpty()) {
formatList(c.getChildCategories(), level + 1);
}
}
}
public String save() {
try {
categorySB.save(category);
return Redirector.redirectWithInfo("Category has been saved.", "/admin/category-list.xhmtl?faces-redirect=true");
} catch (Exception ex) {
return Redirector.redirectWithError(ex.getMessage(), "/admin/category-form.xhtml?faces-redirect=true");
}
}
}
CategorySB(EJB)
package com.github.cvetan.bookstore.sb.category;
import com.github.cvetan.bookstore.model.Category;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
/**
*
* #author cvetan
*/
#Stateless
public class CategorySB implements CategorySBLocal {
#PersistenceContext (unitName = "BookstorePU")
private EntityManager entityManager;
#Override
public List<Category> getAll() {
return entityManager.createNamedQuery("Category.findAll").getResultList();
}
#Override
public void save(Category category) {
entityManager.persist(category);
}
#Override
public Category getById(int id) {
Query query = entityManager.createNamedQuery("Category.findById");
query.setParameter("id", id);
Category category = (Category) query.getSingleResult();
return category;
}
#Override
public void update(Category category) {
}
#Override
public void delete(int id) {
Category category = entityManager.find(Category.class, id);
entityManager.remove(category);
}
}
Category entity
package com.github.cvetan.bookstore.model;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
*
* #author cvetan
*/
#Entity
#Table(name = "categories")
#XmlRootElement
#NamedQueries({
#NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c")
, #NamedQuery(name = "Category.findById", query = "SELECT c FROM Category c WHERE c.id = :id")
, #NamedQuery(name = "Category.findByName", query = "SELECT c FROM Category c WHERE c.name = :name")
, #NamedQuery(name = "Category.findBySlug", query = "SELECT c FROM Category c WHERE c.slug = :slug")
, #NamedQuery(name = "Category.findByDescription", query = "SELECT c FROM Category c WHERE c.description = :description")
, #NamedQuery(name = "Category.findByTitleTag", query = "SELECT c FROM Category c WHERE c.titleTag = :titleTag")
, #NamedQuery(name = "Category.findByDescriptionTag", query = "SELECT c FROM Category c WHERE c.descriptionTag = :descriptionTag")
, #NamedQuery(name = "Category.findByCreatedAt", query = "SELECT c FROM Category c WHERE c.createdAt = :createdAt")
, #NamedQuery(name = "Category.findByUpdatedAt", query = "SELECT c FROM Category c WHERE c.updatedAt = :updatedAt")})
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
#Basic(optional = false)
#NotNull
#Size(min = 1, max = 255)
#Column(name = "name")
private String name;
#Basic(optional = false)
#NotNull
#Size(min = 1, max = 255)
#Column(name = "slug")
private String slug;
#Size(max = 255)
#Column(name = "description")
private String description;
#Size(max = 255)
#Column(name = "title_tag")
private String titleTag;
#Size(max = 255)
#Column(name = "description_tag")
private String descriptionTag;
#Basic(optional = false)
#NotNull
#Column(name = "created_at")
#Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
#Column(name = "updated_at")
#Temporal(TemporalType.TIMESTAMP)
private Date updatedAt;
#OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
private List<Category> childCategories;
#JoinColumn(name = "parent", referencedColumnName = "id")
#ManyToOne
private Category parent;
public Category() {
}
public Category(Integer id) {
this.id = id;
}
public Category(Integer id, String name, String slug, Date createdAt) {
this.id = id;
this.name = name;
this.slug = slug;
this.createdAt = createdAt;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitleTag() {
return titleTag;
}
public void setTitleTag(String titleTag) {
this.titleTag = titleTag;
}
public String getDescriptionTag() {
return descriptionTag;
}
public void setDescriptionTag(String descriptionTag) {
this.descriptionTag = descriptionTag;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
#XmlTransient
public List<Category> getChildCategories() {
return childCategories;
}
public void setChildCategories(List<Category> childCategories) {
this.childCategories = childCategories;
}
public Category getParent() {
return parent;
}
public void setParent(Category parent) {
this.parent = parent;
}
#Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object object) {
if (!(object instanceof Category)) {
return false;
}
Category other = (Category) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
#Override
public String toString() {
return name;
}
#PrePersist
public void setCreatedAt() {
createdAt = new Date();
}
#PreUpdate
public void setUpdatedAt() {
updatedAt = new Date();
}
}
There is caching on the JPA/EclipseLink side, which i didn't know. When i clear cache with: entityManager.getEntityManagerFactory().getCache().evictAll(); it refreshes data. So problem was not on the bean or it's scope.

Error : javax.el.PropertyNotFoundException: Target Unreachable, 'null' returned null [duplicate]

This question already has answers here:
Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable
(18 answers)
Closed 7 years ago.
I got this error below when I was running my JSF page.
javax.el.PropertyNotFoundException: Target Unreachable, 'null' returned null..
Warning: /createStaff.xhtml #33,125
value="#{staffBean.staff.firstName}": Target Unreachable, 'null'
returned null javax.el.PropertyNotFoundException: /createStaff.xhtml
#33,125 value="#{staffBean.staff.firstName}": Target Unreachable,
'null' returned null
I don't get why I will run into the error when I use value="#{staffBean.staff.firstName}". There is no problem when I use the value="#{staffBean.userName}" and value="#{staffBean.passWord}" above.
This is my createStaff.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:p="http://primefaces.org/ui">
<h:head>
<title>Create Staff</title>
</h:head>
<h:body>
<f:view>
<h:form>
<p:panel id ="panel" header="Staff Creation">
<p:messages id="msgs" />
<h:panelGrid columns="3" columnClasses="label, value">
<h:outputText value="Username: *" />
<p:inputText id="username" value="#{staffBean.userName}" required="true" label="Username">
</p:inputText>
<p:message for="username" />
<h:outputLabel for="pwd1" value="Password 1: *" />
<p:password id="pwd1" value="#{staffBean.passWord}" match="pwd2" label="Password 1" required="true" feedback="true" />
<p:message for="pwd1" />
<h:outputLabel for="pwd2" value="Password 2: *" />
<p:password id="pwd2" value="#{staffBean.passWord}" label="Password 2" required="true" feedback="true" />
<p:message for="pwd2" />
<h:outputText value="First name: *" />
<p:inputText id="firstname" value="#{staffBean.staff.firstName}" required="true" label="Username">
</p:inputText>
<p:message for="firstname" />
<h:outputText value="Last name: *" />
<p:inputText id="lastname" value="#{staffBean.staff.lastName}" required="true" label="Username">
</p:inputText>
<p:message for="lastname" />
<h:outputText value="Last name: *" />
<p:selectOneRadio id="genderconsole" value="#{staffBean.staff.gender}" required="true">
<f:selectItem itemLabel="Male" itemValue="Male" />
<f:selectItem itemLabel="Female" itemValue="Female" />
</p:selectOneRadio>
<p:message for="genderconsole" />
<p:commandButton value="Create Staff"
id="ajax"
update="panel">
</p:commandButton>
</h:panelGrid>
</p:panel>
</h:form>
</f:view>
</h:body>
</html>
This is my StaffBean.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package managedbean;
import entities.Staff;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import sessionBean.staffSessionBeanLocal;
#Named(value = "staffBean")
#SessionScoped
//#ViewScoped
public class StaffBean implements Serializable {
#EJB
private staffSessionBeanLocal staffSession;
private String userName;
private String passWord;
private String loginStatus;
private Staff staff;
...........
////Code removed
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getLoginStatus() {
return loginStatus;
}
public void setLoginStatus(String loginStatus) {
this.loginStatus = loginStatus;
}
public Staff getStaff() {
return staff;
}
public void setStaff(Staff staff) {
this.staff = staff;
}
}
This is my staff entity.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package entities;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
#Entity
public class Staff extends User implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String imageURL;
#ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
private List<Roles> roles = new ArrayList<Roles>();
#Override
public Long getId() {
return id;
}
#Override
public void setId(Long id) {
this.id = id;
}
#Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Staff)) {
return false;
}
Staff other = (Staff) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
#Override
public String toString() {
return "entities.Staff[ id=" + id + " ]";
}
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
public List<Roles> getRoles() {
return roles;
}
public void setRoles(List<Roles> roles) {
this.roles = roles;
}
}
This is my User class which Staff class extends from.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package entities;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
#MappedSuperclass
public class User implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String password;
private Timestamp joinDate;
private String userType;
private String gender;
private String email;
private String contactNo;
private String firstName;
private String lastName;
private Timestamp dOB;
private String address;
private String accountStatus;
private int numOfFailLogin;
private String maritalStatus;
private String activationCode;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof User)) {
return false;
}
User other = (User) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
#Override
public String toString() {
return "entities.User[ id=" + id + " ]";
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Timestamp getJoinDate() {
return joinDate;
}
public void setJoinDate(Timestamp joinDate) {
this.joinDate = joinDate;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Timestamp getdOB() {
return dOB;
}
public void setdOB(Timestamp dOB) {
this.dOB = dOB;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAccountStatus() {
return accountStatus;
}
public void setAccountStatus(String accountStatus) {
this.accountStatus = accountStatus;
}
public String getMaritalStatus() {
return maritalStatus;
}
public void setMaritalStatus(String maritalStatus) {
this.maritalStatus = maritalStatus;
}
public int getNumOfFailLogin() {
return numOfFailLogin;
}
public void setNumOfFailLogin(int numOfFailLogin) {
this.numOfFailLogin = numOfFailLogin;
}
public String getActivationCode() {
return activationCode;
}
public void setActivationCode(String activationCode) {
this.activationCode = activationCode;
}
}
You have no property firstName in your entity staff
UPDATE:
Looks like your staffobject is null add:
#PostConstruct
public void init() {
staff = new Stuff();
}
The error suggests that when the "firstName" is being accessed, it cannot be reached. So the "Staff" has not been constructed yet.
Add a method to your managed bean, this will resolve the issue.
#PostConstruct
public void init() {
staff= new Staff ();
}
For better understanding of why you should do it that way and not
Staff staff = new Staff();
JSF - what is the difference between #PostConstruct and direct method call from constructor?

primefaces autocomplete not showing in textBox

I have tried to implement the autocomplete feature in primefaces but suggestions do not show up in my textbox. Can someone show me what I'm missing. theses are codes
udateCategory.xhtml
<p:panel header="Type in Category to Edit" >
<p:outputLabel value="Category Name"/>
<p:autoComplete value="#{categoryBean.selectedCategory}"
completeMethod="#{categoryBean.completeCategory}"
var="cat"
itemLabel="#{cat.categoryName}"
itemValue="#{cat}"
converter="#{catConverter}"
forceSelection="true"/>
<p:commandButton value="Update" action="#{category.saveCategory}"/>
</p:panel>
CategoryBean
public class CategoryBean implements Serializable{
private Category selectedCategory;
/**
* Creates a new instance of CategoryBean
*/
public CategoryBean() {
}
public List<Category> completeCategory (String query){
CategoryManager manager = new CategoryManager();//an instance of the manager
List<Category> suggestions = new ArrayList<>();//an instance of list
List<Category> allCategory = new ArrayList<>(); //populate the allCategory with data fro db
allCategory = manager.getAllCategory();
//checck to see if data exist in allCategory
if(!allCategory.isEmpty()){
System.out.println("kobla : allcategory has data");
}
else
{
System.out.println("kobla: no data in alcategory");
}
for(Category cat : allCategory){
if(cat.getCategoryName().startsWith(query)){
suggestions.add(cat);
}
}
//check to see if data exists in sugestions
if (!suggestions.isEmpty()) {
System.out.println("kobla : suggestions has data");
} else {
System.out.println("kobla: no data in suggestions");
}
return suggestions;
}
/**
* #return the selectedCategory
*/
public Category getSelectedCategory() {
return selectedCategory;
}
/**
* #param selectedCategory the selectedCategory to set
*/
public void setSelectedCategory(Category selectedCategory) {
this.selectedCategory = selectedCategory;
}
}
CategoryConverter
public class CategoryConverter implements Converter{
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if(value.trim().equals("")){
return null;
}
else{
try{
int id = Integer.parseInt(value);
List<Category> myCategory = new ArrayList<>();//
myCategory = new CategoryManager().getAllCategory();//load data fro db
for(Category cat : myCategory){
if(cat.getCategoryID() == id){
return cat;
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
return null;
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if(value == null || value ==""){
return null;
}
else
{
return String.valueOf(((Category)value).getCategoryName());
}
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
this works for me
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.ViewScoped;
#ManagedBean
#ViewScoped
public class CategoryBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Category selectedCategory;
#PostConstruct
public void init(){
selectedCategory = new Category();
}
public void saveCategory(){
System.out.println("saved "+this.selectedCategory);
}
public List<Category> completeCategory(String query) {
CategoryManager manager = new CategoryManager();// an instance of the
// manager
List<Category> suggestions = new ArrayList<>();// an instance of list
List<Category> allCategory = new ArrayList<>(); // populate the
// allCategory with data
// fro db
allCategory = manager.getAllCategory();
// checck to see if data exist in allCategory
if (!allCategory.isEmpty()) {
System.out.println("kobla : allcategory has data");
} else {
System.out.println("kobla: no data in alcategory");
}
for (Category cat : allCategory) {
if (cat.getCategoryName().startsWith(query)) {
suggestions.add(cat);
}
}
// check to see if data exists in sugestions
if (!suggestions.isEmpty()) {
System.out.println("kobla : suggestions has data");
} else {
System.out.println("kobla: no data in suggestions");
}
return suggestions;
}
/**
* #return the selectedCategory
*/
public Category getSelectedCategory() {
return selectedCategory;
}
/**
* #param selectedCategory
* the selectedCategory to set
*/
public void setSelectedCategory(Category selectedCategory) {
this.selectedCategory = selectedCategory;
}
}
and
import java.io.Serializable;
public class Category implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int categoryID;
private String categoryName;
public int getCategoryID() {
return categoryID;
}
public void setCategoryID(int categoryID) {
this.categoryID = categoryID;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Category(int categoryID, String categoryName) {
super();
this.categoryID = categoryID;
this.categoryName = categoryName;
}
public Category() {
super();
}
#Override
public String toString() {
return "Category [categoryID=" + categoryID + ", categoryName="
+ categoryName + "]";
}
}
and
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
#ManagedBean
#RequestScoped
public class CategoryConverter implements Converter, Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if(value.trim().equals("")){
return null;
}
else{
try{
int id = Integer.parseInt(value);
List<Category> myCategory = new ArrayList<>();//
myCategory = new CategoryManager().getAllCategory();//load data fro db
for(Category cat : myCategory){
if(cat.getCategoryID() == id){
return cat;
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
return null;
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if(value == null || value ==""){
return null;
}
else
{
return String.valueOf(((Category)value).getCategoryName());
}
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
and
<!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>
<h:form>
<p:outputLabel value="Category Name" />
<p:autoComplete
value="#{categoryBean.selectedCategory}"
completeMethod="#{categoryBean.completeCategory}"
var="cat"
itemLabel="#{cat.categoryName}"
itemValue="#{cat}"
converter="#{categoryConverter}"
forceSelection="true" />
<p:commandButton value="Update" action="#{categoryBean.saveCategory}" />
</h:form>
</h:body>
</html>

#ViewScoped calls post construct each time action is performed

I've asked one thousand of times but no one gives me a valid solution. Please why in my xhtml page the post construct is called all times when i press the button?how can i solve this problem?Please a valid solution. I've read many answers but they don't fit with my problem.I'm in trouble by 1 week.
The scope of my xhtml isn't changed :
update of a render component that updates another component
<!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="hotel and escursions" render="#form" update=":form:Hotel :form"/>
</h:panelGrid>
</p:panel>
<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:commandButton actionListener="#{addDefaultPackageBean.add}" value="Add" update=":form:Volo :form:Hotel" render="#form" process="#this,:form:Volo,:form:Location"/>
</h:form>
</h:body>
</html>
My bean page:
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 implements Serializable {
/**
*
*/
#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();
this.elelishotel=new ArrayList<ElementDTO>();
this.elelisescursion=new ArrayList<ElementDTO>();
this.elelisfly=new ArrayList<ElementDTO>();
this.availableLocations=new ArrayList<String>();
this.flyElem=new ElementDTO();
}
#PostConstruct
public void init()
{
System.out.print("merda init ");
if(this.getElelisfly().size()==0){System.out.print("Voli vuoto ");}
this.setElelis(elemMgr.getAllElements());
this.selectedEscursions=new ElementDTO[this.getElelis().size()];
this.fly="";
for(ElementDTO e:elelis)
{
if (this.availableLocations.contains(e.getLocation())==false)
{
this.availableLocations.add(e.getLocation());
}
}
}
public String add() {
System.out.print("entrato nell'ultimo step!il mio hotel è "+hotel);
//System.out.print("entrato nell'ultimo step!il mio hotel è "+this.selectedEscursions[0].getName());
/* //assegno location al pacchetto defpackDTO
this.defpackDTO.setLocation(this.getFlyElem().getLocation());
//assegno location a defpackDTO
this.defpackDTO.getElem().add(this.getFlyElem());
//assegno hotel a defpackDTO
this.AssignElemHotelFromSelection();
//assegno escursioni a defpackDTO
for(int i=0;i<this.selectedEscursions.length;i++)
{
this.defpackDTO.getElem().add(this.selectedEscursions[i]);
}
defpackMgr.save(defpackDTO);
*/
return "/employee/index?faces-redirect=true";
}
public void sel()
{
System.out.print("ehila"+this.fly );
this.setElelis(this.elemMgr.getAllElementsByLocation(this.defpackDTO.getLocation()));
for(ElementDTO e:elelis)
{
System.out.print("elemento della location Haiti "+e.getName());
}
for(ElementDTO e:elelis)
{
System.out.print("elementisfwefsf dei voli "+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())))
{
System.out.print("ho un escursione tra gli elementi con le date giuste"+e.getName());
this.getElelisescursion().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(){
this.getElelisfly().clear();
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;
}
}
Use import javax.faces.bean.ViewScoped; if the managed bean is not a CDI artifact.

Resources