I don't want to see you. The Master said: no code in mind, natural HD.
Let's get down to the truth.
Environment:
- Tomcat7, jdk6
- Spring + springmvc + mybatis
- Maven
Problem: There is characterencodingfilter, and the controller processes GET request garbled characters.
Controller:
@Controllerpublic class SearchController { @RequestMapping(value = "/s/{k}.html", method = RequestMethod.GET) public String search(@ModelAttribute("k") @PathVariable String k) { System.out.println("k=" + k); return "search/search"; }}
Page retrieval:
<Div class = "form-group"> <input type = "text" class = "form-control" style = "width: pixel; "Name =" K "id =" K "Placeholder =" what do you want to search? Sn ~ Hash code ~ "Value =" $ {k} "/> </div>
After checking on the Internet, the more reliable solution is to modify the server. xml configuration in Tomcat and add the configuration in the connector node:useBodyEncodingForURI="true" URIEncoding="UTF-8"
;
<Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="UTF-8"/>
The difference between uriencoding and usebodyencodingforuri is:
- Uriencoding is a unified re-encoding of the data of all GET requests,
- Usebodyencodingforuri recodes data based on the request. setcharacterencoding parameter of the page responding to the request. Different pages can be reencoded differently.
After modification, follow these steps:
OK, no problem! So I am looking for a jspspace provider to modify the configuration. This is what we say:
How can I be troubled by the cool smell of money as a rich technician!
So I collected the information myself, changed the code again, and re-encoded it in the controller:
Controller:
@Controllerpublic class SearchController { @RequestMapping(value = "/s/{k}.html", method = RequestMethod.GET) public String search( @PathVariable String k, HttpServletRequest request) { String key = EncodingUtil.encodeStr(k); request.setAttribute("key", key); return "search/search"; }}
Encodingutil:
public class EncodingUtil { public static String encodeStr(String str) { try { return new String(str.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } }}
Okay, the problem is solved.
However, this is obviously not the way that the goods are said to be invincible. Which big God has a better way? Thank you !!!
From code to code-free