From code to code-free

Source: Internet
Author: User

 

I don't want to see you. The Master said: no code in mind, natural HD.

Let's get down to the truth.

Environment:

  1. Tomcat7, jdk6
  2. Spring + springmvc + mybatis
  3. 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

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.