当bean属性更改值

时间:2016-12-04 00:26:47

标签: jsf

我正在使用多线程和JSF 2.2套接字,richafes 4.5,Tomcat 7开发即时消息应用程序。

当client1将消息发送到client2时,后者更新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);
                }
            }
        }

Bean 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;
}

Bean编辑器:

@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 个答案:

没有答案