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

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

@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里面配置

        <!-- 字符过滤器 -->
<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编码。 

    <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

  

Spring @RequestParam乱码问题的更多相关文章

  1. 170117、spring解决乱码

    spring解决乱码这个问题网上有很多解决方法,也可以关注本博客的文章,在此不再赘述, 今天推荐大家另外两种解决方法! 问题现象: 1.后台代码 2.前端界面 解决方法: 方法1:是在后台请求方法上加 ...

  2. Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    背景 昨天一个人瞎倒腾spring boot,然后遇到了一点问题,所以把这个问题总结一下. 主要讲解request 数据到handler method 参数数据的绑定,所用到的注解和什么情形下使用. ...

  3. spring中文乱码问题

    第一:code @RequestMapping(value = "/query/{keyword}", method = RequestMethod.GET, produces = ...

  4. spring中文乱码过滤器

    中文乱码过滤器 在您通过表单向服务器提交数据时,一个经典的问题就是中文乱码问题.虽然我们所有的 JSP 文件和页面编码格式都采用 UTF-8,但这个问题还是会出现.解决的办法很简单,我们只需要在 we ...

  5. Customize Spring @RequestParam Deserialization for Maps and/or Nested Objects

    @RestController class MyController { @RequestMapping(...) public void test(Container container) { .. ...

  6. spring MVC 乱码问题

    (转) spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,Cha ...

  7. Spring @RequestParam、@RequestBody和@ModelAttribute区别

    一.@RequestParamGET和POST请求传的参数会自动转换赋值到@RequestParam 所注解的变量上1. @RequestParam(org.springframework.web.b ...

  8. spring解决乱码

    spring提供的工具类解决乱码问题 在web.xml配置中添加如下代码: <!--乱码处理--> <filter> <filter-name>encodingFi ...

  9. spring 中文乱码问题

    spring 开发过程中的中文乱码问题主要分为以下几种: 1.前端传参数到后台前  就已经乱码. 这个很大原因就是前端的问题了! 2.传入后台后,乱码. 可能存在几个原因: 2.1 传入tomcat前 ...

随机推荐

  1. python编码错误

    初学python,遇到的最难忘的坑没有之一.这个问题起码困扰了我一周.在我写了一段代码之后经常遇见这样的报错. 本质原因是我用的python2,在编码流派中python2是比较奇葩的一派,不随大流.所 ...

  2. JPA 中文乱码问题

    背景 Spring Boot使用JPA,当前端传入到后端的数据为中文的时候,入库变为??? 解决方法 修改下项目的配置文件中DB的配置,新增characterEncoding=utf-8即可 spri ...

  3. 【Js应用实例】jQuery监听回车键

    $(function(){ //焦点放在第一个文本输入框 $('input:text:first').focus(); //获取所有的输入框 var $inp=$('input'); //定义所有输入 ...

  4. Brackets 1.8 开源+免费的Web前端网页文本编辑工具

    Brackets 1.8 开源+免费的Web网页文本编辑工具   -------------->> ---------------------- A modern, open source ...

  5. 总结切面编程AOP的注解式开发和XML式开发

    有段日子没有总结东西了,因为最近确实有点忙,一直在忙于hadoop集群的搭建,磕磕碰碰现在勉强算是能呼吸了,因为这都是在自己的PC上,资源确实有点紧张(搭建过程后期奉上),今天难得大家都有空(哈哈哈~ ...

  6. Java开发相关命名规范

    JAVA文件命名规范 1.类命名 抽象类以 Abstract 或者 Base 开头.异常类以 Exception 作为后缀.枚举类以 Enum 作为后缀.工具类以 Utils 作为后缀(相应的包名最后 ...

  7. Linux Command Line learning

    https://www.codecademy.com/en/courses/learn-the-command-line Background The command line is a text i ...

  8. 使用Groovy处理SoapUI中Json response

    最近工作中,处理最多的就是xml和json类型response,在SoapUI中request里面直接添加assertion处理json response的话,可以采用以下方式: import gro ...

  9. TOMCAT闪退。cmd执行startup.bat保错:the CATALINA_HOME environment variable is not defined correctly

    从上图可以看出 是我们没有设置CATALINA_HOME变量 于是我设置了这个变量之后 ,再次重启,ok了

  10. c# typeof 与 GetType 作用与区别

    官方解释: Used to obtain the "System.Type" object for a type. A 'typeof' expression takes the ...