Myfaces Trinidad JSF:发送非US-ASCII字符时的Ajax问题

Myfaces Trinidad JSF:发送非US-ASCII字符时的Ajax问题,jsf,character-encoding,websphere,myfaces,trinidad,Jsf,Character Encoding,Websphere,Myfaces,Trinidad,问题: 我们有一个基于ApacheMyFaces特立尼达的Java web应用程序。当一个字段包含一些特殊字符时,我们在尝试部分提交表单(即:通过Ajax)时会遇到一些问题,例如,ñ、á等 当我们在文本字段中写入一个类似于camión的值时,生成的消息是Hola cami(它会删除特殊字符和下一个字符) 我们运行代码的应用服务器是IBM WebSphere AS 7.0 在IExplorer和Firefox浏览器上观察到了这种行为,它们都是从WindowsXPProfessional(西班牙语版

问题:

我们有一个基于ApacheMyFaces特立尼达的Java web应用程序。当一个字段包含一些特殊字符时,我们在尝试部分提交表单(即:通过Ajax)时会遇到一些问题,例如,ñ、á等

当我们在文本字段中写入一个类似于
camión
的值时,生成的消息是
Hola cami
(它会删除特殊字符和下一个字符)

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

在IExplorer和Firefox浏览器上观察到了这种行为,它们都是从WindowsXPProfessional(西班牙语版本)运行的

主要代码段

更新:根据
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>
当然,命令按钮是在faces-config.xml中配置的(我认为这段代码不相关)

迄今为止已尝试修复:

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

    <context-param>
        <param-name>PARAMETER_ENCODING</param-name> 
        <param-value>UTF-8</param-value> 
    </context-param>
    
    
    参数编码
    UTF-8
    
  • 我们已经记录了值是多少 作为参数传递给
    setName
    方法,如果可以 写入响应时出错。这个 传递的值已经错误

  • 我们试着写一个网页过滤器, 将请求字符集设置为UTF-8(仅在 凯斯,你知道的)。但是当记录 过滤器上的参数值,我们 也得到了错误的值

  • 最后,我们拆除了这座桥
    partialSubmit
    的属性 提交按钮,并获得正确的 价值但这并不能真正解决我们的问题 问题,正如我们正在努力解决的那样 实现一些内联搜索和 验证(我们不想重新绘制 整页)

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

最后是一个问题:

你对如何解决这个问题有什么建议吗


非常感谢。

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

嗨,我无法重现您的问题。实际上,如果我尝试像您那样编程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。 见特立尼达的文件:

在这种配置下,我对anó没有任何问题。 我在web.xml中没有参数_编码集,我使用的是jsf1.2特立尼达1.2.14 这不是一个真正的答案,但可能会有所帮助

关于

又是lkdg了

我试图复制你的问题

jsp:

一切都很好,而且ễ 不管怎样。 jsf-1.2;特立尼达1.2.14;ApacheTomcat6.0.29


我不知道IBM WebSphere AS 7.0。但你的问题可能与这家伙在这里写的东西有关:

这是特立尼达岛的一个已知问题&WAS:。您将不得不对两个js文件进行一个小的修改,您需要对发送的字符进行编码(Naveen Ravindra的解决方案是有效的)。编辑这些文件:

  • 特立尼达岛impl XXX.jar\META-INF\adf\jsLibsDebug\xhr\XMLRequest.js
  • 特立尼达岛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;
}
函数编码字符(字符串){
string=string.replace(/\r\n/g,“\n”);
var utftext=“”;
对于(var n=0;n127)和((c<2048)){
utftext+=String.fromCharCode((c>>6)| 192);
utftext+=String.fromCharCode((c&63)| 128);
}   
否则{
utftext+=String.fromCharCode((c>>12)| 224);
utftext+=String.fromCharCode((c>>6)和63)| 128);
utftext+=String.fromCharCode((c&63)| 128);
}
}
返回utftext;
}
在同一个js中更改
TrXMLRequest.prototype.send
函数。在其中更改

  • 调试版本中的
    xmlhttp.send(内容);
    xmlhttp.send(encodeCharacters(内容));
    ,以及
  • a4.send(a3);
    到生产版本中的
    a4.send(encodeCharacters(a3));

经过测试,适用于WAS 7.0.0.11。

您能否发布生成的HTML页面的一小部分,至少是元数据和页眉部分。检查真正输出到浏览器的内容总是很好的。另外,您使用的是什么应用服务器和浏览器。发送非Unicode字符时“…是什么意思?”?UTF-8中的“U”代表Unicode,而在Unicode中存在着洎,á。你不应该将你的问题重新表述为“…发送Unicode字符时”?@Gugusse:我指的是US-ASCII字符。我已经修复了以避免混淆。谢谢你的观点。不,据我所知。代码工作(有点),设置名称值,并在tr:outputText处显示消息(这里是我设置partialTriggers属性的地方)。只有显示的消息与非ASCII字符有问题。是的,您是对的。如果在后端设置输入字段的值,会发生什么情况。。这是否正确地显示在(部分)之后的inputfield中重新加载?谢谢你的回答(+1)。试过了,但我对ó、á等的行为仍然是错误的。
<%@ 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: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;
}
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;
}