Sanath
Sanath

Reputation: 4886

richfaces sending data to the server using ajax

My application lets the user to type text in the message field and when he is typing, at htat time it has to allow the admin to see what is being typed in a different console.

for this I need to send data periodically to the managed bean and from there to the business layer.

      <h:form>
          Name : <h:inputText id="name" value="#{clockBean.name}"/>
          Message: <h:inputText id="age" value="#{clockBean.msg}"/>
          <a4j:poll id="poll" interval="20000" enabled="#{clockBean.enabled}" action="#
           {clockBean.process}" render="clock,counter"/>
          <a4j:log/>        
      </h:form>

I have managedBean properties for name and msg and I need to access the name and msg properties and send them to the business layer when I process them in process() method of the clockBean managed Bean.

@ManagedBean 
@ViewScoped 

public class ClockBean implements Serializable{ 

private string msg; 
private string name; 
private boolean enabled; 

public void process(){ 

System.out.println("timer event calling *** - msg is "+msg+" : name is "+name); } 

//getters setters & rest of the code

currently I have my bean scope as ViewScopedand I am getting null values for the 2 fields when the poll runs every 20 seconds. How can I get the name and msg property values when the poll runs in a given time interval? is there any better approach to resolving this issue?

Upvotes: 4

Views: 1019

Answers (2)

Sanath
Sanath

Reputation: 4886

got the answer to my issue .. I have not added

 execute="@form" 

property to my poll tag..so the values that were related to the input fields were not getting into the request properly.. all the input was highly appreciated.

Upvotes: 2

bogdan.mustiata
bogdan.mustiata

Reputation: 1845

The session scope is only visible for the current user. Thus if you will try to get #{clockBean} in the admin's page you'll actually end up with a brand new bean. In order to make this information available to the admin user as well, you will need to persist this information and read it.

Update: I wouldn't do it with polling since polling does the request every time even if the data doesn't change. I would do it with onchange events, a queue and a request delay. If a4j:poll doesn't submits the form (bug in richfaces maybe?), you can implement this easily with a4j:function and just create a js function and call it with setInterval() from js.

Upvotes: 4

Related Questions