【问题标题】:Update the value of the jsf component when the bean property changes valuebean属性改变值时更新jsf组件的值
【发布时间】:2016-12-04 00:26:46
【问题描述】:

我正在开发一个使用多线程和 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>

【问题讨论】:

标签: jsf


【解决方案1】:

使用 a4j:poll 的解决方案:

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

【讨论】:

    猜你喜欢
    • 2012-06-25
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多