JSF SelectOneRadio PrimeFaces javax.el.PropertyNotFoundException: Target Unreachable - jsf

When I insert this into my xhtml I'll get the javax.el.PropertyNotFoundException: Target Unreachable, identifier 'telefonbuch' resolved to null
<h3>Auswahl</h3>
<h1:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<p:outputLabel for="geschaeftsstelle" value="Geschäftsstelle:" />
<p:selectOneRadio id="geschaeftsstelle" value="#{telefonbuch.geschaeftsstelle}" layout="grid" columns="3">
<f:selectItems value="#{telefonbuch.geschaeftsstellen}" var="c" itemLabel="#{geschaeftsstelle}" itemValue="#{geschaeftsstelle}"/>
</p:selectOneRadio>
</h1:panelGrid>
My model class looks like this:
#Data
#Entity
public class Telefonbuch {
#PostConstruct
public void init() {
geschaeftsstellen = new ArrayList<String>();
geschaeftsstellen.add("Dortmund");
geschaeftsstellen.add("Essen");
geschaeftsstellen.add("Stralsund");
geschaeftsstellen.add("Stuttgart");
geschaeftsstellen.add("Zürich");
geschaeftsstellen.add("Istanbul");
geschaeftsstellen.add("Köln");
geschaeftsstellen.add("Aachen");
geschaeftsstellen.add("Berlin");
}
public String getGeschaeftsstelle() {
return geschaeftsstelle;
}
public void setGeschaeftsstelle(String geschaeftsstelle) {
this.geschaeftsstelle = geschaeftsstelle;
}
public List<String> getGeschaeftsstellen() {
return geschaeftsstellen;
}
public void setGeschaeftsstellen(List<String> geschaeftsstellen) {
this.geschaeftsstellen = geschaeftsstellen;
}
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column
private String vorname;
#Column
private String nachname;
#Column
private String telefonnummer;
#Column
private String handynummer;
#Column
private String geschaeftsstelle;
#Column
#ElementCollection
private List<String> geschaeftsstellen;
protected Telefonbuch() {
}
public Telefonbuch(String vorname, String nachname, String telefonnummer, String handynummer) {
this.vorname = vorname;
this.nachname = nachname;
this.telefonnummer = telefonnummer;
this.handynummer = handynummer;
}
I don't know what the problem is. The buttons won't be shown. Just the heading is there. No choices.

Just clean your project and it should work fine.
I don't know is it your case, but whenever i have similar problem, cleaning project and reloading server solved it for me.
EDIT:
I will assume that #Data and #Entity are valid identifiers in this case (because i am always using #ManagedBean, and store it in some DTO class later.
Also i think that this line:
<f:selectItems value="#{telefonbuch.geschaeftsstellen}" var="c" itemLabel="#{geschaeftsstelle}" itemValue="#{geschaeftsstelle}"/>
should look like this:
<f:selectItems value="#{telefonbuch.geschaeftsstellen}" var="c" itemLabel="#{c.geschaeftsstelle}" itemValue="#{c.geschaeftsstelle}"/>

Related

Set a default input value that can be editable

I would like to create a JSF application
Where a user must provide their age. This will default to 18 and be editable (If the user wants to provide a different value).
How to achieve this functionality?
The input value provided by the user or the default value ie which is 18 would be saved in the Database
<h:form>
<p:panel id="panel" header="New User">
<h:panelGrid columns="3" cellpadding="5">
<p:outputLabel for="age" value="Age:" />
<p:inputText id="age" value="#{managedBean.entityBean.age}" required="true" label="Age">
</p:inputText>
</h:panelGrid>
<p:commandButton value="Save" update="panel" action="#{managedBean.save}" />
</p:panel>
</h:form>
Entity.java
public class Entity implements Serializable {
#Column(name = "AGE")
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
managedBean.java
#ManagedBean(name = "managedBeanJava")
#ViewScoped
public class managedBeanJava implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(managedBeanJava.class);
private Entity entityBean;
public Entity getEntityBean() {
return entityBean;
}
public void setEntityBean(Entity entityBean) {
this.entityBean = entityBean;
}
private Integer age = 50;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
#PostConstruct
public void init() {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, String> params = externalContext.getRequestParameterMap();
}
public void save(AjaxBehaviorEvent event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
try {
entityBean.setAge(this.entityBean.getAge());
}
}
You could define default value of <h:inputText> in your managed bean directly.
private Integer age = 18;

Display List<List> in <p:dataTable><p:columns>

I am trying to display below list of list of object in datatable. But nothing is showing up. Help is very much appreciated!
public class TimrsDisplayBean {
private static final long serialVersionUID = 1L;
private String teamName = "";
private String teamType = "";
private boolean reported;
private boolean noProd;
private boolean missing;
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getTeamType() {
return teamType;
}
public void setTeamType(String teamType) {
this.teamType = teamType;
}
public boolean getReported() {
return reported;
}
public void setReported(boolean reported) {
this.reported = reported;
}
public boolean getNoProd() {
return noProd;
}
public void setNoProd(boolean noProd) {
this.noProd = noProd;
}
public boolean getMissing() {
return missing;
}
public void setMissing(boolean missing) {
this.missing = missing;
}
}
XHTML FILE
<p:dataTable value="#{dashboardMBean.timrsDisplayDataList}" var="var" rowIndexVar="row"
styleClass="large-card-datatable alternatingRowTable no-border nowrap">
<f:facet name="header">
<span class="updateDate"> </span>
</f:facet>
<p:column headerText="Type" value=" #{dashboardMBean.timrsDisplayDataList[0]}" columnIndexVar="i">
#{var[i].teamType}
</p:column>
<p:column headerText="Type" value=" #{dashboardMBean.timrsDisplayDataList[0]}" columnIndexVar="i">
#{var[i].teamName}
</p:column>
Please support full code parts if possible. Your datatable tag doesn't even finish.
Beside that, it seems like you've done too much work. Did you try to calculate the row indices by yourself? Not needed. The basic column definiton is more comfortable. Use your defined variable var to name each element/row as shown in the Primefaces showcase:
http://www.primefaces.org/showcase/ui/data/datatable/basic.xhtml
In your case, it will be something like
<p:column headerText="Type">
<h:outputText value="#{var.teamType}"/>
</p:column>
Plus, as Jasper De Vries said correctly, there is no attribute called columnIndexVar. Remove it to prevent strange behaviour.
If this is not enough, you need to share some more code of your ManagedBean. Not sure if your posted java class represent your Bean, but if so, you need to declare it as a ManagedBean like
#ManagedBean(name = "timrsDisplayBean)
#SessionScoped
public class TimrsDisplayBean {
I hope this helps!

Passing parameters in JSF and PrimeFaces

I am studying a PrimeFaces AutoComplete demo. I shortenied it from the full showcase demo. http://www.primefaces.org/showcase/ui/input/autoComplete.xhtml
AutoCompleteBean.java
#ManagedBean
public class AutoCompleteBean {
private Query query;
private List<Query> queries = new ArrayList<Query>();
#PostConstruct
public void init() {
queries.add(new Query(0, "Afterdark", "afterdark"));
queries.add(new Query(1, "Afternoon", "afternoon"));
queries.add(new Query(2, "Afterwork", "afterwork"));
queries.add(new Query(3, "Aristo", "aristo"));
}
public List<Query> completeQuery(String query) {
List<Query> filteredQueries = new ArrayList<Query>();
for (int i = 0; i < queries.size(); i++) {
Query skin = queries.get(i);
if(skin.getName().toLowerCase().contains(query)) {
filteredQueries.add(skin);
}
}
return filteredQueries;
}
public void onItemSelect(SelectEvent event) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Item Selected", event.getObject().toString()));
}
public Query getQuery() {
return query;
}
public void setQuery(Query query) {
this.query = query;
}
}
Query.java
public class Query {
private int id;
private String displayName;
private String name;
public Query() {}
public Query(int id, String displayName, String name) {
this.id = id;
this.displayName = displayName;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return name;
}
}
I omitted a Convert class, which I think is not that relevant.
search.xhtml
<h:form>
<p:growl id="msgs" showDetail="true" />
<h:panelGrid columns="2" cellpadding="5">
<p:autoComplete id="queryPojo" value="#{autoCompleteView.query}"
completeMethod="#{autoCompleteView.completeQuery}" var="query"
itemLabel="#{query.displayName}" itemValue="#{query}"
converter="queryConverter" forceSelection="true" />
<p:commandButton value="search" oncomplete="PF('dlg').show()"/>
</h:panelGrid>
</h:form>
I have three questions for this:
1) completeMethod="#{autoCompleteView.completeQuery}": completeQuery method is called without passing a parameter, but it's defined as completeQuery(String query). How does this work?
2) value="#{autoCompleteView.query}". Query is an object defined in AutoCompleteBean. How can this Query object take user input string as its value? Usually InputText's value is good for taking user's input, which is a String value.
3) Can I still add an attribute "action=..." to the p:autoComplete componenet?
The converter class that you omitted here plays the real game.... Lets see your questions
As you see converter class overrides 2 methods
getAsString and getAsObject
1)the value
completeMethod="#{autoCompleteView.completeQuery}
gets refactred to
autoCompleteView.completeQuery(autoCompleteView.query);
as you can find to string method in Query class.
2).as converter is defined for autocomplete it calls getAsString method to render on screen. when selected getAsObject method is called to convert string value to object(Query).
3)you can use ajax select event
<p:ajax event="select" listener="#{autoCompleteView.someMethod}">
or call a remoteCommand by onSelect attribute in p:autoComplete
<p:autoComplete id="queryPojo" value="#{autoCompleteView.query}" onSelect="someRemoteCommand();"
completeMethod="#{autoCompleteView.completeQuery}" var="query"
itemLabel="#{query.displayName}" itemValue="#{query}"
converter="queryConverter" forceSelection="true" />
<p:remoteCommand name="someRemoteCommand" update="queryPojo" actionListener="#{autoCompleteView.execute}" />

How to manipulate Java List with jsf RequestScope

I'm using JSF 2.2 with RequestScoped managebean. I'd like to update a Java List inside of Customers entity. When submitting, only info in Java List (List<'Phone >) is gone. I don't want to use SessionScoped. Could anyone share some tips or solve this issue?
My codes are shown here.
Customers Entity:
{ ..
#Column(name = "FIRSTNAME")
private String firstname;
#Column(name = "LASTNAME")
private String lastname;
...
#ElementCollection
#CollectionTable(name = "CUSTOMERS_Phone",
joinColumns = #JoinColumn(name = "CUSTOMERS_ID"))
#AttributeOverride(name = "teleNumbers",
column = #Column(name = "PHONE_NUMBER",length=30))
private List<Phone> phone;
..}
JSF Managebean:
#Named(value = "editCustomersBeanService")
#RequestScoped
public class EditCustomersBeanService implements Serializable {
/**
* Creates a new instance of EditCustomersBeanService
*/
#PostConstruct
public void init() {
ctx = FacesContext.getCurrentInstance();
customers =new Customers();
phones = new ArrayList<>();
customers.setPhone(phones);
}
public EditCustomersBeanService() {
}
#Inject
private BusinessSessionCustomers businessSSCustomers;
private int customerId;
private List<Phone> phones;
private Customers customers;
//setter, getter ...
//update to DB
public String updatedCustomers() {
System.out.println("customer Name: " + customers.getFirstname());
System.out.println("customer LastName: " + customers.getLastname());
System.out.print("List of Phones in updatedCustomers: ");
for (Phone ph : customers.getPhone()) {
System.out.print(ph.getPhoneType() + ", " + ph.getTeleNumbers());
}
businessSSCustomers.mergeToDB(customers);
return "customers";
}
..
}
CustomersEdit.xhtml:
<h:form>
...
<label for="Last Name">Last Name</label>
<h:inputText id="lastName" p:placeholder="Last Name"
value="#{editCustomersBeanService.customers.lastname}"/>
<ui:repeat var="phone" value="#{editCustomersBeanService.customers.phone}" varStatus="status">
<label for="phones">Phone: [#{status.index}]</label>
<h:selectOneMenu value="#{phone.phoneType}">
<f:selectItems value="#{editCustomersBeanService.phoneTypeList}"
itemLabel="#{editCustomersBeanService.phoneType}"
itemValue="#{editCustomersBeanService.phoneType}"/>
</h:selectOneMenu>
<h:inputText class=" form-control" value="#{phone.teleNumbers}" />
</ui:repeat>
<h:commandLink value="Save" action="#{editCustomersBeanService.updatedCustomers()}" />
…
</h:form>
Phone.java:
#Embeddable
public class Phone {
public enum PhoneType {
Home, Mobile, Work
}
#Enumerated(EnumType.STRING)
#Column(name = "PHONE_TYPE", length = 10)
private PhoneType phoneType;
#Column(name = "PHONE_NUM", length = 30)
private String teleNumbers;
//setter, getter
..
}

DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.

I'm trying to create a DataTable with Multiple Row Selection but i'm getting an error here's the link of the tutorial http://www.primefaces.org/showcase/ui/datatableRowSelectionMultiple.jsf :
Here's my xhtml:
<p:dataTable border="1" value="#{projectAdminisrationMB.projectNoUsersList}"
var="userObj"
selection="#
{projectAdminisrationMB.selectedUsers}"
selectionMode="multiple" rowIndexVar="rowIndex"binding="#{table2}">
<p:column id="column3">
<f:facet name="header">
<h:outputText value=" user "></h:outputText>
</f:facet>
<h:outputText value="#{userObj.name}"/>
/
<h:outputText value="#{userObj.lastName}"></h:outputText>
<h:outputText value="#{userObj.firstName}"></h:outputText>
</p:column>
<f:facet name="footer">
<p:commandButton id="addProjectUser" value=" Add " onclick="dlg1.show()" />
<p:commandButton id="deleteProjectUser" value=" Delete " />
</f:facet>
</p:dataTable>
Managed Bean :
#ManagedBean
#SessionScoped
public class ProjectAdminisrationMB implements Serializable {
private static final long serialVersionUID = 1L;
private String projectName;
private List <User> projectUsersList;
private List<User> projectNoUsersList;
private List<User> selectedUsers;
private String projectAdmin;
public ProjectAdminisrationMB() {
super();
AdministrationProjectFinal administrationProjectFinal =new
AdministrationProjectFinal();
this.projectUsersList=administrationProjectFinal.getUserList();
this.projectNoUsersList=administrationProjectFinal.getNotUserList();
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public List<User> getProjectUsersList() {
return projectUsersList;
}
public void setProjectUsersList(List<User> projectUsersList) {
this.projectUsersList = projectUsersList;
}
public String getProjectAdmin() {
return projectAdmin;
}
public void setProjectAdmin(String projectAdmin) {
this.projectAdmin = projectAdmin;
}
public List<User> getProjectNoUsersList() {
return projectNoUsersList;
}
public void setProjectNoUsersList(List<User> projectNoUsersList) {
this.projectNoUsersList = projectNoUsersList;
}
public List<User> getSelectedUsers() {
return selectedUsers;
}
public void setSelectedUsers(List<User> selectedUsers) {
this.selectedUsers = selectedUsers;
}
}
i'm getting this error:
javax.faces.FacesException: DataModel must implement
org.primefaces.model.SelectableDataModel when selection is enabled.....
just add this attribute rowKey to the datatable tag :
<p:dataTable border="1" value="#{projectAdminisrationMB.projectNoUsersList}"
var="userObj"
rowKey="#{userObj.name}"selection="#{projectAdminisrationMB.selectedUsers}"
selectionMode="multiple" rowIndexVar="rowIndex"
binding="#{table2}">
You can get this error if you try to add a new item to the underlying list and forget to assign a value to that new item's rowKey.
Alternatively to rowKey you can wrap your data in a custom model which really implements org.primefaces.model.SelectableDataModel. This is helpful if
all of your your classes have the same kind of #Id (e.g. a long) and can implement the same interface (e.g. EjbWithId)
you want to add additional functionalities to your data which are not domain specific and don't belong e.g. User.
The interface may be something like this:
public interface EjbWithId
{
public long getId();
public void setId(long id);
}
Then a generic implementation of SelectableDataModel for all your classes can be used:
public class PrimefacesEjbIdDataModel <T extends EjbWithId>
extends ListDataModel<T> implements SelectableDataModel<T>
{
public PrimefacesEjbIdDataModel(List<T> data)
{
super(data);
}
#Override public T getRowData(String rowKey)
{
List<T> list = (List<T>) getWrappedData();
for(T ejb : list)
{
if(ejb.getId()==(new Integer(rowKey))){return ejb;}
}
return null;
}
#Override public Object getRowKey(T item) {return item.getId();}
}
In your #ManagedBean:
private PrimefacesEjbIdDataModel<User> dmUser; //+getter
dmUser = new PrimefacesEjbIdDataModel<User>(administrationProjectFinal.getUserList());
first check whether you've added
rowKey="#{userObj.id}"
then you need to have the data table List set in filteredValue attribute of your data table in xhtml, instead of value.

Resources