Ajax request and attributes

241 Views Asked by At

I am trying this tutorial which describes how to set attributes into server call and how to analyse the attributes on backing bean;

<h:commandButton id="submit" 
actionListener="#{userData.attributeListener}" action="result"> 
   <f:attribute name="value" value="Show Message" />                
   <f:attribute name="username" value="JSF 2.0 User" />
</h:commandButton>

I googled a lot but most examples show how to set attrs for synch-ed calls not the asynch-ed ones :S So my question is ... how to send attributes on server if that would be the ajax call and how to get them on backing bean (see suggestion A code snippet)?

suggestion A :

<h:commandButton id="submit" 
    actionListener="#{userData.attributeListener}" action="result"> 
       <f:ajax>
            <f:attribute/>? how to
       </f:ajax>

    </h:commandButton>

and if there is a good tutorial concerning to this question please do share the link :)

Thanks

2

There are 2 best solutions below

0
cbhogf On BEST ANSWER

OK I just wrote a test which sets attributes out the ajax block as :

<h:commandButton id="submit" 
    actionListener="#{userData.callWithAttributes}" action="result"> 
       <f:attribute name="a" value="#{testa}"/>
       <f:attribute name="b" value="#{testb}"/>
       <f:ajax .../>
</h:commandButton>

...and on the backing bean

...
public void callWithAttributes(ActionEvent e){
  String a=(String) e.getComponent().getAttributes().get("a");
  ...
}
...

Seems like it's working fine :) So attributes should be placed into event-making component block - the h:commandButton in this particular case...

0
djmj On

To set two properties in backing bean you can use f:setPropertyActionListener

<h:commandButton action="#{bean.method}"> 
   <f:setPropertyActionListener value="#{testA}" target="#{bean.valueA}/>
   <f:setPropertyActionListener value="#{testB}" target="#{bean.valueB}/>
</h:commandButton>

or with EL method argument support:

<h:commandButton action="#{bean.method(testA, testB)}"/>