2016-12-04 81 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>