Directly on the job to meet the problem it:
In the SPRINGMVC project, the foreground form is sent through a GET request. Chinese is garbled after receiving in the background
So I configured a spring filter.
Org.springframework.web.filter.CharacterEncodingFilter
But only to deal with the post request of the Chinese garbled problem, online said there is a simple way is to modify the Tomcat Server.xml file, but this method is too reliable, or another think he sent it!
So I started to write a filter to deal with garbled problems by wrapping
Javax.servlet.http.HttpServletRequestWrapper
Class rewrite request in the method of obtaining parameters and re-coding the parameters to solve the problem, it should be no problem, but it has not worked for a long while, I filtter in the
Chain.dofilter (...)
Before and after the printing request parameters, display transcoding successful Ah, what's going on?
After a moment of thinking, the original is a problem in the processing logic, in the program did not do to determine whether to turn over the code, resulting in the possibility of the springmvc of the automatic loading request parameters may be in the process of the parameters I have been transferred code again so certainly not!!
Here is the code, the attention of the place marked red bar:
/** * encode the data for requests and responses * * @author aracwong * */public Class characterencodingfilter implements filter {public static final string PARAM_ENCODING_NAME = "ENCODING";p ublic static final string utf_8 = "UTF-8";p ublic static final string iso_8859_1 = "iso-8859-1";p rivate String encoding = characterencodingfilter.utf_8;public void dofilter (ServletRequest request, servletresponse response,filterchain chain) throws IOException, servletexception {httpservletrequest requ = (HttpServletRequest) request; httpservletresponse resp = (HttpServletResponse) response;request.setcharacterencoding ( Characterencodingfilter.utf_8); response.setcharacterencoding (characterencodingfilter.utf_8);// Using the wrapper class myhttpservletrequest myrequest&Nbsp;= new myhttpservletrequest (requ); Chain.dofilter (MYREQUEST,&NBSP;RESP);} Public void init (Filterconfig filterconfig) throws servletexception {string _encoding = filterconfig.getinitparameter (Characterencodingfilter.param_encoding_name); (Stringutils.isnotblank (_encoding)) {encoding = _encoding;} private class myhttpservletrequest extends httpservletrequestwrapper {private httpservletrequest request;private boolean flag = false;public Myhttpservletrequest (httpservletrequest request) {super (request); this.request = request;} @Overridepublic string getparameter (string name) {Map<String, String[]> Map = getparametermap ();if (map != null) {String[] values = Map.get (name);if (values != null) {return values[0];}} Return null;} @OvErridepublic string[] getparametervalues (String name) {Map<String, String[]> map = getparametermap ();if (map != null) {string[] values = map.get (name);if (values != null) {return values;} Return null;} @Overridepublic map<string, string[]> getparametermap () {if ("Post". Equalsignorecase (Request.getmethod ())) {// post Way to submit try {request.setcharacterencoding (encoding);} catch (unsupportedencodingexception e) {e.printstacktrace ();} Return request.getparametermap ();} else {// get Way to submit map<string, string[]> parammap = Request.getparametermap ();if (!flag) {for (map.entry<string, string[]> me : parammap.entryset ()) {string[] values = me.getvalue ();for (int i = 0; i < values.length; i++) {try {values[i] = new string (Values[i].getbytes (CharacterEncodingFilter.ISO_ 8859_1), encoding);} catch (unsupportedencodingexception e) {e.printstacktrace ();}} Flag = true;} Return parammap;}}} Public void destroy () {}}
This code is very necessary: if the Request object has been transcoded, the next time to call the request parameters to make a judgment, no longer transcoding, or the UTF-8 data to iso8859-1 to the conversion to UTF-8 will definitely error, it is a valuable error experience
Private Boolean flag = false;
On the solution of garbled problem in Java Web development