SpringMVC——数据乱码问题

乱码解决:
 1.controller传递数据给页面 :在RequestMapping当中指定produces="text/json;charset=utf-8"
 2.Controller接收页面数据 : 使用编码过滤器

一、传递数据:

 @RequestMapping(value = "/secondRequest*",produces="text/json;charset=utf-8",method = RequestMethod.GET)//这里method用来指定请求方式
    @ResponseBody
    public String secondRequest(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException {

        model.addAttribute("user","李四");
        return "姓名:李四,年龄:16";
    }

二、接收数据

  找到web.xml进行文件配置

<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>
    <!--强制使用UTF-8编码-->
    <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>

 

posted @ 2019-11-10 22:23  萧然成长记  阅读(362)  评论(0编辑  收藏  举报