2016-12-04 60 views
-2

我投入使用multithreding和JSF 2.2插座即时消息收发应用,richafes 4.5,Tomcat的7.更新JSF组件的值时bean属性值改变

当客户端1发送到客户端2的消息,后者更新bean属性,但只有当我更新页面时,client2组件才会更改它的值。

facescontext发布后有没有更新组件的方法?

线程客户:

public class ThreadWait extends Thread { 
      public void run() { 

       String msg; 

       try { 
        while ((msg = input.readLine()) != null) { 

         editorBean.setMsg(msg); 

         /*** KO because the FacesContext already released *******/ 
         UIViewRoot viewRoot = viewHandler.createView(context, context.getViewRoot().getViewId()); 
         context.setViewRoot(viewRoot); 
         context.renderResponse(); 
         /***************/  
        } 

       } catch (IOException e) { 
        System.err.println("IOException" + e); 
       } 
      } 
     } 

豆Message.java:

public void send() { 

     FacesContext context = FacesContext.getCurrentInstance(); 
     Application application = context.getApplication(); 
     ViewHandler viewHandler = application.getViewHandler(); 
     UIViewRoot viewRoot = viewHandler.createView(context, context.getViewRoot().getViewId()); 

     if (connected) { 
      client.sendMessage(treeBean.getCurrentSelection() + "{MESSAGE}" + editor.getMsg()); 
      return; 
     } 

     client = new Client(editor, signIn, context, application, viewHandler, viewRoot); 
     // test if we can start the Client 
     if (!client.runConnexion(treeBean.getCurrentSelection() + "{MESSAGE}" + editor.getMsg())) 
      return; 

     connected = true; 

     return; 
} 

豆编辑:

@ManagedBean 
@SessionScoped 
public class EditorBean implements Serializable { 
    private static final long serialVersionUID = 1L; 


    private String msg; 

    /** 
    * @return the msg 
    */ 
    public String getMsg() { 
     return msg; 
    } 

    /** 
    * @param msg 
    *   the msg to set 
    */ 
    public void setMsg(String msg) { 
     this.msg = msg; 
    } 

    public EditorBean() { 

    } 

    public EditorBean(String msg) { 
     super(); 
     this.msg = msg; 
    } 

} 

的index.xhtml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:rich="http://richf`enter code here`aces.org/rich" xmlns:a4j="http://richfaces.org/a4j" 
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> 
<h:head> 
    <title>Welcome</title> 
</h:head> 
<h:body> 


    <h:form> 
     <table> 
      <tr> 
       <td> 
        <div> 
         <rich:editor id="editor" toolbar="full" value="#{editorBean.msg}" style="margin-bottom: 1em"> 
         </rich:editor> 

         <h:commandButton value="Send" action="#{message.send}" type="submit"></h:commandButton> 

        </div> 
       </td> 
      </tr> 
     </table> 
    </h:form> 

</h:body> 
</html> 
+0

的可能的复制[?哪有服务器推送异步改变由JSF创建的HTML页面(http://stackoverflow.com/questions/3787514/how-can- server-push-asynchronous-changes-to-a-html-page-created-by-jsf) – BalusC

+0

感谢您的帮助 –

回答

0

解决方案使用A4J:民调:

<a4j:poll id="poll" interval="2000" enabled="#{pollBean.pollEnabled}" render="poll,grid" /> 

<rich:panel width="80%" id="grid"> 
    <f:facet name="header">Messages</f:facet> 
    <h:outputText id="serverDate" escape="false" value="#{editorBean.msgReveive}" /> 
</rich:panel>