2

私の目標は、チャットで2人のユーザー(教師と生徒)をサブスクライブするページの読み込みであり、お互いにメッセージを送信できるので、ここで私がしたことは次のとおりです。

1-バッキングビーン:

package com.myapp.beans;

import org.primefaces.context.RequestContext;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ocpsoft.pretty.faces.annotation.URLMapping;
import com.xeno.xenoTemplate.utils.constants.Pages;

@Component("chatBean")
@Scope("view")
@URLMapping(id = Pages.CHAT, pattern = "/chat", viewId = "/faces/pages/users/chat.xhtml")
public class ChatView {

    private final PushContext pushContext = PushContextFactory.getDefault()
            .getPushContext();

    private String privateMessage;

    private String privateUser;

    private final static String CHANNEL = "/chat/";

    private String student = "student";

    private String teacher = "teacher";

    private boolean pageLoaded;

    private boolean studentLoggedIn;

    private boolean teacherLoggedIn;

    public void preRender() {
        System.out.println("########### preRender CHAT BEAN #########");
        if (!pageLoaded) { // invoked first time page loaded
            System.out.println("########### Invoking PreRender Code #########");
            if (!studentLoggedIn) {
                System.out.println("########## STUDENT LOG IN ##########");
                RequestContext.getCurrentInstance().execute(
                        "subscriber.connect('/" + student + "')");
                studentLoggedIn = true;
            }
            if (!teacherLoggedIn) {
                System.out.println("########## TEACHER LOG IN ##########");
                RequestContext.getCurrentInstance().execute(
                        "subscriber.connect('/" + teacher + "')");
                teacherLoggedIn = true;
            }
            pageLoaded = true;
        }
    }

    public void sendPrivate() {
        System.out.println("######### SEND PRIVATE ##########");
        pushContext.push(CHANNEL + privateUser, "[PM] " + "PRIVATE" + ": "
                + privateMessage);
        privateMessage = null;
    }

    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }

    public String getTeacher() {
        return teacher;
    }

    public void setStudent(String student) {
        this.student = student;
    }

    public String getStudent() {
        return student;
    }

    public void setStudentLoggedIn(boolean studentLoggedIn) {
        this.studentLoggedIn = studentLoggedIn;
    }

    public boolean isStudentLoggedIn() {
        return studentLoggedIn;
    }

    public void setTeacherLoggedIn(boolean teacherLoggedIn) {
        this.teacherLoggedIn = teacherLoggedIn;
    }

    public boolean isTeacherLoggedIn() {
        return teacherLoggedIn;
    }

    public String getPrivateUser() {
        return privateUser;
    }

    public void setPrivateUser(String privateUser) {
        this.privateUser = privateUser;
    }

    public String getPrivateMessage() {
        return privateMessage;
    }

    public void setPrivateMessage(String privateMessage) {
        this.privateMessage = privateMessage;
    }

}

そしてJSFページで:

<!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:f="http://java.sun.com/jsf/core"
    xmlns:ice="http://www.icesoft.com/icefaces/component"
    xmlns:p="http://primefaces.org/ui"
    xmlns:pretty="http://ocpsoft.com/prettyfaces"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:sec="http://www.springframework.org/security/tags"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

<h:head>
    <title>Chat</title>
</h:head>

<h:body>

    <ui:composition template="/pages/template/commonLayout.xhtml">

        <ui:define name="content">
        <f:event type="preRenderView" listener="#{chatBean.preRender}" />

            <h:form id="form">

                <p:fieldset id="container" legend="PrimeChat" toggleable="true">

                    <h:panelGroup>

                       <h:panelGrid columns="2" columnClasses="publicColumn,usersColumn"
                            style="width:400px;">

                       <p:outputPanel style="height: 100px;" id="public" layout="block"
                                styleClass="ui-corner-all ui-widget-content chatlogs" />

                       <h:panelGroup id="users" styleClass="usersList">

                                <p:commandButton id="private_send" title="Chat" icon="ui-icon-comment"
                                    oncomplete="pChat.show()" update=":form:privateChatContainer">
                                    <f:setPropertyActionListener value="#{chatBean.student}"
                                        target="#{chatBean.privateUser}" />
                                </p:commandButton>
                        #{chatBean.student}
                        </h:panelGroup>

                        </h:panelGrid>



                    </h:panelGroup>


                </p:fieldset>

                <!-- Private Chat Dialog -->
                <p:dialog widgetVar="pChat" header="Private Chat" modal="true"
                    showEffect="fade" hideEffect="fade">
                    <h:panelGrid id="privateChatContainer" columns="2"
                        columnClasses="vtop,vtop">
                        <p:outputLabel for="pChatInput"
                            value="To: #{chatBean.privateUser}" />
                        <p:inputTextarea id="pChatInput"
                            value="#{chatBean.privateMessage}" rows="5" cols="30" />

                        <p:spacer />
                        <p:commandButton value="Send"
                            actionListener="#{chatBean.sendPrivate}"
                            oncomplete="pChat.hide()" />
                    </h:panelGrid>
                </p:dialog>

            </h:form>

            <p:socket onMessage="handleMessage" channel="/chat"
                autoConnect="false" widgetVar="subscriber" />

            <script type="text/javascript">
                function handleMessage(data) {
                    alert('handleMessage');
                    var chatContent = $(PrimeFaces
                            .escapeClientId('form:public'));
                    chatContent.append(data + '<br />');
                    chatContent.scrollTop(chatContent.height());
                }
            </script>



        </ui:define>

    </ui:composition>

</h:body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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"
      version="3.0"> 

  <display-name>MyAPP</display-name>

  <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

  <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        classpath:META-INF/spring/applicationContext.xml
        classpath:META-INF/spring/applicationSecurity.xml
        </param-value>

  </context-param>

  <context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>
    /WEB-INF/springsecurity.taglib.xml;/WEB-INF/utils.taglib.xml
    </param-value>
  </context-param>


  <context-param>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>false</param-value>
  </context-param>





  <!--  -->
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <welcome-file-list>
    <welcome-file>/</welcome-file>
  </welcome-file-list>

  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
  </context-param>

  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>


   <filter>
      <filter-name>hibernateFilter</filter-name>
      <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
      <init-param>
         <param-name>sessionFactoryBeanName</param-name>
         <param-value>sessionFactory</param-value>         
      </init-param>      
   </filter>

   <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping> 

  <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <!--  
    <filter>
      <filter-name>Pretty Filter</filter-name>
      <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
      <init-param>
            <param-name>logLevel</param-name>
            <param-value>ERROR</param-value>
      </init-param>
    </filter>

    <filter-mapping>
    <filter-name>Pretty Filter</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>FORWARD</dispatcher>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    -->

    <session-config>
      <session-timeout>30</session-timeout>
    </session-config>



  <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>/faces/*</url-pattern>
  </servlet-mapping>

  <error-page>
     <exception-type>java.lang.Throwable</exception-type>
     <location>/error</location>
  </error-page>

   <error-page>
     <error-code>400</error-code>
     <location>/error</location>
   </error-page>

   <error-page>
     <error-code>404</error-code>
     <location>/pageNotFound</location>
   </error-page>

   <!-- PrimePush Servlet -->

   <servlet>
      <servlet-name>Push Servlet</servlet-name>
      <servlet-class>org.primefaces.push.PushServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
        <init-param>
          <param-name>org.atmosphere.cpr.broadcasterCacheClass</param-name>
          <param-value>org.atmosphere.cache.HeaderBroadcasterCache</param-value>
        </init-param>
        <init-param>
          <param-name>org.atmosphere.cpr.broadcasterClass</param-name>
          <param-value>org.atmosphere.cpr.DefaultBroadcaster</param-value>
        </init-param>
        <init-param>
          <param-name>org.atmosphere.cpr.broadcastFilterClasses</param-name>
          <param-value>org.atmosphere.client.TrackMessageSizeFilter</param-value>
        </init-param>    
        <init-param>
          <param-name>org.atmosphere.cpr.sessionSupport</param-name>
          <param-value>true</param-value>
        </init-param>
     </servlet>

     <servlet-mapping>
       <servlet-name>Push Servlet</servlet-name>
       <url-pattern>/primepush/*</url-pattern>
     </servlet-mapping>

</web-app>

私は次の瓶を使用しています:

  1. JSF 2.1.10
  2. PrimeFaces 4.0-スナップショット
  3. トムキャット 7.0.32
  4. 大気ランタイム 1.0.1

ヘッダー

Request URL:http://localhost:8080/MyAPP/chat
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/xml, text/xml, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:282
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:JSESSIONID=2BB4298A4E291CCCBB80EE3A4A5624BC
Faces-Request:partial/ajax
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/PrimeTemplate/chat
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11
X-Requested-With:XMLHttpRequest
Form Dataview URL encoded
javax.faces.partial.ajax:true
javax.faces.source:form:j_idt31
javax.faces.partial.execute:@all
form:j_idt31:form:j_idt31
form:form
form:j_idt24:
form:container_collapsed:false
form:pChatInput:aaaaaaaaaaaa
javax.faces.ViewState:5079906166114901626:-433970771510989466
Response Headersview source
Cache-Control:no-cache
Content-Length:190
Content-Type:text/xml;charset=UTF-8
Date:Thu, 18 Oct 2012 10:40:04 GMT
Server:Apache-Coyote/1.1
X-Powered-By:JSF/2.0

応答:

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="javax.faces.ViewState"><![CDATA[5079906166114901626:-433970771510989466]]></update></changes></partial-response>

問題:

学生用のチャット ウィンドウを開き、チャット メッセージを入力して Enter キーを押します。コードはsendPrivateBean メソッドに入りますが、JShandleMessage関数が呼び出されないため、プライベート メッセージが JSF ページに表示されません。エラーは発生しません。ブラウザコンソールで。

それを修正する方法を教えてください。

4

1 に答える 1

1

次の jar を lib フォルダーに追加してみてください (まだ行っていない場合)。

大気互換トムキャット-1.0.1.jar

大気-compat-tomcat7-1.0.1.jar

また、STUDENT として「学生」チャネルに接続し、メッセージを「教師」チャネルにプッシュしている場合、教師のチャネルのみがメッセージを受信することにも注意してください。

チャットをテストするには、あるブラウザでは学生として、別のブラウザでは教師としてログインする必要があります。この場合、あるブラウザで学生として教師にメッセージをプッシュすると、別のブラウザの教師がメッセージを受信します。

于 2013-07-03T05:49:48.490 に答える