0

I would like to add buttons dynamically from a backing bean to a JSF page (supporting Rich Faces as well).

The value of the buttons needs to be determined in run time and returned to the backing bean when the button is pressed. (Hence the title - I am actually trying to be able to do something like "#{beans.run(3)}", i.e - set a fixed parameter to be used when clicking a button)

So for example, if the user creates a button (on run time) and gives the button a value. This value should be returned to the backing bean to be analysed.

My question - How do I assign a button (the button is a JSF component with a4j:support child) with a value at runtime? (I tried using a4j:actionParam, but couldn't manage to work it out)

P.S - I've overhauled this question to be shorter and more to the point from the original-too-long-question

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ben
  • 10,020
  • 21
  • 94
  • 157

1 Answers1

3

There are a number of opions:

  • use JSF 2.0
  • use JBoss EL extension
  • use <f:setPropertyActionListener value="3" target="#{bean.propety>, where propety is later read by the run() method.

    <h:commandButton action="#{bean.run}">
        <f:setPropertyActionListener target="#{bean.property}" 
            value="#{pageVariable}" />
    </h:commandButton>
    <!-- pageVariable contains the number you are passing -->
    
    public class Bean {
       private int property; // with setters and getters
       public void run() {
          // do something with property
       }
    }
    
  • use Facelets functions (here's an example for such a function) (not applicable in all cases)

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks! How do I get "f:setPropertyActionListener" to evaluate only when I press the button that it is assigned to? – Ben Feb 08 '10 at 13:42
  • Tnx Again. I have 2 problems with f:setPropertyActionListener: (1) I am not using h:commandButton but rather a4j:support under an html panel grid. (2) I wanted to add f:setPropertyActionListener to a4j:support children but I could not find the object representation of it to create in the backing bean. – Ben Feb 08 '10 at 14:53
  • 1. no problem. `f:setPropetyActionListener` works with all action components. 2. Why are you using binding with `a4j:support`. I'd advise against it. – Bozho Feb 08 '10 at 15:17
  • I'm creating these buttons dynamically so I have to do this from the backing bean with 'binding'. – Ben Feb 08 '10 at 15:41
  • well, it's just a special `ActionListener`. Depending on your jsf implementation it differs. In MyFaces it's `SetPropertyActionListener` – Bozho Feb 08 '10 at 15:48
  • I'd like to read more about why it's bad to use 'binding'. Do you know where I can read more about it? Thanks for all the help. – Ben Feb 08 '10 at 15:58
  • It is not necessarily bad, but it should be used only if necessary. I've been using richfaces for more than a year now, in two projects, and needed the `binding` only a once of twice. – Bozho Feb 08 '10 at 16:32