0

In a JSF 2.2 application, a user visit the link test.xhtml?id=324 , The page must have the paramater id. The page is backed by a CDI @ViewScoped Bean called TestBean.

The fields get initiliazed using a @postconstruct method

@PostConstruct
public void initialize() {…}

and this method that, load data based on id, if id is not present or correct, then direct the user to another page

<f:metadata>
    <f:viewAction action="#{testBean.redirectNoParameters}"></f:viewAction>
</f:metadata>

public String redirectNoParameters() {
//method load data based on id, if id is not present or correct, then direct the user to another page   
        test = testDao.find(id);

In this page there is a comment section

<h:form role="form" prependId="false" >
    <h:selectOneMenu value="#{testBean.sortComment}" valueChangeListener="#{testBean.orderComments}">
        <f:ajax event="change" execute="@this" render="comment-listing" />
        <f:selectItem itemLabel="Recent" itemValue="1"></f:selectItem>
        <f:selectItem itemLabel="Oldest" itemValue="2"></f:selectItem>
        <f:selectItem itemLabel="Popular" itemValue="3"></f:selectItem>
    </h:selectOneMenu>
</h:form>

<div jsf:id="comment-listing">

    <div >
    <h:form role="form" prependId="false">
        <ui:repeat var="comment" value="#{testBean.comments}" >
            <div >
                <div >
                    <div>
    <h:commandButton  value="#{comment.votes.size()}" action="#{testBean.upVote(comment)}" >
        <f:ajax execute="@this" render="comment-listing" immediate="true"/>
    </h:commandButton>
                    </div>

                </div>
                <div >
                    <h4 >
                        <h:link outcome="channel">
                            <f:param name="handle" value="#{comment.commentor.handle}"></f:param>
                            #{comment.commentor.handle}
                        </h:link>
                        on <span class="c-date">#{comment.creationDate}</span>
                    </h4>
                        #{comment.comment}
                </div>
            </div>
        </ui:repeat>
        </h:form>

    </div>
</div>

As You can see for each comment there is a <h:commandButton> for a user to upvote the comment (like in youtube, stackoverflow…) . When the method is clicked it works as inteded. The method testBean.upVote(comment), is called and the ui is updated.

THEN I use the first form above to Re-order comments, say for example the change is from popular -> Recent. Also the method does its job and the ui is updated.

Till now all is working great.

However, When I click the upvote btn/method (of a different comment) AGAIN. The contsructor get called, so is @postconstruct, and the redirectNoParameters().

I have tried removing all ajax tag to see if it is an ajax problem. And The problem still occurs. So it’s not an ajax issue.

I have tried remove the parameter comment from the method "#{testBean.upVote(comment)}" to see if that’s what producing the problem. And The problem still occurs.

I have tried using <c:forEach instead of <ui:repeat . Still no hope.

What’s produce the problem? Why is this happening instead of just calling the method of the command button ?? Thanks.

usertest
  • 2,140
  • 4
  • 32
  • 51
  • Off topic: http://stackoverflow.com/questions/7415230/uiform-with-prependid-false-breaks-fajax-render – Kukeltje Nov 10 '17 at 10:39
  • Hmm, try using some `@PreDestroy` on the ViewScoped bean to see when exactly (if) it gets destroyed. Also I stumbled upon one SO question with good desc on this topic (but you might already know all that) https://stackoverflow.com/questions/6025998/difference-between-view-and-request-scope-in-managed-beans – Siliarus Nov 10 '17 at 14:19
  • what happens is that, a new bean is created, then call constructor, @postconstruct then redirectNoParameters(), this method redirect the user to another page because the there is no paramater id in the request test.xhtml . the newly created bean is detroyed, BUT not the the first one. I checked by using browser back btn, and the the page still works. so the bean does not get destroyed. Though that does not explain why a new bean is created instead of using the existing one. – usertest Nov 12 '17 at 10:21

0 Answers0