如何将 bean object/value 传递给 jsf(xhtml) 页面?
How to pass bean object/value to a jsf(xhtml) page?
我在当前可用版本中使用 Tomcat 和 PrimeFaces。
我遇到了一个问题,我无法将值从托管 bean 异步发送到它的 xhtml 页面。
这是我的 xhtml 页面中的代码片段:
<p:inputTextarea id="transcriptionResult" rows="4" cols="120"
value="#{transcriptionTabView.currentTranscritpion}" />
这是被管理 bean "transcriptionTabView" 中的 re-/set 和 object/value 异步调用的方法。其中包含一个名为 Transcription currentTranscription 的对象。
@Override
public void trascriptionReady(Transcription t) {
this.currentTranscription = t;
}
我认为这足以重置视图,但什么也没发生。
我用 PrimeFaces 的推送功能解决了这个问题。
在BalusC的提示下,我阅读了PrimeFaces的UserGuide Chapter 9. PrimeFaces Push PDF and watched this video教程。
这些是我为解决问题所做的步骤。
第 1 步:
我在 TranscriptionTabView-Bean 中添加了以下方法:
public void pushMessage() {
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/resultreceiver", result);
System.out.println("Message Sent at " + new Date());
}
步骤 2.
我创建了一个 Receiver-Bean 来接收我推送的消息:
@PushEndpoint(value = "/resultreceiver")
public class TranscriptionResultReceiver {
@OnMessage(encoders = { JSONEncoder.class })
public String onMessage(String message) {
return message;
}
}
步骤 3.
我向我的 JSF 添加了一个 <p:inputTextarea>
组件来显示消息:
<p:inputTextarea rows="6" cols="60"
id="transcriptionResult" value="#{transcriptionTabView.result}">
</p:inputTextarea>
步骤 4.
向等待传入消息的 jsf 添加一个套接字:
<p:socket channel="/resultreceiver" onMessage="handleMessage"></p:socket>
确保 channel 与 Receiver-Bean 中提到的 @PushEndpoint 注释相同。
步骤 5.
当 TranscriptionTabView-Bean 推送消息时,将以下 javascript 方法添加到由套接字标记调用的 JSF。
<script type="text/javascript">
function
handleMessage(data){
document.getElementById(<GENERATED_ID OF INPUTTEXTAREA>).value=data;
}
</script>
正如我通过阅读用户指南和观看 youtube 视频所理解的那样,TranscriptionTabView 中的 pushMessage 方法打开了一个到 JSF 的通道并使用 eventBus.publish("/resultreceiver", message);
发送消息 在客户端,套接字标签等待传入给定通道上的消息,如果 onMessage
事件发生,将调用 javascript 方法 handleMessage
对传入数据执行某些操作。
如有错误请指正。我是新手。
我在当前可用版本中使用 Tomcat 和 PrimeFaces。
我遇到了一个问题,我无法将值从托管 bean 异步发送到它的 xhtml 页面。
这是我的 xhtml 页面中的代码片段:
<p:inputTextarea id="transcriptionResult" rows="4" cols="120"
value="#{transcriptionTabView.currentTranscritpion}" />
这是被管理 bean "transcriptionTabView" 中的 re-/set 和 object/value 异步调用的方法。其中包含一个名为 Transcription currentTranscription 的对象。
@Override
public void trascriptionReady(Transcription t) {
this.currentTranscription = t;
}
我认为这足以重置视图,但什么也没发生。
我用 PrimeFaces 的推送功能解决了这个问题。 在BalusC的提示下,我阅读了PrimeFaces的UserGuide Chapter 9. PrimeFaces Push PDF and watched this video教程。 这些是我为解决问题所做的步骤。
第 1 步: 我在 TranscriptionTabView-Bean 中添加了以下方法:
public void pushMessage() {
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/resultreceiver", result);
System.out.println("Message Sent at " + new Date());
}
步骤 2. 我创建了一个 Receiver-Bean 来接收我推送的消息:
@PushEndpoint(value = "/resultreceiver")
public class TranscriptionResultReceiver {
@OnMessage(encoders = { JSONEncoder.class })
public String onMessage(String message) {
return message;
}
}
步骤 3.
我向我的 JSF 添加了一个 <p:inputTextarea>
组件来显示消息:
<p:inputTextarea rows="6" cols="60"
id="transcriptionResult" value="#{transcriptionTabView.result}">
</p:inputTextarea>
步骤 4.
向等待传入消息的 jsf 添加一个套接字:
<p:socket channel="/resultreceiver" onMessage="handleMessage"></p:socket>
确保 channel 与 Receiver-Bean 中提到的 @PushEndpoint 注释相同。
步骤 5. 当 TranscriptionTabView-Bean 推送消息时,将以下 javascript 方法添加到由套接字标记调用的 JSF。
<script type="text/javascript">
function
handleMessage(data){
document.getElementById(<GENERATED_ID OF INPUTTEXTAREA>).value=data;
}
</script>
正如我通过阅读用户指南和观看 youtube 视频所理解的那样,TranscriptionTabView 中的 pushMessage 方法打开了一个到 JSF 的通道并使用 eventBus.publish("/resultreceiver", message);
发送消息 在客户端,套接字标签等待传入给定通道上的消息,如果 onMessage
事件发生,将调用 javascript 方法 handleMessage
对传入数据执行某些操作。
如有错误请指正。我是新手。