javaee SpringMVC 乱码问题解决

方法一

在web.xml文件中注册过滤器


  <filter>
    <filter-name>CharacterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
    <init-param>
      <param-name>forceResponseEncodingparam-name>
      <param-value>trueparam-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

方法二

在注解中添加编码设置,主要是处理ajax请求乱码
produces = “text/html;charset=utf-8”

 @RequestMapping(value="/getMajorList2",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String getMajorList2()
    {

        List<Major> majorList=new ArrayList<Major>();

        majorList.add(new Major(1,"计算机"));

        majorList.add(new Major(2,"英语"));

        return JSON.toJSONString(majorList);
    }

方法三

修改tomcat的sever.xml文件
添加 URIEncoding=“utf-8”
主要解决post请求乱码

 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="utf-8" />

你可能感兴趣的:(Mac开发,java-ee,okhttp,java)