p:commandbutton doesn't call method - jsf

I got a problem where one of my commandbuttons doesn't call the method I gave it and I have no idea why:
<p:layoutUnit position="center">
<h:form>
<p:dialog position="center" widgetVar="benutzerErstellen" visible="true" closable="false" resizable="false" draggable="false">
<h:panelGrid columns="2" cellpadding="5" styleClass="noBorders">
<h:outputLabel for="nachname" value="Nachname:" />
<p:inputText id="nachname" value="#{benutzerverwaltung.nachname}" required="true"/>
<h:outputLabel for="vorname" value="Vorname:" />
<p:inputText id="vorname" value="#{benutzerverwaltung.vorname}" required="true"/>
<h:outputLabel for="benutzername" value="Benutzername:" />
<p:inputText id="benutzername" value="#{benutzerverwaltung.benutzername}" required="true" validator="#{benutzerverwaltung.benutzerUeberschneidungValidate}"/>
<h:outputLabel for="teams" value="Teams:" />
<p:selectCheckboxMenu id="teams" label="Teams" scrollHeight="200" panelStyle="width:300px">
<f:selectItems/>
</p:selectCheckboxMenu>
<h:outputLabel for="passwort" value="Passwort:" />
<p:password id="passwort" feedback="true" value="#{benutzerverwaltung.passwort}" required="true" match="passwortBestaetigung" promptLabel="Bitte geben Sie ein Passwort ein." weakLabel="Schwach" goodLabel="Gut" strongLabel="Stark"/>
<h:outputLabel for="passwortBestaetigung" value="Passwort bestätigen:" />
<p:password id="passwortBestaetigung" value="#{benutzerverwaltung.passwort}" required="true"/>
<f:facet name="footer">
<p:commandButton id="erstellen" value="Benutzer erstellen" action="#{benutzerverwaltung.erstelleBenutzer()}"/>
</f:facet>
</h:panelGrid>
</p:dialog>
<p:growl/>
</h:form>
</p:layoutUnit>
This is the corresponding code:
erstelleBenutzer:
public void erstelleBenutzer(){
Benutzer benutzer = new Benutzer();
System.out.println(vorname);
benutzer.setVorname(vorname);
benutzer.setNachname(nachname);
benutzer.setBenutzername(benutzername);
benutzer.setPasswort(passwort);
this.benutzer.add(benutzer);
FacesMessage message = new FacesMessage("Benutzer wurde erstellt.");
message.setSeverity(FacesMessage.SEVERITY_INFO);
throw new ValidatorException(message);
}
benutzerUeberschneidungValidate:
public void benutzerUeberschneidungValidate(FacesContext ctx, UIComponent component, Object value) throws ValidatorException {
if (benutzerExistiert((String) value) || adminExistiert((String) value)) {
FacesMessage message = new FacesMessage("Benutzer existiert bereits.");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}
I tried removing the validator, I tried removing the Exception from benutzerErstellen(), I tried giving the dialog it's own h:form and much more, but nothing worked. Anyone got an idea?

Earlier versions of PrimeFaces 3 have a bug wherein a commandButton or commandLink doesn't fire the action listener if it is located in the header or the footer.
See the post I have listed at the bottom.
p:commandButton action and f:setpropertyactionlistener not invoked in p:columngroup

I solved this like that
<p:dataTable>
...
<p:column>
...
<p:commandButton id="selectButton" update=":form:dataTable" icon="ui-icon-circle- close" title="Delete" action="#{myBean.doSomething}">
<p:ajax event="click" listener="#{mainPageBean.doRemoveEmployee(employee)}"/>
</p:commandButton>
...
</p:column>
...
</p:dataTable>

Related

Datatable is not updated after actionListener

I'm facing some problem updating my dataTable when I perform a filter operation with this:
<p:panel styleClass="card" header="Filtri">
<h:panelGrid columns="4" layout="grid"
styleClass="ui-panelgrid-blank form-group">
<h:panelGroup styleClass="md-inputfield">
<p:inputText id="orderMinFilter" label="N. bolla più basso"
value="#{productionOrdersView.orderNumberStartFilter}">
</p:inputText>
<p:outputLabel for="orderMinFilter" value="N. bolla più basso" />
</h:panelGroup>
<h:panelGroup styleClass="md-inputfield">
<p:inputText id="orderMaxFilter" label="N. bolla più alto"
value="#{productionOrdersView.orderNumberEndFilter}">
</p:inputText>
<p:outputLabel for="orderMaxFilter" value="N. bolla più alto" />
</h:panelGroup>
<p:commandButton value="Cancella filtri" icon="ui-icon-clear"
styleClass="red-btn flat"
actionListener="#{productionOrdersView.clearFilter()}"
style="width:auto;margin-bottom:10px; float:right;"
update="dtOrders" />
<p:commandButton value="Cerca" icon="ui-icon-search"
styleClass="primary-btn flat"
actionListener="#{productionOrdersView.filter()}"
update="dtOrders"
style="width:auto;margin-bottom:10px; float:right;" />
</h:panelGrid>
</p:panel>
This is my dataTable:
<p:dataTable id="dtOrders" var="productionOrder"
value="#{productionOrdersView.orders}" selectionMode="single"
reflow="true" selection="#{productionOrdersView.selected}"
filteredValue="#{productionOrdersView.filtered}"
widgetVar="ordersTable" rowKey="#{productionOrder.pk}"
emptyMessage="Nessun elemento oppure la ricerca è ancora attiva..."
paginator="true" rows="20">
And this is the method:
public void filter() {
if (orderNumberStartFilter != null && orderNumberEndFilter != null) {
this.orders = productionOrderController.findFromToOrderNumber(Integer.valueOf(orderNumberStartFilter),
Integer.valueOf(orderNumberEndFilter));
this.filtered = orders;
}
}
The filter() method is executed correctly and the list in the bean are correct (I search for 11 elements and I got it), but the dataTable is updated but not with these values; why? Where am I wrong?
You add form tag to update property of commandButton.
<p:commandButton value="Cerca" icon="ui-icon-search"
styleClass="primary-btn flat"
actionListener="#{productionOrdersView.filter()}"
update=":formName:dtOrders"
style="width:auto;margin-bottom:10px; float:right;" />
.

Pass parameter from a datatable to a p:dialog [duplicate]

This question already has an answer here:
How to show details of current row from p:dataTable in a p:dialog and update after save
(1 answer)
Closed 6 years ago.
The following code implements a datatable inside a layout, in the datatable i added an edit button in each row
<p:dataTable id="tbl" var="person" value="#{mybean.listPersons}" >
<p:column>
<f:facet name="header">
<h:outputText value="Name " />
</f:facet>
<h:outputText value="#{person.name}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Age :" />
</f:facet>
<h:outputText value="#{person.age}" />
</p:column>
<p:column>
<p:commandButton icon="ui-icon-pencil"
oncomplete="PF('dlg1').show();" action="mybean.setSelectedPerson(person)" />
</p:column>
</p:dataTable>
When i click on the edit button, the dialog box (code below) is shown but the inputs are empty, what i wanted is to show the infos of the row in the dialog bow, i'm still a beginner, i searched everywhere... but no results
<p:dialog header="Modify" widgetVar="dlg1" >
<h:form >
<p:growl id="msgs" showDetail="true" />
<h:panelGrid id="form2" value="#{myBean.person}" var="person">
<p:outputLabel value="Name :" />
<p:inputText value="#{person.name}" />
<p:outputLabel value="Age :" />
<p:inputText value="#{person.age}" />
<p:commandButton value="Submit" action="#{myBean.modifyPerson(person)}" />
</h:panelGrid>
</h:form>
</p:dialog>
#ManagedBean
#RequestScoped
public class muBean implements Serializable{
private Person selectedPerson;
//getter and setter
public void modifyPerson(Person p) {
this.selectedPerson = p;
}
}
i would be so grateful if anyone can help, i really need this
Change the command button to the following, use an actionlistener:
<p:commandButton icon="ui-icon-pencil" update=":persondlgid" oncomplete="dlg1.show();" actionListener ="mybean.findSelectedPerson">
<f:param name="personalid" value="#{person.id}" />
<p:commandButton/>
This is the dialog, add the id property to it. Then change the value of the panel grid to selectedPerson because this corresponds to the correct object in the managedbean:
<p:dialog header="Modify" widgetVar="dlg1" id="persondlgid" >
<h:form>
<p:growl id="msgs" showDetail="true" />
<h:panelGrid id="form2" value="#{myBean.selectedPerson}" var="person">
<p:outputLabel value="Name :" />
<p:inputText value="#{person.name}" />
<p:outputLabel value="Age :" />
<p:inputText value="#{person.age}" />
<p:commandButton value="Submit" action="#{myBean.modifyPerson(person)}" />
</h:panelGrid>
</h:form>
</p:dialog>
The managed bean function should look as follows. This action listener is called when the button is clicked and it then retrieves the id of the selected person and loops through the list of persons to find the one you are looking for:
public void findSelectedPerson(ActionEvent event){
if(event.getComponent().getAttributes().get("personid") != null){
Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
int personid = (params.get("personid")!= null) ? Integer.parseInt(params.get("personid")) : -1;
// Loop through the persons array
for(Person p : listPersons){
if(p.getId() == personid){
selectedPerson = p;
break;
}
}
}

How to hide/show selectOneMenu in JSF? [duplicate]

This question already has an answer here:
Ajax update/render does not work on a component which has rendered attribute
(1 answer)
Closed 7 years ago.
I have an inputText and a selectOneRadio component, when form load selectOneRadio should be hidden. When user select inputText, I want to show the selectOneRadio component. I have made the selectOneRadio hidden in #PostConstruct, which should be display on select inputText.
<h:panelGrid id="panelgrid">
<p:panel id="panel" >
<h:outputLabel value="Name: " for="name" />
<p:inputText id="name" value="#{userBean.name}" immediate="true">
<p:ajax event="onselect" update="city" listener="#{userBean.showName}" />
</p:inputText>
<p:selectOneRadio id="city" value="#{userBean.city}" layout="grid" columns="3" rendered="#{userBean.displayName}" >
<f:selectItems value="#{userBean.cities}" var="c" itemLabel="#{city}" itemValue="#{city}" />
</p:selectOneRadio>
</p:panel>
</h:panelGrid>
The bean code is like:
#PostConstruct
public void init() {
displayName = false;
}
public boolean isShowName() {
return true;
}
...
But some how this is not working. I'm using JSF2.0 with primefaces 5.2.
I found the solution.
<h:form id="frm">
<h:panelGrid id="panelgrid">
<p:panel id="panel" >
<h:outputLabel value="Name: " for="name" />
<p:inputText id="name" value="#{userBean.name}" immediate="true" onkeypress="#{userBean.showName}">
<p:ajax event="change" update="frm:panelgrid" listener="#{userBean.inputcangeeListener}" />
</p:inputText>
</p:panel>
<p:panel id="p2" rendered="#{userBean.displayName}">
<p:selectOneRadio id="city" value="#{userBean.city}" layout="grid" columns="3">
<f:selectItems value="#{userBean.cities}" var="c" itemLabel="#{city}" itemValue="#{city}" />
</p:selectOneRadio>
</p:panel>
</h:panelGrid>
</h:form>
And my managed bean looks like-
#PostConstruct
public void init() {
displayName = false;
}
public boolean isShowName(){
return displayName;
}
public void inputcangeeListener(javax.faces.event.AjaxBehaviorEvent changeEvent){
setDisplayName(true);
cities = new ArrayList<>();
cities.add("pune");
cities.add("KOL");
}
Thanks for your response.

Primefaces command button event is not invoked

In Primefaces, a button will call a prepareCreate method and then open the form dialog. The issue is, this method never get called so that the form in the dialog is not shown.
Command button
<p:commandButton id="createButton1" value="#{bundle.Create}" actionListener="#{purchaseOrderController.prepareCreate}"
update=":PurchaseOrderCreateForm11" oncomplete="PF('PurchaseOrderCreateDialog11').show()"/>
Dialog
<p:dialog id="PurchaseOrderCreateDlg11" widgetVar="PurchaseOrderCreateDialog11" modal="true" resizable="false" appendTo="#(body)" header="#{bundle.CreatePurchaseOrderTitle}">
<h:form id="PurchaseOrderCreateForm11">
<h:panelGroup id="display">
<p:panelGrid columns="2" rendered="#{purchaseOrderController.selected != null}">
<p:outputLabel value="#{bundle.CreatePurchaseOrderLabel_orderNum}" for="orderNum" />
<p:inputText id="orderNum" value="#{purchaseOrderController.selected.orderNum}" title="#{bundle.CreatePurchaseOrderTitle_orderNum}" required="true" requiredMessage="#{bundle.CreatePurchaseOrderRequiredMessage_orderNum}"/>
<p:outputLabel value="#{bundle.CreatePurchaseOrderLabel_quantity}" for="quantity" />
<p:inputText id="quantity" value="#{purchaseOrderController.selected.quantity}" title="#{bundle.CreatePurchaseOrderTitle_quantity}" />
<p:outputLabel value="#{bundle.CreatePurchaseOrderLabel_shippingCost}" for="shippingCost" />
<p:inputText id="shippingCost" value="#{purchaseOrderController.selected.shippingCost}" title="#{bundle.CreatePurchaseOrderTitle_shippingCost}" />
</p:panelGrid>
<p:commandButton actionListener="#{purchaseOrderController.create}" value="#{bundle.Save}" oncomplete="handleSubmit(args,'PurchaseOrderCreateDialog11');"/>
<p:commandButton value="#{bundle.Cancel}" onclick="PurchaseOrderCreateDialog11.hide()"/>
</h:panelGroup>
</h:form>
</p:dialog>
JSF managed bean
#Named("purchaseOrderController")
#SessionScoped
public class PurchaseOrderController implements Serializable {
public PurchaseOrder prepareCreate() {
System.out.println("Purchase order prepare create");
selected = new PurchaseOrder();
initializeEmbeddableKey();
return selected;
}
}

JSF FacesContext#addMessage is not displayed

In my previous question I had the problem of displaying validation messages from a Login form. That issue is now solved, but this time I am not able to display a custom message with FacesContex#addMessage.
Using JSF + PrimeFaces.
<p:dialog header="Login" widgetVar="loginDlg">
<h:form id="loginForm">
<h:panelGrid columns="3" cellpadding="5">
<h:outputLabel for="username" value="Username:" />
<p:inputText value="#{loginBean.username}" id="username" required="true" label="username" />
<p:message for="username" />
<h:outputLabel for="password" value="Password:" />
<h:inputSecret value="#{loginBean.password}" id="password" required="true" label="password" />
<p:message for="password" />
<f:facet name="footer">
<p:commandButton value="Login" id="loginDlgButton" update=":loginForm,:welcomeMsg" actionListener="#{loginBean.login}"
oncomplete="handleLoginRequest(xhr, status, args)"/>
<p:message for="loginDlgButton" />
</f:facet>
</h:panelGrid>
</h:form>
</p:dialog>
In LoginBean (a SessionScoped ManagedBean):
public void login() {
FacesContext context = FacesContext.getCurrentInstance();
RequestContext rContext = RequestContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
try {
request.login(this.username, this.password);
rContext.addCallbackParam("loggedIn", true);
} catch (ServletException e) {
rContext.addCallbackParam("loggedIn", false);
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials"));
}
}
This code, when validation succeeds and login fails, should display the "Invalid credential" message, but doesn't. Moreover, somewhere in the body of my web page, I have also added this line:
<p:messages autoUpdate="true" />
but my message isn't displayed even there.
Javadocs say that
If clientId is null, this FacesMessage is assumed to not be associated with any specific component instance
But I can't understand what this means.
place <p:messages autoUpdate="true" /> inside your form or inside some wrapper that is being updated by update of your commandButton , or place loginDlgButton instead of null in context.addMessage(...
I don't see a p:messages tag in your code. It is not the same as the p:message tag. p:message is attached to another component and is displayed as part of validation. The p:messages (or p:growl) component is what you are updating in your bean. Try adding a messages or growl component like this:
<h:form id="loginForm">
<p:growl id="messageGrowl" showDetail="true" sticky="false" />
<h:panelGrid columns="3" cellpadding="5">

Resources