页面中文乱码的处理

页面中文乱码的处理

1、页面本身的乱码

保证页面的charset和pageEncoding的编码一致,并且都支持中文(例如utf-8)。还要保证浏览器的编码与页面编码设置一致。

2. http请求的两种方式,post和get方式
a.post

post的请求,在获取请求信息之前通过设置request.setCharacterEncoding(“utf-8”)即可正常显示

b.get

get的请求,设置request.setCharacterEncoding(“utf-8”)不起作用,可以通过修改tomcat的server.xml文件的方式。在Connector节点添加useBodyEncodingForURI属性,即在Connector节点内添加

useBodyEncodingForURI="true"

即可,使用请求体的编码方式。

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/>
c.springMvc处理方式

修改web.xml,增加编码过滤器,如下(注意,需要设置forceEncoding参数值为true)

<filter>
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>
d.先解码,在编码
①.js编码字符串

三种方式:escape、encodeURI和encodeURIComponent

escape是对字符串(string)进行编码(而另外两种是对URL),作用是让它们在所有电脑上可读。
编码之后的效果是%XX或者%uXXXX这种形式。其中 ASCII字母、数字、@/+ ,这几个字符*不会被编码,其余的都会。最关键的是,当你需要对URL编码时,请忘记这个方法,这个方法是针对字符串使用的,不适用于URL。

encodeURIComponent比encodeURI编码的范围更大。

encodeURI方法不会对下列字符编码 ASCII字母、数字、~!@#$&*()=:/,;?+’

encodeURIComponent方法不会对下列字符编码 ASCII字母、数字、~!*()’

1、如果只是编码字符串,不和URL有半毛钱关系,那么用escape。

2、如果你需要编码整个URL,然后需要使用这个URL,那么用encodeURI。

encodeURI("http://www.cnblogs.com/season-huang/some other thing");编码后"http://www.cnblogs.com/season-huang/some%20other%20thing";其中,空格被编码成了%20。

3、当你需要编码URL中的参数的时候,那么encodeURIComponent是最好方法。

var param = "http://www.cnblogs.com/season-huang/"; //param为参数
param = encodeURIComponent(param);
var url = "http://www.cnblogs.com?next=" + param;
console.log(url) //"http://www.cnblogs.com?next=http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2F"
②servlet服务端解码
String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
3.springMvc返回的数据中文乱码
a. 在 @RequestMapping 里面加入 produces=”text/html;charset=UTF-8”
@ResponseBody
@RequestMapping(value="/logon",produces="text/html; charset=UTF-8")
public String logon(){
}
b.使用 Spring 的后置处理器 BeanPostProcessor

对这个类不太了解的,见:http://www.cnblogs.com/libra0920/p/6118157.html 有一个简单的说明。

在 bean 实例化之后,当 bean 的类型为 StringHttpMessageConverter 时,设置 @responseBody 返回数据编码为 utf8 格式。

/**
 * 实例化之后进行处理
 */
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  if(bean instanceof StringHttpMessageConverter){
    MediaType mediaType = new MediaType("text", "plain", Charset.forName("UTF-8"));
    List<MediaType> types = new ArrayList<MediaType>();
    types.add(mediaType);
    ((StringHttpMessageConverter) bean).setSupportedMediaTypes(types);
  }
  return bean;
}
c.使用配置文件
    <!-- <mvc:annotation-driven /> -->
    <mvc:annotation-driven>  
        <mvc:message-converters>  
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <property name="supportedMediaTypes" value="text/html;charset=utf-8"></property>  
            </bean>  
        </mvc:message-converters>  
    </mvc:annotation-driven>  
4.数据库乱码
a.设置url的编码
jdbcUrl =jdbc:mysql:///apple?useUnicode=true&characterEncoding=UTF-8
b.设置数据库的编码,使其支持中文
mysql

http://www.jianshu.com/p/4c6a27542df4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值