3

I am using spring mvc. I am surfing to:

http://localhost:8080/services/cities/פת.html

notice that פת is in hebrew and not english.
My controller is:

@RequestMapping(value="/services/cities/{name}", method=RequestMethod.GET)
public @ResponseBody List<SelectElement> getCities(@PathVariable String name) {
    List<SelectElement> elements=null;
    ...
    ...
    return elements;
}

The problem is that the controller receives פת and not the correct characters.
How can I fix it?

Even if I surfing to: http://localhost:8080/services/cities/%D7%A4%D7%AA.html I get this problem.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Naor
  • 23,465
  • 48
  • 152
  • 268

3 Answers3

5

If you are using tomcat, then you have to specify the URL encoding for requests by adding URIEncoding="UTF-8" on <Connector> in the Tomcat server.xml config, as described here:

http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8

nickdos
  • 8,348
  • 5
  • 30
  • 47
  • 1
    I prefer not to touch tomcat's server.xml since I am going to host this app on a server where I would not be able to change this file. – Naor Mar 08 '12 at 12:32
  • The container (tomcat) handles the encodings of the text in the URLs not your app - it handles the encodings of the body content and response. I don't think you have a choice in this case (but happy to be corrected). – nickdos Mar 09 '12 at 01:25
  • This actually works but I this solution is not good enough since I have to configure server that is not mine. I don't know if I could do such thing. If there is another solution - I will be glad to hear. – Naor Mar 12 '12 at 07:58
1

Here you have an interesting documentation about encoding requests in Tomcat

Something like CharacterEncodingFilter will only work in POST requests. You need to change Tomcat (or another container) configuration in order to use an URI enconding different from ISO-8859-1. But this won't work in all browsers, it depends on how they encode the URI too.

So my recommendation is always use simplest characters as possible. If you tell us what are you trying to achieve maybe we can find some better solution (I suppose you don't need the user too type the name of the city directly in address bar).

sinuhepop
  • 20,010
  • 17
  • 72
  • 107
  • I want to allow the user surg to 'mysite/cityname.html' ant get relevant results for cityname. But cityname might be in hebrew. – Naor Mar 08 '12 at 12:34
  • Naor: the user will insert city name directly in address bar or in a textbox in your application? For the first, you can try what @nickdos and me are saying, but it won't work with all browsers. – sinuhepop Mar 08 '12 at 13:18
0

Use the CharacterEncodingFilter filter...

<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
</init-param>
<init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
</init-param>

Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
  • I already have this filter. I also have filter-mapping for /*. – Naor Mar 07 '12 at 20:46
  • Are you sure that all your requests are being intercepted by this filter? To my knowledge, this should solve the problem unless there is something else going on.. – Teja Kantamneni Mar 07 '12 at 20:50
  • I am sure.. does '/*' refer also to paths like '/services/cities/פת.html' ? – Naor Mar 07 '12 at 21:00
  • yes, it should. You can check the log to see if it is being intercepted or not and if there are any warnings.. – Teja Kantamneni Mar 07 '12 at 21:01
  • Which log? I am new in spring mvc.. Where can I find it? – Naor Mar 07 '12 at 21:13
  • The application log. When you get a new request, the application log (set to debug) and see – Teja Kantamneni Mar 07 '12 at 21:43
  • I add an interceptor, put there a breakpoint and the request url was: /services/cities/%D7%A4%D7%AA.html. I add the following watch: URLDecoder.decode("/services/cities/%D7%A4%D7%AA.html", "UTF-8") and then I get /services/cities/פת.html as it should be. How can I use this info to fix it? – Naor Mar 07 '12 at 22:06
  • According to http://stackoverflow.com/a/8153988/289246 : The CharacterEncodingFilter does exactly that but has no influence on path parameters. What do you think? – Naor Mar 07 '12 at 22:55