@RequestParam乱码问题

https://www.cnblogs.com/SaraMoring/p/5594980.html

在网上找了很多资料才找到解决的方法,通过URL传递命名参数,后台接收的却是乱码解决方法如下:

方法一:将接收的参数重新编码

1

2

3

4

5

6

@RequestMapping(value="/handle")

public String handle81(@RequestParam("userName") String userName){         

    userName=new String(userName.getBytes("ISO-8859-1"), "UTF-8")

    modelMap.put("userName", userName);

    return "/user/showUser";

}

方法二:在web.xml里面配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

    <!-- 字符过滤器 --> 

<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>

方法三:修改tomcat的server.xml(在conf文件下),找到下面代码添加URIEncoding编码。 

1

2

3

<Connector port="8080" protocol="HTTP/1.1"

           connectionTimeout="20000"

           redirectPort="8443" URIEncoding="UTF-8"/>

 最终我的解决方法时方法二和方法三同时用,没用方法一才解决@RequestParam乱码问题。 

 

另外表单提交时出现乱码解决方法如下:

一、首先将Eclipse文件编码是UTF-8:Windows——Preference——General——Workspace——Text file encoding改为UTF-8.

二、设置页面编码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
    contentType="text/html; charset=utf-8"%>

<%
    request.setCharacterEncoding("utf-8");
%>

三、将form表单提交方式变为post方式,即添加method="post"

四、在action类中,添加3句话

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");

五、将接收到的数据转码

String  name = new String(request.getParameter(“name”).getBytes("ISO-8859-1"),"utf-8");

 六、post提交方式下可进行URL编码:

String info=java.net.URLEncoder.encode("你好吗.jpg","utf-8");

http://localhost:8080/webTest/index.jsp?name=<%=URLEncoder.encode("张三","UTF-8")%>

String myname=URLDecoder.decode(name,"UTF-8");
 

参考文献:

解决JSP中文乱码问题:http://www.cnblogs.com/chengkai/articles/2171848.html

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用@RequestParam注解接收前台请求参数时,可以通过设置编码来解决中文乱码问题。常用的设置编码的方式有两种。 第一种方式是在@RequestParam注解中使用value属性设置编码方式,例如:@RequestParam(value = "username", encoding = "UTF-8")。这样可以指定编码为UTF-8,确保正确处理中文字符。 第二种方式是在Controller类或方法上使用@RequestMapping注解,并设置produces属性来指定响应的媒体类型和编码方式。例如:@RequestMapping(value = "/user", produces = "application/json;charset=UTF-8")。这样可以确保接收和响应的数据都使用UTF-8编码。 需要注意的是,@RequestParam注解本身并没有设置编码的属性,它只是用来绑定请求参数的注解。编码的设置应该放在@RequestParam注解所在的方法或类上。 综上所述,可以通过在@RequestParam注解中设置编码方式或在Controller类或方法上设置编码来解决中文乱码问题。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [关于使用@RequestParam接受请求参数中文乱码解决](https://blog.csdn.net/weixin_46609992/article/details/114460102)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [springMVC的 RequestBody和 ResponseBody和RequestParam.docx](https://download.csdn.net/download/weixin_44609920/85693875)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值