1

I am developing commenting structure similar to facebook. Inside <ui:repeat> I have placed a form and I expect that when I click on a command link within that form, it should submit the form which is the command link containing. But what I get is that the action of every link is invoked on click of a single link.

The backing bean is @ViewScoped. I am using Mojarra 2.1.7.

How is this caused and how can I solve it? Is this problem related to the <h:form> tag?

<ui:repeat var="parentComment" value="#{commentTree.parentComments}">
    <h:form>
        <ul>
            <li>
                #{parentComment.comment}<br />
                <span class="small darkblue">#{parentComment.userNodeImpl.firstName} #{parentComment.userNodeImpl.lastName}</span>
                <span class="small darkblue">#{parentComment.commentTime}</span>

                <ui:repeat var="childComment" value="#{commentTree.getChildComments(parentComment)}">
                    <ul>
                        <li>
                            #{childComment.comment}<br />
                            <span class="small darkblue">#{childComment.userNodeImpl.firstName} #{childComment.userNodeImpl.lastName}</span>
                            <span class="small darkblue">#{childComment.commentTime}</span>
                        </li>
                    </ul>
                </ui:repeat> 

                <div class="small darkblue">
                    <h:commandLink value="comment" action="#{commentTree.saveChildComment(parentComment)}" />
                </div>
            </li>
        </ul>
    </h:form>
</ui:repeat>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'm not sure if I understand the concrete problem. What exactly do you mean with "fire data in different order to my backing bean"? Do you mean that it's invoked for every single row, or that it's invoked just once (as expected) but passes the wrong `parentComment` (unexpected)? – BalusC May 19 '12 at 15:27
  • yes it is invoked for every single row. and my backing #{commentTree.saveChildComment(parentComment)} method calls several times and it displays all the data of every single row. – Emil Thushanga May 19 '12 at 17:13
  • Are you doing business logic in the getter method? The way how you get the value of the inner loop is also strange and indicates that you're calling the DB in the getter; I'd expect to see a `#{parentComment.childComments}` here. Getter methods should not contain business logic but just return the already loaded/prepared property. – BalusC May 19 '12 at 17:18
  • public List getChildComments(IComment parentComment){ return commentDao.commentList(FacesUtils.getCurrentTopic(), parentComment); } – Emil Thushanga May 19 '12 at 17:35
  • You shouldn't do that... A getter is invoked as many times as EL needs to access the property. The getter should solely return the already-prepared value. See also http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times/2090062#2090062 I'm not sure if this is related to the problem you're seeing as the cause of the problem is likely in `#{commentTree.parentComments}` or maybe in nesting forms. Removing the inner loop doesn't solve the problem, right? – BalusC May 19 '12 at 17:36
  • Yes i call the dao service in the getter. – Emil Thushanga May 19 '12 at 17:37
  • i have a Parent comment and list of child comments. By using to above code fragment i could generate the expected display. As for example , if i have 10 parent comments It generates display for parent comment and the child commetns with command link to add a new comment. So it means i get 10 forms with the ui repeat.The problem is if i click on one command link it submits the all the 10 forms data into my baking bean.Thank you so much helping me again. if you could come up with any idea that will help me so much.i would appriciate if any similar example code if you know.thanks a lot. – Emil Thushanga May 19 '12 at 17:55
  • I could fix this issue, what i could understand is forms should properly used to wrap components as possible as minimum scope. Thanks for your support. as well as i re wrote the backing bean as you suggested. now i ended up with ajax problem. if you could please check that also. thaks a lot. [link]http://stackoverflow.com/questions/10682302/jsf2-ajax-render-id-not-found[link] – Emil Thushanga May 22 '12 at 05:10

0 Answers0