From code to code-free,
@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"> <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:
@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 !!!