html 如何使用JSF 2实现简单聊天
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 如何使用JSF 2实现简单聊天相关的知识,希望对你有一定的参考价值。
package ru.reshaka.labs.chat;
import java.io.Serializable;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
public class Message implements Serializable {
private Date dateSent;
private String user;
private String message;
public Date getDateSent() {
return dateSent;
}
public void setDateSent(Date dateSent) {
this.dateSent = dateSent;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
package ru.reshaka.labs.chat;
import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;
import ru.reshaka.labs.chat.ejb.MessageManagerLocal;
/**
*
* @author Anton Danshin
*/
@ManagedBean
@ViewScoped
public class MessageBean implements Serializable {
@EJB
MessageManagerLocal mm;
private final List messages;
private Date lastUpdate;
private Message message;
/**
* Creates a new instance of MessageBean
*/
public MessageBean() {
messages = Collections.synchronizedList(new LinkedList());
lastUpdate = new Date(0);
message = new Message();
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
public void sendMessage(ActionEvent evt) {
mm.sendMessage(message);
}
public void firstUnreadMessage(ActionEvent evt) {
RequestContext ctx = RequestContext.getCurrentInstance();
Message m = mm.getFirstAfter(lastUpdate);
ctx.addCallbackParam("ok", m!=null);
if(m==null)
return;
lastUpdate = m.getDateSent();
ctx.addCallbackParam("user", m.getUser());
ctx.addCallbackParam("dateSent", m.getDateSent().toString());
ctx.addCallbackParam("text", m.getMessage());
}
}
package ru.reshaka.labs.chat.ejb;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import ru.reshaka.labs.chat.Message;
@Singleton
@Startup
public class MessageManager implements MessageManagerLocal {
private final List messages =
Collections.synchronizedList(new LinkedList());;
@Override
public void sendMessage(Message msg) {
messages.add(msg);
msg.setDateSent(new Date());
}
@Override
public Message getFirstAfter(Date after) {
if(messages.isEmpty())
return null;
if(after == null)
return messages.get(0);
for(Message m : messages) {
if(m.getDateSent().after(after))
return m;
}
return null;
}
}
package ru.reshaka.labs.chat.ejb;
import java.util.Date;
import javax.ejb.Local;
import ru.reshaka.labs.chat.Message;
@Local
public interface MessageManagerLocal {
void sendMessage(Message msg);
Message getFirstAfter(Date after);
}
<?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:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
>
<h:head>
<title>Chat test!</title>
</h:head>
<script type="text/javascript">
setInterval("nextMessage()", 200); //update the chart every 200 ms
function updateMessages(xhr, status, args) {
if(!args.ok) return;
$('#chat').append('<div class="msg">[' +args.dateSent+ '] <strong>'+args.user+'</strong>: '+args.text+'</div>');
}
</script>
<h:body>
<h:form prependId="false">
<h:panelGrid columns="2">
Name: <p:inputText value="#{messageBean.message.user}"/>
Text: <p:inputText value="#{messageBean.message.message}"/>
<p:commandButton type="reset" value="Clear"/>
<p:commandButton value="Send!" actionListener="#{messageBean.sendMessage}"/>
</h:panelGrid>
<p:remoteCommand name="nextMessage" actionListener="#{messageBean.firstUnreadMessage}"
oncomplete="updateMessages(xhr, status, args);"/>
</h:form>
<hr/>
<h3>Live chat</h3>
<div id="chat"></div>
</h:body>
</html>
以上是关于html 如何使用JSF 2实现简单聊天的主要内容,如果未能解决你的问题,请参考以下文章