开发者

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

开发者 https://www.devze.com 2023-02-16 15:31 出处:网络
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 re开发者_StackOverflow社区direct url when the controller redirect

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 re开发者_StackOverflow社区direct 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/تشكيل


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消