天天看點

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