天天看点

JSON页面响应数据乱码问题

页面响应数据乱码问题

首先查看web.xml是否配置乱码过滤

<filter>
    <filter-name>encodingFilter</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>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

           

然后发现还是乱码,数据还都是????

页面的头文件也有设置编码,但还是乱码

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
           

仔细一想,页面里面的标题和文字都没有乱码,就是从后台传过来的数据有问题,应该去后端找问题,百度发现

给注解@RequestMapping加上"produces="text/html;charset=UTF-8",解决乱码

@RequestMapping(value = "/all",produces="text/html;charset=UTF-8")
    @ResponseBody
    public String queryAll(Model model){
        List<Book> list = bookService.queyAllBook();
        int count = bookService.countBook();
        model.addAttribute("list",list);

        Map map = new HashMap();
        map.put("code",0);
        map.put("msg"," ");
        map.put("count",count);
        map.put("data",list);
        System.out.println(map);
        return  JSONObject.toJSONString(map);

    }
           

来自:https://www.cnblogs.com/19322li/p/12835741.html