Flex HttpService中乱码的解决


文章分类:Flash编程

Flex与Strust2的交互中,乱码问题很是头痛,网上的解决方法也不系统,花了很长时间才解决~~~

 

Flex->Action

在flex前台将要传输的字符串编码:

UserRegisterDetailSave.request.address = encodeURIComponent(address.text);

 

address中的内容包含中文,就需要重新编码

 

后台需要增加一个编码的Filter:

Java代码
  1. package  com.filter;  
  2.   
  3. import  java.io.IOException;  
  4.   
  5. import  javax.servlet.Filter;  
  6. import  javax.servlet.FilterChain;  
  7. import  javax.servlet.FilterConfig;  
  8. import  javax.servlet.ServletException;  
  9. import  javax.servlet.ServletRequest;  
  10. import  javax.servlet.ServletResponse;  
  11.   
  12. public   class  EncodingFilter  implements  Filter{  
  13.   
  14.     public   void  destroy()  
  15.     {  
  16.           
  17.     }  
  18.       
  19.     public   void  doFilter(ServletRequest request, ServletResponse response,  
  20.         FilterChain filterChain) throws  IOException, ServletException  
  21.     {  
  22.         request.setCharacterEncoding("utf-8" );  
  23.         response.setCharacterEncoding("utf-8" );  
  24.           
  25.         filterChain.doFilter(request, response);  
  26.     }  
  27.   
  28.     public   void  init(FilterConfig arg0)  throws  ServletException {  
  29.         // TODO Auto-generated method stub   
  30.       
  31.     }  
  32. }  
package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter{

	public void destroy()
	{
		
	}
	
	public void doFilter(ServletRequest request, ServletResponse response,
		FilterChain filterChain) throws IOException, ServletException
	{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		filterChain.doFilter(request, response);
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
	
	}
}

 

 

这个Filter将请求编码为"utf-8"的形式,这样才能进行下一步解码.在Action中,需要用到值中包含中文的属性时,用 URLDecoder.decode(address, "utf-8")进行解码,解码前后address都是String类型.如果还是不行的话,检查一下前台界面是不是 有:initialize="flash.system.System.useCodePage=true"的标记,有的话改成false.

 

Action->Flex

这个很方便,添加两行代码至Action末端:

HttpServletResponse response = ServletActionContext.getResponse();
  response.setCharacterEncoding("utf-8");

 

最后就是通过HttpService获取远程Xml了,这个更方便了,将页面Application标签里添加:

initialize="flash.system.System.useCodePage=true"

 

上面的方法全是项目中自己遇到然后解决了的,如果没有用的话那只能深感无奈了~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值