From code to code-free,

Source: Internet
Author: User

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 !!!




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.