Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The component <code>id</code> should indeed be unique. This is implicitly required by the HTML specification. You know, all JSF does is just generating the appropriate HTML/CSS/JS code. Give them all a different <code>id</code> or just leave it away, it has no additional value in this specific situation (unless you'd like to hook some CSS/JS on it).</p> <p>To achieve your functional requirement, you may find <code>f:setPropertyActionListener</code> useful.</p> <pre><code>&lt;h:commandButton action="answer" value="+"&gt; &lt;f:setPropertyActionListener target="#{calculator.operation}" value="+" /&gt; &lt;/h:commandButton&gt; &lt;h:commandButton action="answer" value="-"&gt; &lt;f:setPropertyActionListener target="#{calculator.operation}" value="-" /&gt; &lt;/h:commandButton&gt; &lt;h:commandButton action="answer" value="*"&gt; &lt;f:setPropertyActionListener target="#{calculator.operation}" value="*" /&gt; &lt;/h:commandButton&gt; &lt;h:commandButton action="answer" value="/"&gt; &lt;f:setPropertyActionListener target="#{calculator.operation}" value="/" /&gt; &lt;/h:commandButton&gt; </code></pre> <p>And have a property <code>operation</code> in your <code>calculator</code> managed bean:</p> <pre><code>private String operation; // +setter. </code></pre> <p>You can access it in the <code>getAnswer()</code> method and handle accordingly.</p> <hr> <p>Alternatively, let the buttons each point to a different bean action but which returns all <code>"answer"</code>:</p> <pre><code>&lt;h:commandButton action="#{calculator.add}" value="+" /&gt; &lt;h:commandButton action="#{calculator.substract}" value="-" /&gt; &lt;h:commandButton action="#{calculator.multiply}" value="*" /&gt; &lt;h:commandButton action="#{calculator.divide}" value="/" /&gt; </code></pre> <p>with the following methods in your <code>calculator</code> managed bean:</p> <pre><code>public String add() { answer = number1 + number2; return "answer"; } public String substract() { answer = number1 - number2; return "answer"; } // etc... </code></pre> <p>and just let <code>getAnswer()</code> return <code>answer</code> and do nothing else there. That's a more clean separation of responsibilities.</p>
    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. 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