I have a JSF page in which I am iterating a list within <h:dataTable>
to display some rows containing checkbox, some text and a textbox.
I have to validate the <h:dataTable>
so that when a user checks the checkbox, he has to enter some text inside the textbox.
This is my JSF page.
<h:form prependId="false" id="form">
<h:dataTable id="rm" width="100%" cellspacing="4"
value="#{controller.alertTriggers}" var="alt"
columnClasses="c1,c2,c3,c4">
<h:column>
<h:selectBooleanCheckbox value="#{alt.checkValue}" id="checkbox"/>
</h:column>
<h:column>
<h:outputText value="#{alt.id}" />
</h:column>
<h:column>
<h:outputFormat value="#{alt.msg1}" />
</h:column>
<h:column>
<h:message for="emailID" id="email" styleClass="validation-error"/>
<h:inputText value="#{alt.mailId}" id="emailID" style="width: 87%;" />
</h:column>
</h:dataTable>
</h:form>
I have given the id of all the checkboxes as checkbox
and id of all textboxes as emailID
. When the page is rendered, on checking the page source, I found that the ids of the checkboxes are 'rm:0:checkbox','rm:1:checkbox'... and those of the textboxes are 'rm:0:emailID','rm:1:emailID'..
In the controller, I want to access these dynamic text boxes and check boxes for which I use the following code:
FacesContext context = FacesContext. getCurrentInstance();
for (int i=0;i<9;i++){
UIInput u=(UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent( "form:rm:" +i+":checkbox" );
if ((Boolean) u.getValue()){
UIInput ui=(UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent( "form:rm:" +i+":emailID" );
//code
}
}
But this is giving java.lang.NullPointerException
Even using the code:
UIInput u=(UIInput) FacesContext.getCurrentInstance().getViewRoot().
findComponent( "form:rm:0:checkbox" ); gives the same exception.
But if I use
UIInput u=(UIInput) FacesContext.getCurrentInstance().getViewRoot().
findComponent( "form:rm:checkbox" );
it doesn't give a Null Pointer Exception but I don't know whether which checkbox's value is it giving.
So, in summary,
JSF generates the ids as rm:1:checkbox,rm:2:checkbox etc., but when I try to access this UI component in JSF page, I am not able to do it.
Am I missing something ?