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.

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

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

2 Comments

I want to allow the user surg to 'mysite/cityname.html' ant get relevant results for cityname. But cityname might be in hebrew.
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.
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>

8 Comments

I already have this filter. I also have filter-mapping for <url-pattern>/*</url-pattern>.
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..
I am sure.. does '/*' refer also to paths like '/services/cities/פת.html' ?
yes, it should. You can check the log to see if it is being intercepted or not and if there are any warnings..
Which log? I am new in spring mvc.. Where can I find it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.