Getting a null value passed to a Java function via BootsFaces datatable

398 Views Asked by At

I am trying to create a modal to send user-selected requests to an email address; however, I am having trouble getting the user-selected requests. I keep getting the null value passed through fooBean.setSelected(id).


Versions:

  • BootsFaces: 1.3.0
  • Java: 1.8.0
  • JSF: 2.0
  • Browser: Internet Explorer 11.2x

MCVE of thisThing.xhtml:

<b:dataTable value="#{fooBean.newRequests}"
    var="foo"
    onselect="ajax:fooBean.setSelected(id)"
    ondeselect="ajax:fooBean.setSelected(id)"
    selectedItems="row"
    selection-mode="multiple"
    selectedRows="#{foo.selected}"
    saveState="false">

    <b:dataTableColumn label="Select">
        <b:selectBooleanCheckbox value="#{foo.selected}" />
    </b:dataTableColumn>

    <b:dataTableColumn label="Status" value="#{foo.status}" />

    <b:dataTableColumn label="Request Number"
        value="#{foo.requestNumber}"
        data-type="string" />

    <b:dataTableColumn label="ID" value="#{foo.id}" />

    <b:dataTableColumn value="#{foo.storeName}"
        label="Store Name" />
</b:dataTable>

MCVE of fooBean.java:

@ManagedBean(name="fooBean")
@ViewScoped
public class fooBean extends BeanBase implements Serializable {
    
    private List<FooRecord> fooRecords = new ArrayList<FooRecord>();
    private List<FooRecord> selectedFooRecords = new ArrayList<FooRecord>();
    
    // ...
    
    public void setSelected(String requestId) {
        // This is not how I really do it, but it gives an idea
        // with what I intend to do.
        this.fooRecords.stream().filter(...).toggleSelection();
        this.selectedFooRecords.stream().filter(...).toggleSelection();
    }
}

Update:

I found out that I had the method called as getSelect instead of getSelected, so I fixed it and that part is done. I just remembered the real issue which is why a null parameter is being passed instead of the requestId. When I debug through the fooBean.getSelected(String requestId), it shows null being passed through as parameter. I have even tried:

<!-- Using varName.property -->
onselect="ajax:fooBean.setSelected(foo.id)"

<!-- Using just the property name -->
onselect="ajax:fooBean.setSelected(id)"

<!-- Using the loop variable -->
onselect="ajax:fooBean.setSelected(foo)"

Update 2:

Fired function with null parameter

How do I pass foo.id to the function?

1

There are 1 best solutions below

10
On

There are only three parameters you can pass to the bean method:

  • The loop variable. In your example, that's foo. This parameter only works if the attribute selected-items is set to rows. Caveat: if you activate column or cell select mode, this parameter is still passed to the server, and it seems to be valid. Only that every attribute of this object is null.
  • typeOfSelection. This is either "row", "column" or "item". It determines what the user has selected. That, in turn, is determined by the attribute selected-items, so you hardly ever have to use this parameter. Just omit it from the parameter list.
  • indexes tells the JSF bean which rows, columns or cells have been selected. Note that in the first two cases this is either an individual number or - if multiple items have been selected - a comma separated list. If you've set selected-items="cell", indexes is a comma-separated list of objects containing the row and column index.

I suppose indexes or foo is the most useful parameter in your case.

Final remark: we'd like to add more flexibility, but this requires much more understanding of the internal API of JSF than even we have. Truth to tell, I'm not even sure we can pass arbitrary bean values without modifying the JSF engine itself. If anybody knows how to do that, please leave a message at our bug tracker.

Update: Maybe the problem is caused by the modal. The modal is rendered at load time, but shown later, when the user has selected a row. Thing is, the modal isn't re-rendered automatically unless you add the content of the modal to the update attribute.