Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
997 views
in Technique[技术] by (71.8m points)

jsf - <p:commandButton> not working when disable="true" initially

This is my ManagedBean:

@Named(value = "mrBean")
@RequestScoped
public class MrBean {

   public void laugh() {
      System.out.println("HAHAHA");
   }

   public void prepareToLaugh() {
      System.out.println("Drink water.");
   }

}

And this is the working version of my commandButton:

<p:commandButton actionListener="#{mrBean.laugh}" widgetVar="laughtButton"
                 value="Laugh" oncomplete="laughButton.disable();"  />

When I clicked the above button, I saw HAHAHA and the button is disabled. However, when I set the laughButton's disable attribute to true, the button does not work anymore:

<p:commandButton actionListener="#{mrBean.laugh}" widgetVar="laughtButton"
                 value="Laugh" disabled="true" oncomplete="laughButton.disable();"  />

<p:commandButton actionListener="#{mrBean.prepareToLaugh}"
                 value="Prepare to laugh" oncomplete="laughButton.enable();" />

When I click the 2nd button, I saw Drink water and the 1st button is enabled. However, when I click on the 1st button, nothing happens.

I'd be very grateful if someone could give me an advice on how I should tackle this problem. I'm using PrimeFaces 3.0 RC2.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Like as with rendered attribute, JSF re-evaluates the disabled attribute in the server side during processing of the form submit as part of safeguard against tampered requests and like. You're however enabling/disabling it by JS without notifying the server side of the changes.

You need to ensure that the value of the disabled attribute evaluates false during the request whenever you intend the button to be enabled during processing of the form submit. For example, bind it to a boolean property of a view scoped bean which is set by the other button.

<h:commandButton disabled="#{bean.laughDisabled}" />

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...