Mr.J4mes
Mr.J4mes

Reputation: 9266

Problems regarding PrimeFaces partial process

In my application, I have this managed bean:

@ManagedBean(name = "mrBean")
@RequestScoped
public class MrBean {


    @ManagedProperty(value = "#{param.id}")
    private Long   commentableID;
    private String comment;

    @PostConstruct
    public void init() {
        System.out.println("INIT " + commentableID);
    }

    public void postComment() {
        System.out.println("POST COMMENT " + commentableID);
    }

    public void like(boolean like) {
        System.out.println("LIKE " + commentableID);
    }

    // Getters and Setters

}

PROBLEM 1:

On the page for viewing articles, I have the following box for commenting.

<h:panelGrid columns="1">
    <p:inputTextarea id="comment" value="#{mrBean.comment}" />

    <p:commandButton actionListener="#{mrBean.postComment}" value="Post">
        <f:param name="id"  value="#{viewCommentable.commentableID}" />
    </p:commandButton>
</h:panelGrid>

Everything works fine with the above code. However, since the postComment() function only requires the comment property, I tried to put in process='comment' into the above p:commandButton. At this point, whenever I click the Post button, I always see INIT [commentableID] on the console. However, I never see POST COMMENT [commentableID]. In other words, the listener method postComment() was never called even though the bean was created correctly.

PROBLEM 2:

On the same page, I have the following toggle buttons for liking/disliking an article.

<h:inputHidden id="commentableID" value="#{mrBean.commentableID}" />

<p:selectBooleanButton id="like" value="#{viewCommentable.commentable.liked}" onLabel="Liked" offLabel="Like" >
    <p:ajax process="like dislike commentableID" listener="#{mrBean.like(viewCommentable.commentable.liked)}" />
</p:selectBooleanButton>

<p:selectBooleanButton id="dislike" value="#{viewCommentable.commentable.disliked}" onLabel="Liked" offLabel="Like" >
    <p:ajax process="like dislike commentableID" listener="#{mrBean.dislike(viewCommentable.commentable.disliked)}" />
</p:selectBooleanButton>

These buttons are working fine. However, what I observed is quite weird. When I click the Like button, on the console I saw these lines:

INIT null
LIKE [commentableID]

Somehow, the property commentableID was not available in the init() function but it was later on in the like() function.

I'd be very grateful if you could give me an explanation for the above 2 problems.

Best regards,

Upvotes: 2

Views: 3072

Answers (2)

Mr.J4mes
Mr.J4mes

Reputation: 9266

I've finally realised that I didn't use the process attribute correctly. In order to process partially, in a <p:commandButton>, I need the ids of the components that I want to process as well as a @this to process the button itself. Besides, another problem is that I didn't use the correct syntax for process attribute. The ids should be separated by a comma not a space. The following snippet should work:

<p:commandButton process="@this, comment" actionListener="#{mrBean.postComment}" value="Post">
    <f:param name="id" value="#{viewCommentable.commentableID}" />
</p:commandButton>

<p:selectBooleanButton id="like" value="#{viewCommentable.commentable.liked}" onLabel="Liked" offLabel="Like" >
    <p:ajax process="like, dislike, commentableID" listener="#{mrBean.like(viewCommentable.commentable.liked)}" />
</p:selectBooleanButton>

Upvotes: 1

Sai Ye Yan Naing Aye
Sai Ye Yan Naing Aye

Reputation: 6738

Add process and update attributes in your p:commandButton likes this,

     <h:panelGrid columns="1">
        <p:inputTextarea id="comment" value="#{mrBean.comment}" />

        <p:commandButton actionListener="#{mrBean.postComment}" value="Post" process="@this" update="grid">
          <f:param name="id"  value="#{viewCommentable.commentableID}" />
        </p:commandButton>
    </h:panelGrid>

But process attribute has many keywords such as @this, @form, @all ,etc. Show details in here

Upvotes: 0

Related Questions