selectonemenu does not display value from bean [duplicate] - jsf

This question already has answers here:
How to populate options of h:selectOneMenu from database?
(5 answers)
Closed 7 years ago.
I am having problems with the primefaces selectonemenu, it only displays cube.name (the pulldown has the verbiage cube.name not the value of cube.name), here is the code.
<p:selectOneMenu id="cubeConfigId" value="#{projectModel.selectedProject.cubeConfigId}" >
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{projectModel.cubeConfigEntities}" var="cube" itemLabel="cube.name" itemValue="cube.cubeConfigId"/>
</p:selectOneMenu>
what exactly does the var="cube" do?

You got itemLabel="cube.name" instead of itemLabel="#{cube.name}" (same for itemValue).
What is displayed is determined by itemLabel="cube.name" so you see what you got in label - in this case it's only name (if you do something like this itemLabel="#{cube}" - toString() will be displayed of the Cube entity). What is saved in backing bean is under itemValue.
var="cube" is just iterator for value="#{projectModel.cubeConfigEntities}". If cubeConfigEntities is list of Cube entities then var="cube" is Cube in one loop iteration, in another loop iteration it takes another Cube from list etc. You can access Cube methods by invoking them on cube.

Related

How I can use a converter in a JSF tag selectOneMenu? [duplicate]

This question already has answers here:
How to populate options of h:selectOneMenu from database?
(5 answers)
Closed 4 years ago.
The following example works in my interface:
<h:outputText value="#{diplome.pays}">
<gn:converter origine="#{config.origManSirhen}"
nomenclature="#{config.codePays}"
propriete="libelleImpression" />
</h:outputText>
However, when I try to use the same converter does not work when the element is a selectOneMenu
<p:selectOneMenu id="pays">
<f:selectItem
itemValue="#{InformationsPersonnellesModele.adressePrincipale.pays}"
itemLabel="#{InformationsPersonnellesModele.adressePrincipale.pays}" />
<gn:converter origine="#{config.origManSirhen}"
nomenclature="#{config.codePays}" propriete="libelleImpression" />
</p:selectOneMenu>
I have tried to use the attribute converter into the selectOneMenu tag, but it does not work as I expect because I need to use as well the parameter nomenclature and propiete.
I found a possible solution:
The item selected by default must be the value of the selectOneMenu tag.
And the list of items and the converter are directly assigned to the value of the selectItems tag.
<p:selectOneMenu id="pays"
value="#{InformationsPersonnellesModele.adressePrincipale.pays}">
<f:selectItems
value="#{gnl:listeTri(config.origManSirhen, config.codePays,'libelleImpression')}" />
</p:selectOneMenu>

PrimeFaces dataTable method called many times [duplicate]

This question already has answers here:
Defining and reusing an EL variable in JSF page
(2 answers)
Closed 6 years ago.
I have a datatable with a column:
<p:dataTable value="#{cc.attrs.bean.model}"
...
<p:column style="width:#{bean.getWidth('colDate', 55)}px;"
It seems that the bean.getWidth method is called for every row in the table. Thus, when having 100 rows, the method is called a hundred times. I expected the method to be called only once.
Am I wrong?
No, it's right.
You can cache the value into your bean or use JSTL
<c:set var="width" value="#{bean.getWidth('colDate', 55)}" />
<p:dataTable value="#{cc.attrs.bean.model}"
...
<p:column style="width:#{width}px;"
You can find more infos here https://stackoverflow.com/tags/jstl/info.
Don't forget the namespace .
you can save result of getWidth in managed bean attribute then use this in column style

JSF h:selectOneMenu wont call relevant setter method

Hi I've got a question
<h:selectOneMenu id="cmbFileStatus1" disabled="#{!schedulerController.oldFile}"
value="#{schedulerController.test}">
<f:selectItem itemValue="#{null}" itemLabel="--Show All--" noSelectionOption="true"/>
<f:selectItems value="#{schedulerController.statusList}"/>
<f:ajax execute="#this" render="dataTable"/>
</h:selectOneMenu>
The above code the ajax executes(I checked through firebug). But the thing is the selected value wont be set to h:selectOneMenu value parameter.
There is a h:form tag that is wrapping this element, plus there are two other elements similarly using ajax as shown here. But they are positioned before this element in the DOM, they call the relevant setter methods and the updates the bean variables.
But this element it doesn't set necessary selected value.
Also another detail, the list that is populated for selection, it is a list created from enum values.
One instance I moved the problematic code to the top of the DOM (before the other two elements that ajax is applied) and then it hit the setter method when ran in debug mode.
I cannot understand whats wrong, it doesn't show any javascript errors and such. The JSF version is 2.0, this is an old project.
Any ideas guys?

Conversion Error setting value '1' for 'null Converter' [duplicate]

This question already has answers here:
How to populate options of h:selectOneMenu from database?
(5 answers)
Closed 7 years ago.
When I click submit button in a jsf page I get the above error.
html:
<h:selectOneMenu id="ddlCountryCode" value="#{jsfFills.countries}">
<f:selectItems value="#{jsfFills.countries}" var="c"
itemLabel="#{c.CName}" itemValue="#{c.CCode}" />
</h:selectOneMenu>
The "value" attribute should point to a variable where you want to store the selected value of the component, not the list of choices.
In the example above, jsfFills.countries is used as both the list of options AND the value of the component, and that could be causing the issue. We'd want to create a variable in a bean somewhere and use that instead.
<h:selectOneMenu id="ddlCountryCode" value="#{jsfFills.selectedCountry}">
<f:selectItems value="#{jsfFills.countries}" var="c"
itemLabel="#{c.CName}" itemValue="#{c.CCode}" />
</h:selectOneMenu>

How to pre-select data in h:selectManyListbox [duplicate]

This question already has an answer here:
How do UISelectOne and UISelectMany components preselect defaults in f:selectItems
(1 answer)
Closed 7 years ago.
This is a common problem and I already saw the basic solution (populate the selectManyListBox). Here is the code of my JSF 1.2 page:
<h:selectManyListbox id="statusMenu" converter="statusConverter" value="#{aprvAction.ap.statuses}" >
<f:selectItems id="statusItem" value="#{action.ap.statusItens}"/>
<a:support event="onclick" ajaxSingle="true" immediate="true" eventsQueue="queueGeral" />
<a:support event="onchange" ajaxSingle="true" eventsQueue="queueGeral" process="statusMenu"/>
</h:selectManyListbox>
The thing is that #{aprvAction.ap.statuses} is an instance of List<Status> class. However, in tag <f:selectItems> the value: #{action.ap.statusItens} is an Instance of List<SelectItem>.
I populate the the #{aprvAction.ap.statuses} with the values that I want to pre-select the ListBox, but did not work. I think it's because they are different objects in <selectManyListBox> and <selectItems>.
How can I solve this, and show the pre-selected values in <selectManyListBox>?
JSF will use equals() method to compare available items with (pre)selected items.
In case of standard objects, such as String, this is already implemented.
In case of custom objects, such as your Status entity, it's your responsibility to make sure that you've properly implemented the equals() (and hashCode()) method in the class.
See also:
How to populate options of h:selectOneMenu from database?
Right way to implement equals contract
value in h:selectManyListbox should be subset of itemValue's in f:selectItems.
Create a method which return the List or Array of selected statuses. Use this method in value attribute of h:selectManyListbox. Any value from list/array returned in this method should match itemValue in set of {itemValue, itemLabel} pairs used in f:selectItems.

Resources