在网上搜索答案后,他们都没有工作我决定在这里发一个问题。 我正在尝试使用Activemq JMS和Java JSF创建一个聊天应用程序。在用户收到消息后,我更改了inputTextArea的值,但我不知道如何在前端更新它,以便消息显示在页面上。 我正在使用Primefaces 6.2和JSF 2.2
这是我的jsf页面:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Chat with #{singleChatBean.manager.pickedFriend}</title>
</h:head>
<h:body>
<h:form id="form">
<h3>#{singleChatBean.manager.pickedFriend}</h3>
<div id="receivedMessageDiv">
<p:inputTextarea id="receivedMessage" value="#{singleChatBean.messages}" rows="20" style="width: 50%"/>
</div>
<br></br>
<div id="sendMessage">
<p:inputText value="#{singleChatBean.newMessage}" style="width: 45%" autocomplete="false" />
<p:commandButton value="Send" update="receivedMessage" action="#{singleChatBean.sendMessage()}" style="width: 5%" />
</div>
</h:form>
</h:body>
</html>
这是我的支持豆:
@SessionScoped
@Named
public class SingleChatBean implements Serializable {
@Inject
private ManagerBean manager;
private ChatBrokerGateway cbg;
private String newMessage;
private String messages;
@PostConstruct
public void init() {
this.cbg = new ChatBrokerGateway(this.manager.getUser().getUsername()) {
@Override
public void onMessageReceived(Message msg) {
try {
String text = msg.getStringProperty("msg");
String fromUser = msg.getStringProperty("fromUser");
messages += "\n" + fromUser + ": " + text;
//refresh jsf component
} catch (JMSException ex) {
java.util.logging.Logger.getLogger(SingleChatBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
}
public ManagerBean getManager() {
return this.manager;
}
public String getNewMessage() {
return newMessage;
}
public void setNewMessage(String newMessage) {
this.newMessage = newMessage;
}
public String getMessages() {
return messages;
}
public void setMessages(String messages) {
this.messages = messages;
}
public void sendMessage() {
Logger.getLogger(SingleChatBean.class).info("Message: " + this.newMessage);
cbg.sendMessage(newMessage, manager.getUser(), manager.getPickedFriend());
this.messages += ("\nYou: " + this.newMessage);
}
}
我尝试过多种方法,比如ajax,RequestContext#update()和FaceContext#getRenderedIds()。add()。
有人能帮助我吗?