springmvc ajax 乱码

输入信息乱码
@RequestParam传值中文乱码
http://luanxiyuan.iteye.com/blog/1849169
使用代码手动转换
try {
			return new String(str.getBytes(fromCharset), toCharset);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}








返回信息乱码
方法一:
http://blog.csdn.net/kissliux/article/details/14053761
采用了一个比较简单的方法来解决这个问题,就是需要服务器返回中文的时候不使用这个注解,而是直接用HttpServletResponse的对象来完成传输,在服务器端可以通过response.setContentType("text/plain;charset=UTF-8");来设定编码类型,这样就不会出现中文乱码了。
服务器端核心代码如下:
@RequestMapping(value = "test", method = RequestMethod.POST)  
    public void test(HttpServletRequest request,  
            HttpServletResponse response) {  
        String result = null;  
        //取得客户端传来的值  
        String userName = request.getParameter("userName");  
        //向客户端返回一句话  
        result = "您好!";  
  
        PrintWriter out = null;  
        response.setContentType("text/plain;charset=UTF-8");  
        try {  
            out = response.getWriter();  
            out.write(result.toString());  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            out.close();  
        }  
    }  

返回值时根据自己的数据类型进行设置,常用的有:
response.setContentType("text/html; charset=utf-8");           html
response.setContentType("text/plain; charset=utf-8");          文本
response.setContentType("application/json; charset=utf-8");    数据
response.setContentType("application/xml; charset=utf-8");      xml


方法二:@ResponseBody乱码
http://blog.csdn.net/kissliux/article/details/14053761
spring-servlet 配置
<mvc:annotation-driven>   
    <mvc:message-converters register-defaults="true">   
        <bean class="com.abc.spring.UTF8StringHttpMessageConverter"/>   
    </mvc:message-converters>   
</mvc:annotation-driven> 


controller中
@RequestMapping(value = "/getWeather",method = {RequestMethod.POST,RequestMethod.GET},produces="text/plain;charset=UTF-8")  
   @ResponseBody  



转换类:
public class UTF8StringHttpMessageConverter extends   
        AbstractHttpMessageConverter<String> {   
   
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");   
    private final List<Charset> availableCharsets;   
   
    public UTF8StringHttpMessageConverter() {   
        this(DEFAULT_CHARSET);   
    }   
   
    public UTF8StringHttpMessageConverter(Charset defaultCharset) {   
        super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);   
        this.availableCharsets = new ArrayList<Charset>(Charset   
                .availableCharsets().values());   
    }   
   
    @Override   
    protected boolean supports(Class<?> clazz) {   
        return String.class.equals(clazz);   
    }   
   
    @Override   
    protected String readInternal(Class<? extends String> clazz,   
            HttpInputMessage inputMessage) throws IOException,   
            HttpMessageNotReadableException {   
        MediaType contentType = inputMessage.getHeaders().getContentType();   
        Charset charset = contentType.getCharSet() != null ? contentType   
                .getCharSet() : DEFAULT_CHARSET;   
        return FileCopyUtils.copyToString(new InputStreamReader(inputMessage   
                .getBody(), charset));   
    }   
   
    @Override   
    protected void writeInternal(String t, HttpOutputMessage outputMessage)   
            throws IOException, HttpMessageNotWritableException {   
        MediaType contentType = outputMessage.getHeaders().getContentType();   
        Charset charset = contentType.getCharSet() != null ? contentType   
                .getCharSet() : DEFAULT_CHARSET;   
        FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(),   
                charset));   
    }   
   
    protected List<Charset> getAcceptedCharsets() {   
        return this.availableCharsets;   
    }   
       
    @Override   
    protected Long getContentLength(String s, MediaType contentType) {   
        if (contentType != null && contentType.getCharSet() != null) {   
            Charset charset = contentType.getCharSet();   
            try {   
                return (long) s.getBytes(charset.name()).length;   
            } catch (UnsupportedEncodingException ex) {                   
                throw new InternalError(ex.getMessage());   
            }   
        } else {   
            return null;   
        }   
    }   
}   

猜你喜欢

转载自panyongzheng.iteye.com/blog/2200154