0
votes

I'm using a <rich:datatable> to show the content of a List<Map<String, String>

In the code below, spotlightPtBindings is the List<Map<String, String> and spotlightbinding represents each Map<String, String>. In the first column, I'm showing one selectBooleanCheckBox for eah row. When a selectBooleanCheckBox is checked, I'd like to send the value of the Map<String, String> corresponding to the key "URI" as a parameter to the method: inserirBean.onSelectedCheckBox(uri), and that's why I put this value in a ui:param of name: uri. The problem here is that when I try to print the value uri received in inserirBean.onSelectedCheckBox(uri), I don't get the any output, as if it is empty. Below there's the rest of the code:

InsereDocumento.xhtml

<rich:dataTable value="#{inserirBean.spotlightPtBindings}" var="spotlightbinding">

        <rich:column>
                 <f:facet name="header">*</f:facet>
                 <ui:param name="uri" value="#{spotlightbinding['URI']}"/>

                 <h:selectBooleanCheckbox value="#{selectionBean.selected}" />

                    <c:if test="#{selectionBean.selected}">
                        #{inserirBean.onSelectedCheckBox(uri)}"
                    </c:if>

             </rich:column> 

        <c:forEach items="#{inserirBean.variableNamesPt}" var="vname">
             <rich:column>
                 <f:facet name="header">#{vname}</f:facet>
                  #{spotlightbinding[vname]}
             </rich:column> 
         </c:forEach>  


     </rich:dataTable> <br />

SelectionBean

package managedBeans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class CheckBoxSelectionBean implements Serializable {
    private transient boolean selected = false;
    private static final long serialVersionUID = 1L;

    public CheckBoxSelectionBean() {
        // TODO Auto-generated constructor stub
    }

     public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
        }

}

InserirBean - I'm not showing here how the List<Map<String, String>> named spotlightPtBinding and how the List<String> variableNamesPt were populated, because it was a complex process, but I can tell you they are certainly populated, cause I can see their content on the screen.

@ManagedBean
public class InsereDocumentoBean implements Serializable {

    private static final long serialVersionUID = 1L;

        private List<String> variableNamesPt = new ArrayList<String>();
        private List<Map<String, String>> spotlightPtBindings = new ArrayList<Map<String, String>>();


         public List<String> getVariableNamesPt() {
        return variableNamesPt;
    }

    public List<Map<String, String>> getSpotlightPtBindings() {
        return this.spotlightPtBindings;
    }

          public void onSelectedCheckBox(String uri) {
        System.out.println("URI: " + uri);
    }
}

What may the problem be? Thank you! I'm new to JSF and need your help!

1

1 Answers

1
votes

In JSF rendering is a two-step process: there's view build time and view render time. Although they're in the same file, some tags take effect at view build time, some at render time.

JSTL tags like <c:forEach>, <c:if> and all tag handlers (including <ui:param>, see here) are evaluated at view build time, they add content to the "final" xml tree that is then rendered by JSF.

JSF HTML tags and derivates like <rich:dataTable> are evaluated at view render time, so the datatable's var is evaluated later then the <ui:param> which causes spotlightbinding not to be bound when it's assigned to uri.

Instead, I suggest you assign an ajax listener to call the function:

<h:selectBooleanCheckbox value="#{selectionBean.selected}">
    <f:ajax listener="#{inserirBean.onSelectedCheckBox(spotlightbinding['URI'])}" />
</h:selectBooleanCheckbox>

Note that the listener is called whenever the value changes.