注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

Simple chat room with JSF2(primefaces),updated to resove messy code of Chinese  

2010-06-21 21:09:26|  分类: jsf2.0 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Download jar(primefaces-2.0.2.jar) from primefaces;

Tools:  NetBean6.9 and GlassFish Server 3

Libraries:

Simple chat room with JSF2(primefaces) - yyq2007aa - 流星永恒的博客
 
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.allowTextChildren</param-name>
        <param-value>true</param-value>
    </context-param>
    <servlet>
        <servlet-name>Resource Servlet</servlet-name>
        <servlet-class>
          org.primefaces.resource.ResourceServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Resource Servlet</servlet-name>
        <url-pattern>/primefaces_resource/*</url-pattern>
    </servlet-mapping>
    <!--context-param>
        <param-name>SESSION_EXPIRED_PAGE</param-name>
        <param-value>sessionExpired.html</param-value>
    </context-param-->
   
    <servlet>
        <servlet-name>Comet Servlet</servlet-name>
        <servlet-class>org.primefaces.comet.PrimeFacesCometServlet</servlet-class>
        <async-supported>true</async-supported><!--//############NOTE HERE!############//-->
    </servlet>
    <servlet-mapping>
        <servlet-name>Comet Servlet</servlet-name>
        <url-pattern>/primefaces_comet/*</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>1</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsf</welcome-file>
    </welcome-file-list>
</web-app>
 
ManagedBean--ChatBean
ChatBean.java

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.faces.event.ActionEvent;
import org.primefaces.comet.CometContext;

/**
 *
 * @author Administrator
 */
@Named(value="chatBean")
@RequestScoped
public class ChatBean {
String message;
    /** Creates a new instance of ChatBean */
    public ChatBean() {
    }

    public void send(ActionEvent evt) throws UnsupportedEncodingException{

    //解决中文乱码的问题,删除原来的

       //CometContext.publish("chat", message);
        CometContext.publish("chat", URLEncoder.encode(message,"UTF-8"));
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

 

Page--chat.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:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <h:head>
        <title>Chat Room</title>
        <script type="text/javascript">
            var jq = jQuery;
                        function handlePublish(pushed){

                //解决中文问题

                 //var msg = decodeURI(pushed.data); //解码并替换“+”号为空格

                 var msg = decodeURIComponent(pushed.data); //消息中带有html标签,解码并替换“+”号为空格

                  msg = msg.replace(/\+/g, " "); //相当于replaceAll

                 jq("#outPanel_content").append(msg+"<br/>");
                //jq("#outPanel_bd").append(pushed.data+"&lt;br/&gt;");
            }
        </script>
    </h:head>
    <h:body>
        <h:form prependId="false">
            <h:inputText value="#{chatBean.message}"/>
            <p:commandButton value="Send" actionListener="#{chatBean.send}"/>
        </h:form>
        <p:panel id="outPanel"/>
        <p:push channel="chat" onpublish="handlePublish"/>
    </h:body>
</html>

Pictures:

1.

2.Simple chat room with JSF2(primefaces) - yyq2007aa - 流星永恒的博客
 
Simple chat room with JSF2(primefaces) - yyq2007aa - 流星永恒的博客
  评论这张
 
阅读(3748)| 评论(2)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018