问题
I'm using JSF and Glassfish. I have in a ui:repeat several h:inputtextarea where I have a valuechange Listener. The problem is that the valuechangelistener only fires for the last component. here is my code.
The answerhandler return a list of Answerentities (JPA) which define the content. The answerhandler itself is a ManagedBean which is my controller. I know that the code is a little redundant but that's not the matter here. The valuechangelistener should be fired if a form is submited --> the button "save" is clicked. but it is only fired for the last repeated form.
First xhtml file:
<ui:repeat var="a" value="#{answerHandler.answerByCreated}">
<h:form>
<h:inputTextarea readonly="#{!a.equals(answerHandler.tobeEdited)}"
id="showacontent" value="#{a.content}" class="form-control questiontextarea"
rows="8" valueChangeListener="#{answerHandler.acontentChanged}"
pt:maxlength="2500"/>
<div class="btn-group btn-group-sm" >
<h:commandButton action="#{answerHandler.setEditable(a)}" rendered="#{answerHandler.canEditAnswer(a) and !answerHandler.isEdit}" class="btn btn-primary" value="Edit" />
<h:commandButton action="#{answerHandler.cancel()}" rendered="#{answerHandler.isEdit and a.equals(answerHandler.tobeEdited)}" class="btn btn-primary" value="Cancel" />
<h:commandButton action="#{answerHandler.saveChanges(a)}" rendered="#{answerHandler.isEdit and a.equals(answerHandler.tobeEdited)}" class="btn btn-primary" value="Save" />
</div>
</h:form>
</ui:repeat>
And here the answerhandler class with the called methods
public void acontentChanged(ValueChangeEvent e) {
if (e != null) {
System.out.println("change");
oldcontent = e.getOldValue().toString();
tobeEdited.setContent(e.getNewValue().toString());
saveChanges(tobeEdited);
}
}
public void saveChanges(Answer a) {
if (canEditAnswer(a)) {
a.setEdited(new Date());
aManager.edit(a);
disableEditable();
}
}
Thanks for your help in advance :)
EDIT
I wrapped ui:repeat in a h:form and it works now. Thanks https://stackoverflow.com/users/3773914/tt-dev for your comment, it really helped me out. Could you maybe explain why it works when h:form wraps ui:repeat?
回答1:
i actually cannot remember the exact reason behind this but, when you render multiple forms in a repeater component, just the last rendered one submits requests.
so the solution is to place the h:form
out of ui:repeat
.
ps: using multiple forms on page, which are not generated inside a repeater works fine.
edit: seems to be a bug, which is mentioned here by BalusC: <h:form> within <ui:repeat> not entirely working, only the last <h:form> is processed
来源:https://stackoverflow.com/questions/29678755/valuechange-listener-inside-uirepeat-only-works-for-the-last-repeated-component