1

I'm trying to get a firm grasp on ajax but I'm afraid I'm at a lost. The code below renders the Wait button after clicking on the Join button. Join button's actionListener also gets called the first time. Then, after clicking on Wait, the Join button gets rendered but Wait button's actionListener is not called. Finally, when you click on Join nothing happens. This is a slight variation (I'm hoping) of this question (in case any of you saw something similar)

commandButton inactive after ajax rendering

If this is a duplicate question I'm sorry. If BalusC answered this already from the question I posted, I'm frankly not understanding the cause. I'm at the point where nothing makes sense anymore. I'm sure it's silly but can someone please explain what is happening ?

<h:form>
    <p:commandButton id="Join" actionListener = "#{connexion.joinWaitingList}"
                     update="@form" rendered="#{connexion.renderJoinButton}"
                     value="Join"/>
    <p:commandButton id="leave" actionListener = "#{connexion.leaveWaitingList}" 
                     update="@form" rendered="#{connexion.renderWaitButton}" 
                     value="Wait"/>
</h:form>

The java code

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class Connexion implements java.io.Serializable {
    private boolean renderJoinButton; 
    private boolean renderWaitButton; 

    public Connexion() {
        renderJoinButton = true; 
        renderWaitButton = false; 
    }

    public boolean isRenderJoinButton() {
        return renderJoinButton;
    }

    public void setRenderJoinButton(boolean renderJoinButton) {
        this.renderJoinButton = renderJoinButton;
    }

    public boolean isRenderWaitButton() {
        return renderWaitButton;
    }

    public void setRenderWaitButton(boolean renderWaitButton) {
        this.renderWaitButton = renderWaitButton;
    }

    public void joinWaitingList() {
        System.out.println("Waiting list joined"); 
        renderJoinButton = false; 
        renderWaitButton = true; 
    }

    public void leaveWaitingList() {
        System.out.println("Waiting list abandoned");
        renderJoinButton = true; 
        renderWaitButton = false; 
    }
}

UPDATE

I am using JSF 2.2 with PrimeFaces 3.5

UPDATE #2

This seems to work with JSF 2.1 and 2.0.10. It doesn't work with JSF 2.2. I have posted this issue on Primefaces forum. I'll update this post once I get a reply from them.

UPDATE #3

Thanks to jhond, I was made aware of a bug. Also, one of the primefaces community members got back to me and said "only latest snapshot is kind of compatible".

Community
  • 1
  • 1
Andy
  • 5,900
  • 2
  • 20
  • 29
  • Looks like you adapted the code based on that question. Note that BalusC uses a `` in the answer since *you basically need to wrap conditionally rendered JSF components in a component whose HTML representation is always rendered and then reference it instead in the ajax update* (quoting from BalusC's answer). – Luiggi Mendoza Jul 08 '13 at 22:37
  • @LuiggiMendoza From the OP code his form also had a condition for rendering attached to it (I think). – Andy Jul 08 '13 at 22:44
  • Check BalusC's answer code, not just OP's. – Luiggi Mendoza Jul 08 '13 at 23:01
  • @LuiggiMendoza Ok, let me take a closer look. – Andy Jul 08 '13 at 23:11
  • I tested and is working fine with jsf 2.0.10 and primefaces 3.5 (IE 8.0.6). Try working with your code in a new project and test your xhtml file. Do you have nested forms? (using a template maybe) – danRod Jul 08 '13 at 23:16
  • @LuiggiMendoza I feel silly not understanding this, I'm sorry :( I really don't get it. BalusC's code is updating a panel right ? Mines is updating the form and not a particular button. So am I not following what he sais by referencing the form in the update ? The form in my case is already in the DOM therefore I'm just updating it. Just like he is doing with the panel. – Andy Jul 08 '13 at 23:20
  • @danRod Really ? And you were getting an output for each button click and they were rendering fine every time ? I was using JSF 2.2 I guess I should have mentioned that, didn't think it was needed though sorry. – Andy Jul 08 '13 at 23:22
  • @danRod No nested forms, no templates. That's the entire code minus the library declarations in the xhtml (also using ``) – Andy Jul 08 '13 at 23:24
  • @danRod I think you are right. I am able to get it working with JSF 2.1. I need to be absolutely sure. Give me a couple of hours and I'll give you an update. If you turn out to be correct, I will let you know. You can then feel free to post it as an actual answer and I will gladly accept it. – Andy Jul 08 '13 at 23:49
  • Do your tests first, and let me know. – danRod Jul 09 '13 at 01:55
  • Since you say you're using JSF 2.2, please note that you should use CDI annotations instead of old JSF ones. This means, use `@Named @ViewScoped` and verify that @ViewScoped` comes from `javax.faces.view`package. More info: [CDI compatible @ViewScoped](http://jdevelopment.nl/jsf-22/#1087) – Luiggi Mendoza Jul 09 '13 at 01:58
  • @LuiggiMendoza It comes from it, I'm sure. I also tried using CDI only (`no javax.faces.bean.*`) and still getting the same problem. I don't want to jump the gun and say it's a bug but it's looking like it *could* be...Not sure if this should be filed with the primefaces people or JSF. – Andy Jul 09 '13 at 03:20
  • @danRod Tested with JSF 2.2 `@Named` and `@ManagedBean` still issues. JSF 2.1 no issues. However, I can't say for certain it's a bug. Looking at the forums I'm seeing primefaces 4.0 is what is intended to be used with JSF 2.2 and also I'm not seeing any bug reports for my problem. I highly doubt I'm the first person this happens to, so to be on the safe side just say it works with an earlier version I guess. Thanks a lot for taking the extra time to test this ! – Andy Jul 09 '13 at 03:37
  • @Andy changing versions **is definitely not a valid answer**. – Luiggi Mendoza Jul 09 '13 at 03:52
  • @LuiggiMendoza But he went so far as to test it. It could be a bug. I posted it on the primefaces forum. I don't want him to get in trouble/downvoted because of me though. What should I do ? I wrestled with this all day and he was the only one who found out the problem. – Andy Jul 09 '13 at 04:10
  • @Andy if its a bug, then it should be reported. By just changing the version without any proper explanation, that's not an answer, just *try this because the other doesn't work at the moment not for you nor me* =\ – Luiggi Mendoza Jul 09 '13 at 04:12
  • @danRod I don't want you to get more downvotes because of me. I guess it's not considered an answer because you didn't find the root cause. Sorry man. I guess you should delete it. – Andy Jul 09 '13 at 04:15
  • @LuiggiMendoza But I swear I've seen post where people state that it's a possible bug and it was reported. You're right though. I will update my post and if he wants to delete his answer then so be it I guess. Apologies for being careless. – Andy Jul 09 '13 at 04:18
  • @Andy one more thing, remove the mark of accepted answer from the current answer since it doesn't solve the problem. – Luiggi Mendoza Jul 09 '13 at 04:38
  • @LuiggiMendoza I'm thinking about it. I'm not trying to be difficult but his answer did solve my problem...One sec. – Andy Jul 09 '13 at 04:55
  • @danRod Darn it! He does bring a valid point *try this because the other doesn't work at the moment not for you nor me =\*. Since we're not sure it's a bug I can't say with 100% certainty that it's valid. I feel bad for this sorry again. – Andy Jul 09 '13 at 04:59
  • Don't worry I deleted already. Good to know you solve your issue. – danRod Jul 09 '13 at 13:32
  • You can also check if this fits your issue: [bug1](https://java.net/jira/browse/JAVASERVERFACES-2912). Also I am sure that I've seen a bug with JSF 2.2 that had a proposed workaround like `render="@form a_panel_that_is_not_being_rendered_id"` but I cannot find it, if you try it let us know if it works. This one also fits [bug2](https://java.net/jira/browse/JAVASERVERFACES-2755) – Ioannis Deligiannis Jul 09 '13 at 17:09
  • @johnd Thanks a lot. I know what you're talking about. I didn't make much of it last night. I'm going to try this workaround. – Andy Jul 09 '13 at 17:42
  • @johnd I check out bug1 and the constructor does get called multiple times. bug2 I'm not sure what to make of it since I'm using Chrome. Also the update stops occuring after the 3rd time whereas this one doesn't seem to happen at all (I think that's what I read). As for the workaround, no luck there either. One member from the primefaces community got back to me and said only the latest snapshot is *somewhat* compatible with JSF 2.2. – Andy Jul 09 '13 at 20:55
  • JSF2.2 has quite a few bugs. If you want to spend more time on this (which sounds like a bug), you could put a breakpoint to your `FacesServlet.service` method. Check the request parameters and see that JS is actually sending the correct render/execute. A [bug](https://java.net/jira/browse/JAVASERVERFACES-2871) I raised (which has to do with multipart forms) messes up the ajax request parameters. I can see how this would fit your problem (I am not using primefaces so don't know how they interact with mojarra JSF.js) PS. I would also remove primefaces and make sure that it works with native JSF – Ioannis Deligiannis Jul 10 '13 at 09:08
  • @jhond 2.2 is too much of a hassle right now. I'm going back to 2.1. I give up. It sort of works with native jsf but I have to click the first button twice. At first I thought it was a reported bug but now I'm not so sure. Ill check again. – Andy Jul 10 '13 at 13:59

0 Answers0