How to bind multiple UI components to backing bean
jsface Sep 14, 2012 6:52 PMIs it possible to use array or list (like Struts) in backing bean to map to the multiple UI components (dynamically generated by loop such as <c:forEach>, <a4j:repeat>, etc.) in JSF page? If so, how? Can anybody help?
I can bind single UI component to backing bean UI property successfully. But I have trouble to bind multiple UI components to backing bean. The problem is each UI component has to be bound to a single unique UI property in backing bean.
Following is the example I am currently having problem with. It's simplified just for easier review. Basically, I try to attach an error message to the hidden input field whenever the save commandLink is clicked but the textarea for the note is still empty. The error message is just telling user an empty note cannot be saved.
It looks like one UI component property in backing bean can only be mapped to one UI component in JSF page. I am thinking of using array or list but then the index has to be passed in with getter/setter which I don't know how.
JSF page
<c:forEach items="..." var="..." varStatus="status">
<rich:panel>
<h:panelGrid>
<h:panelGroup><h:panelGrid>
...
<h:column>
<a4j:commandLink id="lnkSave" value="#{m_.save}" action="#{logNotesBean.saveLogNote(note)}" />
</h:column>
</h:panelGrid></h:panelGroup>
<rich:message id="msgLogNoteValidationError" for="logNoteValidationError_#{status.index}" ajaxRendered="false"/>
<h:inputHidden id="logNoteValidationError_#{status.index}" binding="#{logNotesBean.logNoteError}"/>
<h:inputTextarea id="txaLogNote" rows="5" value="#{note.noteText}"/>
</h:panelGrid>
</rich:panel>
</c:forEach>
Java bean
@ManagedBean
public class LogNotesBean {
private HtmlInputHidden logNoteError;
public HtmlInputHidden getLogNoteError() {
return this.logNoteError;
}
public void setLogNoteError(HtmlInputHidden logNoteError) {
this.logNoteError = logNoteError;
}
public saveLogNote(note) {
if (note.getNoteText() == null) {
...
FacesMessage facesMessage = ..."Empty log note will not be saved."...;
FacesContext.getCurrentInstance().addMessage(this.getLogNoteError().getClientId(), facesMessage);
}
}
}