ajaxAnywher中文乱码问题

 

        对于中文转换问题,我们一般的做法是指定页面统一用一种编码格式,如UTF-8或GB2312.这样一般的请求都不会出现乱码,

       在Struts中要记得在FORMBEAN的validate方法中进行字符转换,转成与页面一致的编码格式,如

     public  ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) 
{
 
        
// TODO Auto-generated method stub
        
        ActionErrors errors 
= new ActionErrors();
        
if(receiverId==null){
            errors.add(
"message",new ActionMessage(Constants.MSG_MAIL_SEND_FAILED));
        }

        
if(senderId == null){
            errors.add(
"message",new ActionMessage(Constants.MSG_MAIL_SEND_FAILED));
        }

        
     
        
        
try {
            
if(title==null || title.equals("")) {
 
                errors.add(
"message",new ActionMessage(Constants.MSG_MAIL_SEND_FAILED));
            }
else{
            title 
= new String(title.getBytes("ISO-8859-1"),"GB2312");
            }

            
            
if(content!=null ){
                
if(content.length()>2000){
                    errors.add(
"message",new ActionMessage(Constants.MSG_MAIL_SEND_FAILED));
                }

                
else
             
            content 
= new String(content.getBytes("ISO-8859-1"),"GB2312");
            }
 
        }
 catch (UnsupportedEncodingException e) {
            
// TODO Auto-generated catch block
            errors.add("message",new ActionMessage(Constants.MSG_CONVERT_ERROR));
            e.printStackTrace();
        }

        
if(reply!=null){
            
try {
                reply 
= new String(reply.getBytes("ISO-8859-1"),"GB2312");
            }
 catch (UnsupportedEncodingException e) {
                
// TODO Auto-generated catch block
                errors.add("message",new ActionMessage(Constants.MSG_CONVERT_ERROR));
            }

        }

        
return errors;
    }

 

         但是同样的做法,如果页面用ajaxAnywhere请求到后台,这样转换仍然要出乱码,原来是ajaxAnywhere的AAFilter.class中自动将请求字符转换成UTF-8---

   public   void  doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        
throws  IOException, ServletException
    
{
        HttpServletRequest request 
= (HttpServletRequest)servletRequest;
        HttpServletResponse response 
= (HttpServletResponse)servletResponse;
        
if(!AAUtils.isAjaxRequest(servletRequest))
        
{
            filterChain.doFilter(servletRequest, response);
            
return;
        }

        request.setCharacterEncoding(
"UTF-8");
        response.setContentType(
"text/xml;charset=utf-8");
        response.setHeader(
"Cache-Control""no-cache");
        response.setDateHeader(
"Expires"0L);
        response.setHeader(
"Pragma""no-cache");
        BufferResponseWrapper bufferResponseWrapper 
= new BufferResponseWrapper(response);
        java.util.Set refreshZones 
= new HashSet();
        AAUtils.setZonesToRefresh(request, refreshZones);
        AAUtils.getRefreshZonesFromURL(request);
        
try
        
{
            filterChain.doFilter(request, bufferResponseWrapper);
            bufferResponseWrapper 
= execHandler(request, bufferResponseWrapper);
            
if(bufferResponseWrapper.getRedirect() != null)
                XMLHandler.sendRedirect(bufferResponseWrapper);
            
else
                XMLHandler.sendZones(bufferResponseWrapper, refreshZones);
        }

        
catch(Throwable e)
        
{
            XMLHandler.handleError(response, e);
        }

    }

 

所以如果你正在使用GB2312的话,就要小心了....要自己再进行一次字符转换,  用formbean 的话,最好将一般请求用的formbean 与  ajaxAnywhere请求用的formbean 分开 , 然后分别写validate方法,分别进行字符转化.~~~

      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AjaxAnywhere的类库及其用法 AjaxAnywhere使用一个名为aa.js的Javascript文件来处理客户端的全部Ajax操作,包括初始化XMLHttpRequest、获取表单内容、发送Ajax请求、执行回调函数等。aa.js也是使用AjaxAnywhere之前必须了解的,至少应该知道其经常用到的API。Ajax Anywhere的官方网站提供了相应的Javascript Document,方便快速查找和了解这些API。 1.AjaxAnywhere的初始化 aa.js中定义了一个AjaxAnywhere对象,针对Ajax的各种操作被抽象成AjaxAnywhere对象的方法,通过这些对象方法完成所需的操作。必要的时候,可以重载这些方法,以便满足个性化的需求。在aa.js文件的末端,AjaxAnywhere对象使用默认的构造方法完成对象实例化。 ajaxAnywhere = new AjaxAnywhere(); ajaxAnywhere.bindById(); 所以,所有引用aa.js的页面都可以在Javascript代码段中使用AjaxAnywhere对象的实例ajaxAnywhere。 当AjaxAnywhere初始化的时候,它在默认的构造函数中完成XMLHttpRequest对象的创建,并保存在AjaxAnywhere对象属性req中。AjaxAnywhere对象默认的构造方法如例程11-23所示。 例程11-23 AjaxAnywhere对象的默认构造方法 function AjaxAnywhere() { this.id = AjaxAnywhere.defaultInstanceName;//id,用于生成更新区域的编号等用途 this.formName = null;//页面表单名称 this.notSupported = false;//是否支持Ajax this.delayBeforeContentUpdate = true;//在更新页面内容之前是否延迟 this.delayInMillis = 100;//延迟时间 //初始化XMLHttpRequest对象--req if (window.XMLHttpRequest) { this.req = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { this.req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { this.req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e1) { this.notSupported = true; /* XMLHTTPRequest not supported */ } } } //确定浏览器是否支持Ajax if (this.req == null || typeof this.req == "undefined") this.notSupported = true; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值