3

问题:

我们有一个基于 Apache MyFaces Trinidad 的 Java Web 应用程序。在尝试部分提交(就是这样:通过 Ajax)表单时,当字段包含一些特殊字符(例如 Ñ、á 等)时,我们会遇到一些问题。

camión当我们在文本字段中写入一个值时,生成的消息是Hola cami(它删除了特殊字符,然后是下一个)。

我们运行代码的应用服务器是 IBM WebSphere AS 7.0。

此行为已在 IExplorer 和 Firefox 浏览器上观察到,它们都从 Windows XP Professional(西班牙语版本)运行。

主要代码片段

更新:根据lkdg的回答(谢谢),我更新了 JSP 和生成的 HTML 代码片段。

JSP页面代码:

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>  
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>  
<%@ taglib uri="http://myfaces.apache.org/trinidad" prefix="tr" %>  
<%@ taglib uri="http://myfaces.apache.org/trinidad/html" prefix="trh" %>  

<f:view>  
    <tr:document>  
        <trh:head title="Prueba de AJAX con todas las Cabeceras">  
           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
        </trh:head>  

        <tr:form>
        <tr:panelHorizontalLayout halign="left">
            <f:facet name="separator">
                <tr:spacer width="10" height="5"/>
            </f:facet>

            <tr:inputText id="elCampo" label="Your name" value="#{commandButtonBean.name}"
                          requiredMessageDetail="Name is required"

                          />
            <tr:commandButton id="sayHello" text="Say Hello"
                              partialSubmit="true" 
                              action="#{commandButtonBean.sayHello}"
                              />
        </tr:panelHorizontalLayout>

        <tr:spacer height="15px"/>
        <tr:outputText value="#{commandButtonBean.message}" partialTriggers="sayHello" 
                       inlineStyle="font-weight: bold;"/>
        </tr:form>
    </tr:document>  
</f:view>

这是生成的 HTML 输出(删除了一些空行):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!--Start: org.apache.myfaces.trinidad.Document["j_id_jsp_1876237926_1"]--><html dir="ltr" lang="es-ES">
  <head>
    <meta name="generator" content="Apache MyFaces Trinidad">

    <link rel="stylesheet" charset="UTF-8" type="text/css" href="/ModuloTrinidadPilotoWeb/adf/styles/cache/bigfont-desktop-nur72r-ltr-gecko.css">
  </head>
  <!--Start: org.apache.myfaces.trinidad.Document["j_id_jsp_1876237926_1"]-->
  <body onload="_checkLoad()" onunload="_checkUnload(event)"><script type="text/javascript">var _AdfWindowOpenError='Se ha detectado un bloqueo de ventana emergente en el explorador. Estos bloqueos interfieren con el funcionamiento de esta aplicación. Desactívelo o permita elementos emergentes desde esta dirección.';</script><script type="text/javascript" src="/ModuloTrinidadPilotoWeb/adf/jsLibs/Common1_2_10.js"></script><!--Start: org.apache.myfaces.trinidad.Document["j_id_jsp_1876237926_1"]--><div id="tr_pprBlockingDiv" onclick="return _pprConsumeClick(event);" style="position:absolute;left:0;top:0;width:0;height:0;cursor:wait;" onkeydown="return false;" onkeyup="return false;" onmousedown="return false;" onmouseup="return false;" onkeypress="return false;"></div><a name="top"></a>

    <noscript>Esta p&aacute;gina utiliza JavaScript y necesita un explorador activado para JavaScript. Su explorador no est&aacute; activado para JavaScript.</noscript>
    <!--Start: org.apache.myfaces.trinidad.Head["j_id_jsp_1876237926_2"]-->
    <head>
      <title>Prueba de AJAX con todas las Cabeceras</title>

      <meta name="generator" content="Apache MyFaces Trinidad">

      <link rel="stylesheet" charset="UTF-8" type="text/css" href="/ModuloTrinidadPilotoWeb/adf/styles/cache/bigfont-desktop-nur72r-ltr-gecko.css">

           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
        </head>

    <!--Start: org.apache.myfaces.trinidad.Form["j_id_jsp_1876237926_3"]-->
    <form id="j_id_jsp_1876237926_3" name="j_id_jsp_1876237926_3" style="margin:0px" method="POST" onkeypress="return _submitOnEnter(event,'j_id_jsp_1876237926_3');" action="/ModuloTrinidadPilotoWeb/jsp/sgtc/pruebaAjax2.faces"><!--Start: org.apache.myfaces.trinidad.Panel["j_id_jsp_1876237926_4"]--><table cellpadding="0" cellspacing="0" border="0" summary=""><tr>
          <td><!--Start: org.apache.myfaces.trinidad.Input["elCampo"]--><table id="elCampo__xc_" class="af_inputText" cellpadding="0" cellspacing="0" border="0" summary=""><tr>
                <td class="af_inputText_label" nowrap><span id="elCampo::icon" style="display:none;"><a name="_msgAnc_elCampo" title="Error" class="AFErrorIconStyle">X</a></span>&nbsp;<label for="elCampo">Your name</label></td>

                <td valign="top" nowrap class="AFContentCell"><!--Start: org.apache.myfaces.trinidad.Input["elCampo"]--><input id="elCampo" name="elCampo" class="af_inputText_content" size="30" type="text"></td>
              </tr><tr>
                <td></td>

                <td class="AFComponentMessageCell"><!--Start: org.apache.myfaces.trinidad.Input["elCampo"]--><span id="elCampo::msg" class="OraInlineErrorText"></span></td>

              </tr></table></td>

          <td><!--Start: org.apache.myfaces.trinidad.Object["j_id_jsp_1876237926_5"]--><img src="/ModuloTrinidadPilotoWeb/adf/images/t.gif" alt="" width="10" height="5"></td>

          <td><script type="text/javascript">var _pprUpDatemode=false;function _adfspu(f,v,e,s,o){_pprUpdateMode=true;if(!o)o=new Object();if(e)o.event=e;if(s)o.source=s;_submitPartialChange(f,v,o);}</script><!--Start: org.apache.myfaces.trinidad.Command["sayHello"]-->
            <button id="sayHello" name="sayHello" type="button" onclick="TrPage._autoSubmit('j_id_jsp_1876237926_3','sayHello',event,1);return false;" class="af_commandButton">Say Hello</button>
          </td>
        </tr></table><!--Start: org.apache.myfaces.trinidad.Object["j_id_jsp_1876237926_8"]--><div style="margin-top:15px"></div><!--Start: org.apache.myfaces.trinidad.Output["j_id_jsp_1876237926_9"]--><span id="j_id_jsp_1876237926_9" style="font-weight: bold;"></span><input type="hidden" name="org.apache.myfaces.trinidad.faces.FORM" value="j_id_jsp_1876237926_3"><!--Start: org.apache.myfaces.trinidad.Form--><span id="tr_j_id_jsp_1876237926_3_Postscript"><input type="hidden" name="javax.faces.ViewState" value="!-4fd3ee50"><script type="text/javascript">function _j_id_jsp_1876237926_3Validator(f,s){return _validateInline(f,s);}var j_id_jsp_1876237926_3_SF={};</script></span><script type="text/javascript">_submitFormCheck();</script></form>
  </body>

  <!--Created by Apache Trinidad (Apache MyFaces Trinidad API - 1.2.10/Apache MyFaces Trinidad Impl - 1.2.10), skin:bigfont.desktop (bigfont)--></html>

命令按钮 bean 代码:

public class CommandButtonBean {
    public String name;
    public String message;

    public String sayHello(){
        message = "Hola " + name;
        return "";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMessage() {
        return message;
    }

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

}

当然,命令按钮是在 faces-config.xml 中配置的(我认为这个片段是不相关的)。

到目前为止尝试的修复:

  • 我们检查了不同级别(应用程序、JVM 等)的字符集编码。它似乎在不同层之间对齐。
  • 此外,我们在 web.xml 中包含了下一个上下文参数:

    <context-param>
        <param-name>PARAMETER_ENCODING</param-name> 
        <param-value>UTF-8</param-value> 
    </context-param>
    
  • 我们已经记录了作为参数传递给该 setName方法的值是什么,因为它可能是在写入响应时出错。传递的值已经是错误的。

  • 我们尝试编写一个网络过滤器,将请求字符集设置为 UTF-8(以防万一,你知道的)。但是在过滤器中记录参数值时,我们也得到了错误的值。

  • 最后我们去掉了 partialSubmit提交按钮的属性,得到了正确的值。但这并不能真正解决我们的问题,因为我们正在尝试实现一些内联搜索和验证(我们不想重新绘制整个页面)。

因此,当我们发送一个普通的 POST 消息时,我们会得到预期的结果,但是当我们尝试使用 Ajax 方法时,我们会丢失这些特殊字符。

最后,问题:

您对如何解决此问题有任何建议吗?

非常感谢。

4

4 回答 4

2

这是 trinidad&WAS 的一个已知问题:http: //www.mail-archive.com/dev@myfaces.apache.org/msg46298.html。您必须对两个 js 文件进行小修改,您需要对发送的字符进行编码(Naveen Ravindra 的解决方案有效)。编辑这些文件:

  • trinidad-impl-XXX.jar\META-INF\adf\jsLibsDebug\xhr\XMLRequest.js
  • trinidad-impl-XXX.jar\META-INF\adf\jsLibs\xhr\XMLRequest.js

向它们添加这个新函数(将其粘贴到 js 的开头:

function encodeCharacters(string) {
        string = string.replace(/\r\n/g, "\n");
    var utftext = "";
    for ( var n = 0; n < string.length; n++) {
        var c = string.charCodeAt(n);
        if (c < 128) {
            utftext += String.fromCharCode(c);
        } else if ((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }   
        else {  
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }
    }
    return utftext;
}

TrXMLRequest.prototype.send在同一个js中更改函数。在其中改变

  • xmlhttp.send(content);xmlhttp.send(encodeCharacters(content));调试版本中,和
  • a4.send(a3);a4.send(encodeCharacters(a3));生产版本中。

经过测试,可在 WAS 7.0.0.11 中使用。

于 2011-02-28T22:53:44.330 回答
1

您好,我无法重现您的问题。实际上,如果我尝试像您那样对 jsp 页面进行编程,我将一事无成。这就是我的jsp的样子

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>  
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>  
<%@ taglib uri="http://myfaces.apache.org/trinidad" prefix="tr" %>  
<%@ taglib uri="http://myfaces.apache.org/trinidad/html" prefix="trh" %>  
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>  
<f:view>  
<tr:document>  
<trh:head title="Your Title">  
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
</trh:head>  
</tr:document>  
</f:view>

您不使用的 tr:document 创建了一个 html、body 和 head 标签。如果需要,可以使用 trh:head。请参阅特立尼达的文档: 文档 tr:document

使用这种配置,我对 ó 没有任何问题。我在 web.xml 中没有设置 PARAMETER_ENCODING 并且我正在使用 jsf1.2 trinidad 1.2.14 这不是一个真正的答案,但它可能会有所帮助。

问候

于 2011-02-25T13:56:32.707 回答
0

我有点忘记特立尼达了,但是为了让按钮影响输入文本,你不需要在 inputText 组件上设置一个“partialTriggers”属性,它应该有按钮的 ID 吗?

于 2011-02-25T12:50:43.170 回答
0

又是lkdg

我试图重现您的问题。

jsp:

<tr:inputText
  id="myfield"
   value="#{myBean.myname}">
</tr:inputText>
<tr:commandButton 
   id="sayHello" 
   text="Say Hello" 
   partialSubmit="true"                                
   action="#{myBean.sayHello}"/>
<tr:outputText 
   value="#{myBean.message}" 
   partialTriggers="sayHello"
   inlineStyle="font-weight: bold;"/> 

豆:

private String myname;
private String message; 

public String sayHello()
{         
    message = "Hola " + myname;         
    return "";     
} 
public String getMyname() {
    return myname;
}

public void setMyname(String myname) {
    this.myname = myname;
}

public String getMessage() {
    return message;
}

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

使用 ó 和 ễ 等一切正常。jsf-1.2;trinidad1.2.14;apache tomcat6.0.29

我不知道 IBM WebSphere AS 7.0。但可能你的问题与这个人在这里写的有关:Displaying UTF-8 Characters in resource response

于 2011-02-28T13:06:44.183 回答