views:

222

answers:

1

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)

Thank you!!!

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

+3  A: 

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)

Bozho
Thanks!How do I get "f:setPropertyActionListener" to evaluate only when I press the button that it is assigned to?
Ben
see my updated answer.
Bozho
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
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
I'm creating these buttons dynamically so I have to do this from the backing bean with 'binding'.
Ben
well, it's just a special `ActionListener`. Depending on your jsf implementation it differs. In MyFaces it's `SetPropertyActionListener`
Bozho
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
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