Announcement Announcement Module
Collapse
No announcement yet.
Help: confused about RequestMappingHandlerMapping and UTF-8 (Spring 3.1.2) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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).

    Comment


    • #3
      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/测试]

      Comment

      Working...
      X