springmvc参数传递的几种乱码情况

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qqxyy99/article/details/78644355

问题:从jsp页面传递中文参数到后台过程中,出现乱码情况

原因:Request默认编码是iso-8859-1,而前台设置的是utf-8编码,导致获取值输出发现是乱码

解决方案:

1.先保证所有的页面编码都是utf-8,包括jsp页面,浏览器编码设置和eclipse的编码设置。

2.spingmvc给我们提供了一个编码过滤器,只需要在配置文件web.xml中加入即可。如下:

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

3.以上两步有时只能解决post方式传递参数乱码问题,get方式还是出现乱码,则就需要该tomcat的配置文件了,打开tomcat的server.xml文件,找到以下行

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
在上面行中插入URIEncoding="UTF-8",改成如下形式:

<Connector URIEncoding="UTF-8"  connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
4.以上几步都检测无误后,传到后台依然是乱码问题,则按以下方案在Request中获取后转换编码。

前台jsp页面(客户端)传参:

     需要使用js的encodeURIComponet()函数,该函数可把字符串作为 URI 组件进行编码,会将URL中的中文转换为UTF-8格式的字符串,以%XY格式呈现。

     在使用时,使用了两次,进行了两次编码。第一次编码后将汉字编码为%和字母数字的格式,而第二次编码的时候是对%字母数字进行编码。因为第一次encodeURIComponent的时呈现了"%",这个符号在解析参数的时是无法解析的,必须把"%"也进行编码,第二次对"%"编码后就是"%25"。

例:

H766-5807-04-A01(7700-10035 24位 无软垫).dwg


第一次编码将中文转换为UTF-8格式:

H766-5807-04-A01%EF%BC%887700-10035%2024%E4%BD%8D%20%E6%97%A0%E8%BD%AF%E5%9E%AB%EF%BC%89.dwg

第二次编码将%符号转换为%25

H766-5807-04-A01%25EF%25BC%25887700-10035%252024%25E4%25BD%258D%2520%25E6%2597%25A0%25E8%25BD%25AF%25E5%259E%25AB%25EF%25BC%2589.dwg


后台Controller(服务端)接收参数:

     由于Request默认编码是iso-8859-1,或者设置了其他的编码格式比如是GB2312等,这样容器是没有按 UTF-8 解码,都会出现传过来的参数为乱码的情况。

     所以在后台调用request.getParameter时,也要进行两次解码。执行URLDecoder.decode将URL中的%25转换为%,然后再将其转换为汉字。

     虽然解码的时候使用的是ISO-8859-1,但是对于%和字母数字而言用ISO-8859-1和UTF-8解码出来的是一样的,此时就回到了汉字被编码过一次的字符串了,当再次进行解

码的时候使用UTF-8就回将它转会汉字;


还拿上边的示例解码:

例:

第一次解码将%25转换为%

H766-5807-04-A01%EF%BC%887700-10035%2024%E4%BD%8D%20%E6%97%A0%E8%BD%AF%E5%9E%AB%EF%BC%89.dwg

第二次解码转换为正常汉字:
H766-5807-04-A01(7700-10035 24位 无软垫).dwg


项目中应用,传入参数search,编码并解码。

两次编码:

var data = {search:encodeURIComponent(encodeURIComponent($('#search').val()))}
两次解码:
@RequestMapping("/notice-news-list")
	@ResponseBody
	public ResultCode<ReturnTable<NoticeAndNewsmethodModel>> noticeList(HttpServletRequest request,	
			@RequestParam(value = "type", required = false) Integer type,
			@RequestParam(value="page_index", required=false)Integer pageIndex) throws UnsupportedEncodingException{	
		int pageCount = 3;
		String search = new String(request.getParameter("search").getBytes("iso8859-1"),"utf-8");
		search=java.net.URLDecoder.decode(search, "UTF-8");
		search=java.net.URLDecoder.decode(search, "UTF-8");
      
		Paging page=new Paging(pageIndex, pageCount);
		List<NoticeAndNewsmethodModel> noticelist = informationService.queryNoticeOrNewMethod(page,search,type);
		int count = informationService.selectNoticeOrNewMethodCount(search,type);
		ReturnTable<NoticeAndNewsmethodModel> table = new ReturnTable<NoticeAndNewsmethodModel>(pageCount, count, noticelist);
		return new ResultCode<ReturnTable<NoticeAndNewsmethodModel>>(table);
	}

参考文章:

1.有关前台传后台参数乱码的处理

2.JS encodeURIComponent 编码两次的场景及原因分析

猜你喜欢

转载自blog.csdn.net/qqxyy99/article/details/78644355