Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring app not redirecting when foreign language characters (تشكيل) in url

I have a Spring app that is working correctly except for how it deals with foreign language characters that need to be included in the redirect url when the controller redirects the user.

If the controller redirects the user to the following URL, for example, it will fail:

http://localhost:8080/diacritic/تشكيل
like image 985
Hamza Avatar asked Aug 31 '25 23:08

Hamza


2 Answers

You need to re-encode the redirection target.

I have code like this:

String encodedId = URLEncoder.encode("中文", "UTF-8");
ModelAndView m = new ModelAndView(new RedirectView("/view/" + encodedId, true, true, false));
return m;

This worked for me.

like image 54
markm Avatar answered Sep 03 '25 13:09

markm


The path variable is decoded in ISO-8859-1. There are two things you can do to get around this.

  1. To see the actual UTF-8 decoded characters on the server, you can just do this and take a look at the value (you need to add "HttpServletRequest httpServletRequest" to your controller parameters):

    String requestURI = httpServletRequest.getRequestURI();
    String decodedURI = URLDecoder.decode(requestURI, "UTF-8");
    

    You can then do whatever you want, now that you have the right data on the server.

  2. You can also try adding a filter to your web.xml file.

<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>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
like image 22
11101101b Avatar answered Sep 03 '25 12:09

11101101b