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

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.

Related

get the list of element in <ui:repeat> already checked in JSF [duplicate]

This question already has answers here:
How to use <h:selectBooleanCheckbox> in <h:dataTable> or <ui:repeat> to select multiple items?
(3 answers)
Closed 5 years ago.
I have an <ui:repeat> in which I has a list of object , next to each of those object I need to have a checkbox ( to check this element).
I want to know how could I integrate those checkboxes inside the <ui:repeat> in order to have many rows and each rows needs to has the object.getName() and next to it I need to have the checkbox ?
If this is feasible, Please how could I obtain those checked objects in the backing bean ?
<ui:repeat var="myObject" varStatus="status"
value="#{Bean.getListObjects}">
<b>#{status.index+1} .</b>
<h:outputLabel value="#{myObject.name}" />
//need the checkbox here for example
</ui:repeat>
Just create a map with the object UID as the key and a boolean as the value. Then value-bind each checkbox to a map entry.
Assuming your names are unique, you could use them. So, in you bean create a Map<String,Boolean>, and initialize the map with each object.
You can use the map to in the checkbox value like value="#{bean.map[object.name]}".
See also:
Dynamic value binding of JSF component

JSF Datatables : How to handle columns with subtype specific content [duplicate]

This question already has answers here:
Conditionally displaying JSF components
(3 answers)
JSTL c:if doesn't work inside a JSF h:dataTable
(1 answer)
Closed 5 years ago.
I have a scenario where I have a list of objects that extend a common supertype.
This list of objects is supplied to the h:datatable as it's value.
The last column in the table has inputs that are subtype-specific whereas all the other columns apply to fields found in the common superclass.
What is the most elegant way of rendering the subtype-specific fields ?
At the moment I am doing this:
<h:column>
<c:if test="#{cover.cd eq 2}">
//Markup specific to this subtype
</c:if>
<c:if test="#{cover.cd eq 3}">
//Markup specific to this subtype
</c:if>
</h:column>

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

How to implement foreach in jsf?

How do I create ofer_has_location objects (join object from location and ofer) using the current ofer and the selected items from the h:selectManyCheckBox
<h:selectOneMenu id="companyidCompany"
value="#{oferController.selected.companyidCompany}"
title="#{bundle.CreateOferTitle_companyidCompany}"
required="true"
requiredMessage="#{bundle.CreateOferRequiredMessage_companyidCompany}">
<f:ajax event="valueChange" execute="companyidCompany"
render="locationCollection" />
<f:selectItems value="#{companyController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="#{bundle.CreateOferLabel_locationCollection}"
for="locationCollection" />
<h:selectManyListbox id="locationCollection" value="locations"
title="#{bundle.CreateOferTitle_locationCollection}">
<c:forEach items="locations">
<f:selectItems var="locations"
value="#{oferController.selected.companyidCompany.locationCollection}" />
</c:forEach>
</h:selectManyListbox>
What you need to do in order to achieve 'connected elements' functionality:
Have two elements ( <h:selectOneMenu> and <h:selectManyLisBox> in your case), where the second one will be dependent on the selected option(s) of the first one. The second element must have an id in order to be rerendered afterwards.
Every HTML select element (that's rendered by both JSF tags of your choice) will have a set of options that are not supposed to be created via iterative element like <c:forEach> (though, it is in fact possible), but rather via the <f:selectItem>/<f:selectItems> tags (thus, remove your iterative tag in comment).
When values in the components are bound not as plain Strings, or primitive wrappers (Integer, etc.), but rather as model objects (YourClass objects, etc.), then you need to tell JSF two things: how can it print option's value from your class and how can it reconstruct an object from request parameter that is a string. For this you need to implement Converter, that is, explain JSF how to do the abovementioned transformations. Use this answer and BalusC's blog as reference points. Note the appropriate syntax for <f:selectItems itemValue="..."> here.
Model values bound by these two components also need to represent your classes, just in a same way as selected items' values. For <h:selectOneMenu> it is value="#{}"; for <h:selectManyListbox> it is value="#{}" with YourClass selectOneMenuValue and List<YourClass> selectManyListboxValues or YourClass[] selectManyListboxValues bean properties respectively.
Population of second select will be handled via <f:ajax> tag. As contents need to be calculated 'on the fly', the right spot to make it is within its listener attribute (i.e. to have List<YourClass> contentsOfSecondListbox = createListboxValues(YourClass oneMenuSelectedOption);) . As you'd desire to rerender the second element, specify its client id in render attribute of <f:ajax>. Example here.
In case you are binding, for example, to String/String[] values, you won't need the converter parts.
Try to go through it step by step to find out your errors and correct them.

Avoiding null point exception in el in JSF [duplicate]

This question already has an answer here:
JSF: h:outputText; how to show a dash when the value is empty string?
(1 answer)
Closed 5 years ago.
I am developing a JSF application with JPA(EclipseLink 2.0) and Primefaces.
I want to know is there any way to avoid null point exception when el calls a property of a null object. I have described the situation.
I have Bill class. There may be no or more BillItem objects with a Bill objects. Each BillItem object have Objects like Make, Country, Manufacturer, etc objects. I am displaying several properties of a bill within a single JSF file like this.
"#{billControlled.bill.billItem.modal.name}"
But if a bill is not selected, or when there are no bill items for a selected bill, the properties accessing in the el are null. I can avoid this by creating new objects for every bill, for example, new make for a new bill item, etc or by creating new properties in the controller itself for all the properties. But that is a very long way and feel like rudimentory.
Is there any good practice to avoid this null point exception in el in JSF?
Solution is checking for null (and you can also check for empty) and use with render attribute.
Empty can work well with Collections (check for both empty and null), and also with null.
For example:
<h:outputText rendered="#{not empty myBean.myData}" value="..." />
I got the solution. Before every el, I can check null.
JSF: h:outputText; how to show a dash when the value is empty string?
<h:outputText value="#{userHandler.user.phoneNumber != null
? userHandler.user.phoneNumber : '-'}" />
Or
<h:outputText rendered="#{userHandler.user.phoneNumber ne null}" value="#{userHandler.user.phoneNumber}" />

Resources