selectOneMenu does not change the value of the bean object - jsf

So I have the following code:
<h:inputText value = "#{listAllBookings.searchText}">
<f:ajax listener="#{listAllBookings.printValues()}" event="keyup" render="myTable"/>
</h:inputText>
<h:selectOneMenu value="#{listAllBookings.selectedAttr}">
<f:selectItem itemLabel="GUEST" itemValue="GUEST"/>
<f:selectItem itemLabel="HOTEL" itemValue="HOTEL"/>
</h:selectOneMenu>
And my printValues method:
public void printValues() {
System.out.println("searchText:" + searchText + " and selectedAttr: " + selectedAttr);
}
So as you can see the code above is pretty simple.
The problem is that I can't change the value of the selectedAttr value. I already checked if I have the appropriate getter and setter methods.
The value of the selectedAttr remains null, while the searchText value changes.
The current output looks like this:

You did not specify a component for execution with your ajax requests. So only the textfield is executed.
Add an id to the selectOneMenu and execute it:
<h:inputText value="#{listAllBookings.searchText}">
<f:ajax listener="#{listAllBookings.printValues()}" event="keyup" render="myTable"
execute="#this selectSomething"/>
</h:inputText>
<h:selectOneMenu id="selectSomething" value="#{listAllBookings.selectedAttr}">
<f:selectItem itemLabel="GUEST" itemValue="GUEST"/>
<f:selectItem itemLabel="HOTEL" itemValue="HOTEL"/>
</h:selectOneMenu>
But you could also execute the surrounding form via execute="#form"
See also
Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes

try this:
<h:selectOneMenu value="#{listAllBookings.selectedAttr}">
<f:ajax event="change" listener="#{listAllBookings.printValues()}" />
<f:selectItem itemLabel="GUEST" itemValue="GUEST"/>
<f:selectItem itemLabel="HOTEL" itemValue="HOTEL"/>
</h:selectOneMenu>

Related

change the variable JSF from SelectOneMenu

I have setted a JSF Varidable Like this:
<c:set var="selectedech" value="#{ManagedBean.selectedtypeFacturation}" scope="request"/>
After that i putted it on a SelectOneMenu:
<p:selectOneMenu panelStyle="width: 120px" effect="fade" id="typeFacture" value="#{selectedech}" >
<f:selectItem itemValue="Echeancier" itemLabel="Echeancier"/>
<f:selectItem itemValue="Mensuel" itemLabel="Mensuel"/>
<p:ajax event="change" update="#this"/>
</p:selectOneMenu>
I would like to change the value of JSF variable "selectedech" each time i change the selection.
Any idea in order to get it work well
Thx

Calling a function from managed bean and after completion setting focus to a desired component

I have a p:commandButton which calls a method from managed bean. I want to add one more functionality to it. So after calling that particular function , I want to set focus to a p:selectOneMenu
My code is :
<p:selectOneMenu id="particulars" filter="true"
filterMatchMode="startsWith"
value="#{receiptMB.selectedFeeSchemeDetail.planId}"
style="width:120px !important;">
<f:selectItem itemLabel="Select" itemValue="" />
<f:selectItems value="#{receiptMB.incomeSchemes}" var="acc"
itemLabel="#{acc.feeInstallment}" itemValue="#{acc.planId}" />
<p:ajax event="change"
listener="#{receiptMB.onIncomeSourceSelect()}"
update=":messageGrowl :receiptEntryForm:planId
:receiptEntryForm:newVouchDetails txtQty totalAmount
particulars :receiptEntryForm:planId :receiptEntryForm:receiptSource
advAmtRemaining advCheckBox amtPaid" />
</p:selectOneMenu>
<p:commandButton value="#{bundle.add}" icon="ui-icon-plus"
partialSubmit="true" process="jvPanel autoCompleteData"
id="addVoucher" actionListener="#{receiptMB.loadTableData}"
update=":messageGrowl cmdSave :receiptEntryForm:newVouchDetails
txtQty totalAmount
particulars :receiptEntryForm:planId :receiptEntryForm:receiptSource
advAmtRemaining advCheckBox amtPaid">
</p:commandButton>
Please suggest me some solutions.
You can use RequestContext in you action method as :
RequestContext context = RequestContext.getCurrentInstance();
context.scrollTo("receiptEntryForm:particulars");
you can checkout primefaces documentation and showcase for more info.

Primefaces slectOneMenu uses itemLabel as itemValue

I have an <p:selectOneMenu> which has an <f:selectItems> and in this <f:selectItems> I have set an itemLabel and an itemValue. The Item Value is an Enum Object which contains only a String and the Label is these String. The value of the <p:selectOneMenu> is an Collection of these Enum Objects. The selectOneMenu has an valueChangeListener and when its called it becomes the label as "new Value" and not the value. Has anyone an idea how I can fix this?
<h:form>
<p:selectOneMenu id="changeSpracheMenu"
value="#{sessionManager.currentUser.sprache}"
label="#{messageManager.getMessage('USER')}"
valueChangeListener="#{sessionManager.updateSpracheForCurrenUser}"
converter="sprachConverter">
<f:selectItem itemLabel="#{messageManager.getMessage('LANGUAGE')}"
noSelectionOption="true" />
<f:selectItems value="#{sprachManager.allSprachen}" var="sprache"
itemLabel="#{messageManager.getMessage('LANGUAGE_'.concat(sprache.name))}"
itemValue="#{sprache}" />
<p:ajax event="change" update="#form" />
</p:selectOneMenu>
</h:form>
Instead of using the Event of valueChangeListener, you can send the value of your selectItem to your bean.
For that, you have to modify
<p:ajax event="change" update="#form" />
To:
<p:ajax event="change" listener="#{yourBean.yourMethod(sprache)}" update="#form" />
Now in your bean:
private yourObject sprache;
//getter/setter
public void yourMethod(yourObject typeSprache){
whatever xyz = typeSprache.getWhatEverYouWant();
}

set selected object from selectOneMenu to inputText in jsf

I have problem with jsf. My jsf code is:
<h:form>
<p:selectOneMenu style="text-align:left;"
value="#{contractBean.selectedCust}" converter="CustomerConverter">
<f:selectItems value="#{classificatorBean.customerList}"
var="customer" itemLabel="#{customer.name} #{customer.sname}" itemValue="#{customer}" />
<p:ajax event="change" update="custTel" />
</p:selectOneMenu>
<p:inputText id="custTel" value="#{contractBean.selectedCust.name} " />
</h:form>
and I have managed bean (Contractbean) with getter and setter functions of selectedCust Customer object. My problem is when menu changed object dont show customers tel number.
try to use ajax like this:
<p:ajax event="change" process="#this" update="custTel" />

valuechangelistener does not populate values in text box

Here is my jsp:
<h:selectOneMenu value="#{member.dependentName}" onchange="this.form.submit()"
immediate="true" valueChangeListener="#{member.getDependentAddress}">
<f:selectItems value="#{member.dependentList}" />
</h:selectOneMenu>
<h:inputText value="#{member.personName}" immediate="true" />
<h:inputText value="#{member.dob}" immediate="true" />
And this, the function valuechangelistener fires.
public void getDependentAddress(ValueChangeEvent e) {
setPersonName((getDependentsList().get(e.getNewValue().toString())
.getDependentName()));
setDob(getDependentsList().get(e.getNewValue().toString()).getBirth());
System.out.println("New dob value : " + dob);
System.out.println("New name value : " + personName);
FacesContext.getCurrentInstance().renderResponse();
}
The two sysouts give the new value in the console but once the page loads, the fields are blank. I have tried all scopes for the bean. No go. What am i missing?
Thanks
You missed nothing. You've just something too much. To get it to work, you should remove immediate="true" from the to-be-changed components.
<h:selectOneMenu value="#{member.dependentName}" onchange="this.form.submit()"
immediate="true" valueChangeListener="#{member.getDependentAddress}">
<f:selectItems value="#{member.dependentList}" />
</h:selectOneMenu>
<h:inputText value="#{member.personName}" />
<h:inputText value="#{member.dob}" />
The immediate="true" on an UIInput component will cause its validations phase to take place in apply request values phase instead. This gives you the opportunity to use FacesContext#responseComplete() inside a valueChangeListener method skip other components which doesn't have immediate="true" set from being processed. As you have now, with immediate="true", they are also processed.
Please note that this is essentially a hack from the old JSF 1.x ages. If you're already using JSF 2.x, you should be using <f:ajax listener> instead.
<h:selectOneMenu value="#{member.dependentName}">
<f:selectItems value="#{member.dependentList}" />
<f:ajax listener="#{member.getDependentAddress}" render="name dob" />
</h:selectOneMenu>
<h:inputText id="name" value="#{member.personName}" />
<h:inputText id="dob" value="#{member.dob}" />
with
public void getDependentAddress() {
Dependent dependent = getDependentsList().get(dependentName); // Isn't that actually a Map instead of List?
personName = dependent.getDependentName();
dob = dependent.getBirth();
}

Resources