richfaces使用ajax将数据发送到服务器

时间:2022-05-27 20:04:08

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.

我的应用程序允许用户在消息字段中键入文本,当他键入时,在htat时,它必须允许管理员查看在不同控制台中键入的内容。

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

为此,我需要定期向托管bean发送数据,并从那里发送到业务层。

      <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.

我有name和msg的managedBean属性,我需要访问名称和msg属性,并在我在clockBean托管Bean的process()方法中处理它们时将它们发送到业务层。

@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?

目前我的bean范围是ViewScoped,当轮询每20秒运行时,我得到2个字段的空值。当轮询在给定时间间隔内运行时,如何获取name和msg属性值?有没有更好的方法来解决这个问题?

2 个解决方案

#1


3  

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.

会话范围仅对当前用户可见。因此,如果你试图在管理员页面中获得#{clockBean},你实际上最终会得到一个全新的bean。为了使这些信息也可供管理员用户使用,您需要保留此信息并阅读它。

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.

更新:我不会通过轮询来执行此操作,因为即使数据没有更改,轮询也会每次都执行请求。我会用onchange事件,队列和请求延迟来做。如果a4j:poll没有提交表单(richfaces中的bug可能?),你可以使用a4j:function轻松实现它,只需创建一个js函数并用js中的setInterval()调用它。

#2


1  

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.

属性到我的轮询标签..所以与输入字段相关的值没有正确进入请求..所有输入都非常感激。

#1


3  

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.

会话范围仅对当前用户可见。因此,如果你试图在管理员页面中获得#{clockBean},你实际上最终会得到一个全新的bean。为了使这些信息也可供管理员用户使用,您需要保留此信息并阅读它。

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.

更新:我不会通过轮询来执行此操作,因为即使数据没有更改,轮询也会每次都执行请求。我会用onchange事件,队列和请求延迟来做。如果a4j:poll没有提交表单(richfaces中的bug可能?),你可以使用a4j:function轻松实现它,只需创建一个js函数并用js中的setInterval()调用它。

#2


1  

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.

属性到我的轮询标签..所以与输入字段相关的值没有正确进入请求..所有输入都非常感激。