解决post请求乱码问题
1、请求:
<form action="param/test1" method="post"> userId:<input type="text" name="userId"><br> userName:<input type="text" name="userName"><br> <input type="submit" value="提交"> </form>
2、实体类:
package com.ly.springmvc.domain; import java.io.Serializable; public class User implements Serializable { private Integer userId; private String userName; public void setUserId(Integer userId) { this.userId = userId; } public void setUserName(String userName) { this.userName = userName; } @Override public String toString() { return "User{" + "userId=" + userId + ", userName='" + userName + '\'' + '}'; } }
3、处理方法:
@RequestMapping("/param/test1") public String testParam4(User u) { System.out.println("testParam4"); System.out.println(u); return "success"; }
4、总结:
4.1、表单若为GET请求方式时中文不会乱码
4.2、表单若为POST请求方式时中文会乱码,解决中文乱码的方式:在web.xml中配置CharacterEncodingFilter过滤器
<!--解决POST请求中文乱码--> <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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
相识是缘