4

ap:notificationBar にメッセージを書き込むために、primefaces push を使用します。特殊文字 (ロシア文字など) を含むメッセージをプッシュすると、'?' が表示されます。私のメッセージで。この問題を解決するにはどうすればよいですか? ご協力いただきありがとうございます。

(構成:primefaces 3.4およびjsf2。すべてのhtmlページはutf8エンコーディングです)。

これが私のビューコードです:

<p:notificationBar id="bar"
                widgetVar="pushNotifBar"
                position="bottom"
                style="z-index: 3;border: 8px outset #AB1700;width: 97%;background: none repeat scroll 0 0 #CA837D;">
            <h:form prependId="false">
                <p:commandButton icon="ui-icon-close"
                            title="#{messages['gen.close']}"
                            styleClass="ui-notificationbar-close"
                            type="button"
                            onclick="pushNotifBar.hide();"/>
            </h:form>
            <h:panelGrid columns="1" style="width: 100%; text-align: center;">
                <h:outputText id="pushNotifSummary" value="#{growlBean.summary}" style="font-size:36px;text-align:center;"/>
                <h:outputText id="pushNotifDetail" value="#{growlBean.detail}" style="font-size: 20px; float: left;" />
            </h:panelGrid>
        </p:notificationBar>
        <p:socket onMessage="handleMessage" channel="/notifications"/>

        <script type="text/javascript">
            function handleMessage(data) {
            var substr = data.split(' %% ');
            $('#pushNotifSummary').html(substr[0]);
            $('#pushNotifDetail').html(substr[1]);
            pushNotifBar.show();

            }

        </script>  

と私の Bean コード:

public void send() { 
        PushContext pushContext = PushContextFactory.getDefault().getPushContext(); 
        String var = summary + " %% " +  detail;
        pushContext.push("/notifications", var);
4

1 に答える 1

2

これは、おそらく問題の原因となる Atmosphere フレームワーク (Primefaces で使用される) エンコーディング メカニズムに依存しないソリューションです。

メッセージを Base64 でエンコードされた文字列としてプッシュし、Java スクリプトを使用してユーザー側でデコードするという考え方です。

1.Base64を使用してメッセージ文字列をエンコードし、プッシュします

public void send() { 
    PushContext pushContext = PushContextFactory.getDefault().getPushContext(); 
    String var = summary + " %% " +  detail;
    byte b[] = var.getBytes("UTF-8");
    byte b64[] = Base64.encodeBase64(b);
    String message = new String(b64);
    pushContext.push("/notifications", message);
}

org.apache.commons.codec ライブラリにある Base64 エンコーダーを使用して Java コードにインポートできます。

import org.apache.commons.codec.binary.Base64;

2.メッセージがクライアント側に到着したら、次のJavaスクリプトを使用してデコードします

<script type="text/javascript">
    function handleMessage(data) {
      var decodedmessage=b64_to_utf8(data);
      var substr = decodedmessage.split(' %% ');
      $('#pushNotifSummary').html(substr[0]);
      $('#pushNotifDetail').html(substr[1]);
      pushNotifBar.show();
    }

    function b64_to_utf8( str ) {
      return decodeURIComponent(escape(window.atob( str )));
    }
</script> 

Base64 デコード関数 window.atob はブラウザに依存しないソリューションであることに注意してください。詳細については、こちらをご覧ください

于 2013-07-02T04:46:08.207 に答える