SpringMVC的中文乱码问题

it2023-01-13  49

尝试解决:

方法1,添加request.setCharacterEncoding(“UTF-8”)【无效】

@PostMapping("/encodingController/test1") public String test1(String name, Model model, HttpServletRequest request) throws UnsupportedEncodingException { request.setCharacterEncoding("UTF-8"); System.out.println(name); model.addAttribute("name",name); return "firstJsp"; }

可能是后台显示到前台出现问题,解决:

<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg ref="utf8Charset" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean id="utf8Charset" class="java.nio.charset.Charset" factory-method="forName"> <constructor-arg value="UTF-8" /> </bean>

方法2,通过过滤器解决【有效】

a. 添加Filter类

package com.kliurat.filter; import javax.servlet.*; import java.io.IOException; public class EncodingFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("UTF-8"); servletResponse.setCharacterEncoding("UTF-8"); filterChain.doFilter(servletRequest,servletResponse); } public void destroy() { } }

b. 在web.xml中配置filter过滤器【注意是/*,而不是/】

<!--配置处理乱码的过滤器--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>com.kliurat.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

方法3:将表单提交的方法改为GET。【有效】

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>表单</title> </head> <body> <form action="/springmvc_00_testmethod/encodingController/test1" method="get"> <input type="text" name="name"> <input type="submit"> </form> </body> </html> @GetMapping("/encodingController/test1") public String test1(String name, Model model){ System.out.println(name); model.addAttribute("name",name); return "firstJsp"; }

方法4:SpringMVC给我们提供了一个过滤器,可以在web.xml中配置==【有效】==

<!--配置SpringMVC提供的解决乱码的过滤器--> <filter> <filter-name>encoding</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>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

极端处理:修改Tomcat==【麻烦,有效】==

修改Tomcat配置文件,设置编码【Tomcat目录下的config目录下的server.xml】

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>

自定义过滤器

package com.kuang.filter; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Map; /** * 解决get和post请求 全部乱码的过滤器 */ public class GenericEncodingFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //处理response的字符编码 HttpServletResponse myResponse=(HttpServletResponse) response; myResponse.setContentType("text/html;charset=UTF-8"); // 转型为与协议相关对象 HttpServletRequest httpServletRequest = (HttpServletRequest) request; // 对request包装增强 HttpServletRequest myrequest = new MyRequest(httpServletRequest); chain.doFilter(myrequest, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } } //自定义request对象,HttpServletRequest的包装类 class MyRequest extends HttpServletRequestWrapper { private HttpServletRequest request; //是否编码的标记 private boolean hasEncode; //定义一个可以传入HttpServletRequest对象的构造函数,以便对其进行装饰 public MyRequest(HttpServletRequest request) { super(request);// super必须写 this.request = request; } // 对需要增强方法 进行覆盖 @Override public Map getParameterMap() { // 先获得请求方式 String method = request.getMethod(); if (method.equalsIgnoreCase("post")) { // post请求 try { // 处理post乱码 request.setCharacterEncoding("utf-8"); return request.getParameterMap(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else if (method.equalsIgnoreCase("get")) { // get请求 Map<String, String[]> parameterMap = request.getParameterMap(); if (!hasEncode) { // 确保get手动编码逻辑只运行一次 for (String parameterName : parameterMap.keySet()) { String[] values = parameterMap.get(parameterName); if (values != null) { for (int i = 0; i < values.length; i++) { try { // 处理get乱码 values[i] = new String(values[i] .getBytes("ISO-8859-1"), "utf- 8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } } hasEncode = true; } return parameterMap; } return super.getParameterMap(); } //取一个值 @Override public String getParameter(String name) { Map<String, String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); if (values == null) { return null; } return values[0]; // 取回参数的第一个值 } //取所有值 @Override public String[] getParameterValues(String name) { Map<String, String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); return values; } }

修改web.xml文件

<filter> <filter-name>genericEncodingFilter</filter-name> <filter-class>com.kliurat.filter.GenericEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>genericEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping>

最新回复(0)