This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?
(2 answers)
Closed 3 years ago.
I am doing a mini jsf project that is created by using jpa. I have listed costumers and I can remove them one by one, but if I want to remove selected costumers I cannot do it.
This part is from Costumer class
#Transient
private Boolean selection = false;
And this part is from CostumerBean
Costumer costumer = new Costumer();
private List<Costumer> costumerList = new ArrayList<>();
//(getters and setters)
public void removeSelected() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
for (Costumer cos : costumerList) {
if (cos.getSelection()) {
em.remove(em.contains(costumer) ? customer : em.merge(costumer));
}
}
em.getTransaction().commit();
musteri = new Musteri();
}
And finally here is the xhtml page :
<h:form id="firstForm">
<p:commandButton action="#{customerBean.removeSelection}" value="Remove"
update="customerForm">
</p:commandButton>
</h:form id="firstForm">
<h:form id="customerForm">
<p:dataTable value="#{customerBean.customerList}" var="cst"
id="cstTable">
<p:column>
<f:facet name="header">Select</f:facet>
<p:selectBooleanCheckbox value="#{cst.selection}" />
</p:column>
<p:column>
<f:facet name="header">Name</f:facet>
#{cst.name}
</p:column>
</h:form id="customerForm">
I reckon actual problem is I cannot make selection field in Costumer.java true. No action I see.?
Related
This question already has answers here:
Primefaces widgetVar interfering with ui:repeat or ui:datatable
(2 answers)
Closed 3 years ago.
I have a data table built with Primefaces. It contains a list of "Person" . "Person" is a Java class with only 1 String attribute - "name", has constructor with no parameters, constructor with 1 String parameter, getter and setter for "name". First column of the table contains the person name. Second column contains a commandButton. By pressing it, is is opened a dialog. In this dialog I want to see the name of the selected person.
The JSF page is this:
<h:form id="personForm" prependId="false">
<p:dataTable id="personEntitiesTable" var="pers" value="#{personView.persons}">
<p:column headerText="Name">
<h:outputText value="#{pers.name}" />
</p:column>
<p:column headerText="">
<p:commandButton value="Details" type="button" onclick="PF('personDetails').show();" />
<p:dialog header="Person details" widgetVar="personDetails" modal="true" height="150" >
Selected person is "#{pers.name}"
</p:dialog>
</p:column>
</p:dataTable>
</h:form>
The managedBean for the table is:
#ManagedBean
#ViewScoped
public classPersonView implements Serializable {
private List<Person> persons;
#PostConstruct
public void init() {
persons = new ArrayList<Person>();
persons.add(new Person("John"));
persons.add(new Person("Mary"));
persons.add(new Person("James"));
}
// getters and setters
}
So, if I press the button "Details" for the user "Mary", it is opened a dialog where I expect to see the message "Selected person is Mary". The problem is no matter the row where I press the button "Details", the message inside dialog is "Selected person is James" . "James" is the last person from the list.
As the dialog was declared inside datatable column, JSF will generate one dialog for each table row. Since all of them use same widgetVar, the last one will be called when execute PF('personDetails').show().
Try this way:
Put your dialog outside of datatable.
Your dialog must use a managed bean property (personView.selectedPerson).
Details button must set personView.selectedPerson with pers variable:
<f:setPropertyActionListener target="#{personView.selectedPerson}" value="#{pers}"/>
and update the dialog content.
This question already has answers here:
How and when should I load the model from database for JSF dataTable
(1 answer)
How to choose the right bean scope?
(2 answers)
Closed 3 years ago.
I have a problem where the getter of a datatable is called multiple times, and I see thats fine here Primefaces dataTable call method multiple times when click commandButton. why?
The problem is that is giving me a datatable with no results when there is actually results, because at somepoint it will be null, I did a workaround for this with a static variable but I don't know if this can give me a problem in the future (please read im gonna put you in context with the question). here is my code of the datatable:
<p:dialog id="dialogMenu" appendTo="#(body)" widgetVar="dialogMenuwv"
closable="true" closeOnEscape="true">
<h:form id="formDialogMenu">
<p:dataTable value="#{homeView.data1}" var="plato" id="tablaMenu"
resizableColumns="false" tableStyle="width:auto"
emptyMessage="No hay platos disponibles" paginator="true" rows="5">
<f:facet name="header">
Platos
</f:facet>
<p:column filterBy="#{plato.activo}" sortBy="#{plato.activo}">
<f:facet name="header">
<h:outputText value="Activo" />
</f:facet>
<h:outputText value="#{plato.activo}" />
</p:column>
<p:column filterBy="#{plato.nombre}" sortBy="#{plato.nombre}">
<f:facet name="header">
<h:outputText value="Nombre" />
</f:facet>
<h:outputText value="#{plato.nombre}" />
</p:column>
<p:column filterBy="#{plato.precio}" sortBy="#{plato.precio}">
<f:facet name="header">
<h:outputText value="Precio" />
</f:facet>
<h:outputText value="#{plato.precio}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Imagen" />
</f:facet>
<p:graphicImage alt="image" width="100%" height="50%"
value="#{plato.imagenPlato}" cache="false">
</p:graphicImage>
</p:column>
</p:dataTable>
</h:form>
</p:dialog>
As you can see the table is inside a dialog the one is opened when this button is clicked:
<p:commandButton value="Ver Menu" update="formDialogMenu:tablaMenu"
actionListener="#{homeView.getData1()}"
onclick="PF('dialogMenuwv').show()"></p:commandButton>
I had to add that actionListener and update to my button because I notice that leaving it only with the onlick to show the dialog wouldn't work and I followed advice from linked answer.
Finally here is my getter method as I expected it to work and it would work if it was only called when the marker was selected (Im working with gMap):
public List<PlatoDTO> getData1() {
try {
if (data1 == null) {
// puestocomida isn't null when the marker is selected but since the getter is being called so many times, puestocomida will be null later and so data1 will return null.
data1 = businessDelegatorView.getDataPlatoPuesto(puestocomida.getPueId());
}
} catch (Exception e) {
e.printStackTrace();
}
return data1;
}
while debugging I notice that the getter is called multiple times and for some reason my puestocomida variable becomes null, I guess it is because no marker selected anymore, so an exception is throwed there and I get a null returned. So I decided to make a static variable that will store my puestocomida.getpueId() and so I modified the getter like this:
// I made this static to store the id I need for the getter
private static Integer pueId;
public List<PlatoDTO> getData1() {
try {
data1 = businessDelegatorView.getDataPlatoPuesto(pueId);
} catch (Exception e) {
e.printStackTrace();
}
return data1;
}
and it is working as I needed it to work like this, but my question is, this variable static will be different or the same for each user that goes into the application? Because it varies depending on the marker that a user selects, I have a googleMap and depending on the marker that a user selects "puestodecomida" is set, and im storing its id on that static variable, this static only updates when a marker is selected.
public void onMarkerSelect(OverlaySelectEvent event) {
// Just making a Puestocomida object out of the selected marker
marker = (Marker) event.getOverlay();
Puestocomida puestocomida = (Puestocomida) marker.getData();
this.puestocomida = puestocomida;
try {
// Saving the puestocomida id in the static variable
data1 = businessDelegatorView.getDataPlatoPuesto(puestocomida.getPueId());
pueId = puestocomida.getPueId();
} catch (Exception e1) {
e1.printStackTrace();
}
puestocomida is restaurant in english and it varies on the marker user selects.
any advice is appreciated :)
This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 7 years ago.
I have problems with invoking commandLink action in dataTable.
<h:body>
<h:dataTable value="#{manageStaffAccountBean.accounts}" var="staff">
<h:column>
#{staff.surname}
</h:column>
<h:column>
<h:form>
<h:commandButton value="Change status"
action="#{manageStaffAccountBean.changeActivity(staff)}" />
</h:form>
</h:column>
</h:dataTable>
</h:body>
By click on "Change status" i need to invoke changeActivity() method in bean
Managed Bean code:
#Named
#Scope("request")
public class ManageStaffAccountBean implements Serializable {
private List<Staff> accounts = null;
public String changeActivity(Staff staff){
System.out.println(staff.getId());
return "manageStaffAccounts";
}
public void updateAccountsList(){
accounts = staffService.findAll();
}
// ...
}
However, it is not invoked. Can you help me to find the problem?
I found the solution. If we use h:commandButton inside h:dataTable, our manage bean should have scope "session", or button wouldn't work. Just JSF magic
This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 7 years ago.
I am using PrimeFaces 5.2 and at a place in my web app. I am displaying a data table is which corresponding to each row. I am giving an edit link (<p:commandLink>) to edit the the fields of corresponding entity on next page but the link is not working at all. Here is the XHTML content.
<p:dataTable id="tickets" var="ticket"
value="#{ticketController.ticketModels}" paginator="true" rows="10"
filteredValue="#{ticketController.filteredTickets}">
<p:column headerText="Ticket Id" sortBy="#{ticket.ticketId}">
<p:commandLink action="viewDetailedTicket">
<h:outputText value=" #{ticket.ticketId}"></h:outputText>
</p:commandLink>
</p:column>
<p:column headerText="Summary" filterBy="#{ticket.summary}"
filterMatchMode="contains" sortBy="#{ticket.summary}">
<h:outputText value="#{ticket.summary}" />
</p:column>
<p:column headerText="Priority">
<h:outputText value="#{ticket.priority}" />
</p:column>
.
...............................
</p:dataTable>
The following is my backing bean:
#ManagedBean
public class TicketController {
#ManagedProperty(value = "#{ticketpojo}")
private Ticket ticket;
private TicketDao tDao;
private TicketModel ticketModel;
public TicketModel getTicketModel() {
return ticketModel;
}
public void setTicketModel(TicketModel ticketModel) {
this.ticketModel = ticketModel;
}
public TicketController() {
tDao = new TicketDao();
}
public String viewDetailedTicket() {
ticketModel = tDao.getTicket(ticketId);
return "viewDetailedTicket";
}
}
When I hover, over the link , the tooltip at the bottom of the browser shows http://localhost:8080/JSF_1/view/viewTicket.xhtml# , a '#' is appended to the end of the current page URL. When clicked it remains on the same page.
I have tried putting the link outside the data-table that also didn't work .
A commandLink or any action that you'd like to do with JSF/primefaces must be inside a form. Otherwise no HTTP Post can be performed. I suggest to use one form for the whole datatable. Otherwise there will be a lot of forms for the multiple rows.
I have a primefaces datatable. I populate it from the database. One of the fields is a boolean represented by a checkbox. I want that if I check or uncheck the checkbox, that I can save the change back to the database.
I have tried passing the current value of the row to the managed bean to save, but the new value of the checkbox isn't reflected in the current row object. How can I get the change into the current row object so I can successfully save the change to the DB?
Here is what I am doing now... I have tried to provide just what is needed. If it is too much information or too little, let me know. Thanks.
#ManagedBean(name = "itemManagerBean")
#ViewScoped
public class ItemManagerBean implements Serializable {
...
public ArrayList<Item> getAllItemsForUser() {
List list = ecf.findByPartyId(user.getPartyId());
ArrayList<Item> itemList = new ArrayList<>(list);
return (itemList);
}
...
public String saveItem(Item item){
System.out.println(item.toString());
ecf.updateRecord(item);
return (null);
}
}
//item class
public class Item {
private BigInteger itemId;
private String name;
priave boolean saleable; //database column is not null
//getters and setters
}
//facelet
<h:form>
<p:dataTable id="id_itemList"
var="item"
value="#{itemManagerBean.allItemsForUser}" >
<p:column headerText="ID">
<h:outputText value="#{item.itemId}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{item.name}" />
</p:column>
<p:column headerText="Saleable" >
<p:selectBooleanCheckbox value="#{item.saleable}" />
</p:column>
<p:column width="15" >
<p:commandButton id="id_saveRowButton" icon="ui-icon-disk"
title="Save" action="#{itemManagerBean.saveItem(item)}"/>
</p:column>
</p:dataTable>
</h:form>
You need to create a selectedItem property in ItemManagerBean and update its value when the user clicks on the commandButton:
In ItemManagerBean
private Item selectedItem;
// getter and setter
In the xhtml page
<p:column width="15" >
<p:commandButton id="id_saveRowButton" icon="ui-icon-disk"
title="Save" action="#{itemManagerBean.saveItem}">
<f:setPropertyActionListener value="#{item}" target="#{itemManagerBean.selectedItem}" />
</p:commandButton>
</p:column>
(Note that you don't need to pass item through saveItem method. Modify saveItem in the managed bean in order to make it work with selectedItem instead of accepting an input item).
Links:
example in the PrimeFaces showcase
Passing parameter to JSF action
BalusC blog