Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://java.sun.com/javaee/javaserverfaces/1.1_01/docs/api/javax/faces/context/FacesContext.html#addMessage(java.lang.String,%20javax.faces.application.FacesMessage)" rel="noreferrer">FacesContext.addMessage(String, FacesMessage)</a> requires the component's <strong>clientId</strong>, <em>not</em> it's <strong>id</strong>. If you're wondering why, think about having a control as a child of a dataTable, stamping out different values with the same control for each row - it would be possible to have a different message printed for each row. The <strong>id</strong> is always the same; the <strong>clientId</strong> is unique per row.</p> <p>So "myform:mybutton" is the correct value, but hard-coding this is ill-advised. A lookup would create less coupling between the view and the business logic and would be an approach that works in more restrictive environments like portlets.</p> <pre><code>&lt;f:view&gt; &lt;h:form&gt; &lt;h:commandButton id="mybutton" value="click" binding="#{showMessageAction.mybutton}" action="#{showMessageAction.validatePassword}" /&gt; &lt;h:message for="mybutton" /&gt; &lt;/h:form&gt; &lt;/f:view&gt; </code></pre> <p>Managed bean logic:</p> <pre><code>/** Must be request scope for binding */ public class ShowMessageAction { private UIComponent mybutton; private boolean isOK = false; public String validatePassword() { if (isOK) { return "ok"; } else { // invalid FacesMessage message = new FacesMessage("Invalid password length"); FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(mybutton.getClientId(context), message); } return null; } public void setMybutton(UIComponent mybutton) { this.mybutton = mybutton; } public UIComponent getMybutton() { return mybutton; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload