SpringMVC 中文乱码的解决方案
程序员文章站
2022-06-15 17:28:26
背景举个例子,出现中文乱码的例子:提交表单的时候。表单<%@ page contenttype="text/html;charset=utf-8" language="java" %>&l...
背景
举个例子,出现中文乱码的例子:提交表单的时候。
表单
<%@ page contenttype="text/html;charset=utf-8" language="java" %> <html> <head> <title>title</title> </head> <body> <form action="/c02/t1" method="post"> <input type="text" name="name"> <input type="submit"> </form> </body> </html>
业务控制类
package controller; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; @controller public class controllerdemo02 { @requestmapping("/c02/t1") public string test1(string name, model model){ system.out.println(name); model.addattribute("message",name); return "test"; } }
结果:出现乱码
解决方案
使用自己写的过滤器
自定义一个filter过滤器,去过滤乱码
package 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() { } }
在web.xml中配置这个过滤器
<filter> <filter-name>encodingfilter</filter-name> <filter-class>filter.encodingfilter</filter-class> </filter> <filter-mapping> <filter-name>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
使用springmvc提供的过滤器
直接在web.xml中配置(固定的)
<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>
如果上面两种方法还实现不了,在试试以下这个方法,大神编写的
package 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; public class genericencodingfilter implements filter { public void destroy() { } 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); } 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; } // 对需要增强方法 进行覆盖 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(); } //取一个值 public string getparameter(string name) { map<string, string[]> parametermap = getparametermap(); string[] values = parametermap.get(name); if (values == null) { return null; } return values[0]; // 取回参数的第一个值 } //取所有值 public string[] getparametervalues(string name) { map<string, string[]> parametermap = getparametermap(); string[] values = parametermap.get(name); return values; } }
以上就是springmvc 中文乱码的解决方案的详细内容,更多关于springmvc 中文乱码的资料请关注其它相关文章!