Results 1 to 3 of 3

Thread: Help: confused about RequestMappingHandlerMapping and UTF-8 (Spring 3.1.2)

  1. #1
    Join Date
    Aug 2012
    Posts
    2

    Question Help: confused about RequestMappingHandlerMapping and UTF-8 (Spring 3.1.2)

    1, I send a request through firefox: http://localhost:8080/search/q/q/测试
    2, Through the Log, I get this:
    21:39:16.933 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'qqq' processing GET request for [/search/q/q/测试]
    21:39:16.933 [http-bio-8080-exec-4] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /q/q/测试

    RequestMappingHandlerMapping uses the UrlPathHelper to code the URI. I checked the source code of UrlPathHelper and it use "ISO-8859-1".

    Code:
    <beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
            <beans:property name="urlPathHelper">
                <beans:bean class="org.springframework.web.util.UrlPathHelper">
                    <beans:property name="urlDecode" value="true" />
                    <beans:property name="defaultEncoding" value="UTF-8" />
                </beans:bean>
            </beans:property>
        </beans:bean>
    
    <mvc:annotation-driven />
    Then I used this and it's has no effect in xxx-servlet.xml.

    3, controll works well. But I use the 3rd part as an input parameter:
    Code:
    @Controller
    @RequestMapping("/q")
    public class SearchController {
    ...
    @RequestMapping(value = "/q/{keyword}")
        @ResponseBody
        public HashMap<String, ArrayList<Game>> searchq(@PathVariable("keyword") String keyword) {
        try {
                System.out.println(" -------------------------- " + new String(keyword.getBytes("ISO-8859-1"), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    ...
    now the parameter "keyword" is 测试. It's coded with "ISO-8859-1" as the "System.out.println" is right. Here I just want to get a "UTF-8" parameter "keyword".

    Very apprecaited if anyone who can advise about this or give me a full example. For example, resolve this by xml not hardcoding.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Try adding a CharacterEncodingFilter to your filters list and force the encoding to UTF-8... Also make sure that the request you are sending is actually UTF-8 (by specifing the encoding in the page).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2012
    Posts
    2

    Default

    Thanks very much. I have already added the CharacterEncodingFilter in web.xml and URIEncoding="UTF-8" for TomCat7 before encountered this problem.
    I think DispatcherServlet can get the right value.
    21:39:16.933 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'qqq' processing GET request for [/search/q/q/测试]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •