Spring and poor UTF-8 coding

In our web application, we ran into a bad coding problem. To reproduce this problem, the user selects non-Unicode in the encoding of the browser (as an example in chrome → More tools-> encoding-> Koi8) and tries to install Cyrillic text. The balls were ruined when they go to the controller (just checked for debugging), and even it is stored incorrectly and not displayed correctly.
We followed all the recommendations: http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html and it seems that this is a problem with sending the encoding content type application/x-www-form-urlencoded. Because it is impossible to set the encoding during such forms.
As an example, if you send the same data using json and set the necessary type of content, everything is stored correctly. We also tried an example with this article: http://www.codejava.net/frameworks/spring/spring-mvc-form-handling-tutorial-and-example and additionally added a UTF8 filter in the following way:

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
        ServletException {

            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
    chain.doFilter(request, response);
}

But the same problem was reproducible. Can anyone suggest how to solve this problem?
Is it possible to correctly handle the mentioned use case in Spring MVC, because we tried with a simple example and it does not seem to work. Does this use a case of changing browser encoding at all?

+4
source share
3

accept-charset="UTF-8" .

spring. (1.1).

https://issues.apache.org/jira/browse/STR-1636

. jQuery

jQuery( document ).ready(function() {
   jQuery("#formSelector").attr("accept-charset", "UTF-8");
});

, . , , , . , (, KOI8-U ).

accept-charset

0

: web.xml

<filter>
     <filter-name>encoding-filter</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>
 </filter>

 <filter-mapping>
     <filter-name>encoding-filter</filter-name>
     <url-pattern>/*</url-pattern>
 </filter-mapping>
Hide result

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

, jstl,

+4

- (.. String.getBytes() new String(bytes) . , , , Spring Boot UTF-8 , , , UTF-8.

. , , HTML, UTF-8, . " UTF-8". , HTML (.. Jsp) / , . application/x-www-form-urlencoded, Javascript (, , ). , HTML , , , . UTF-8 ( thats whats HTML), , (.. ?UTF-8Name=KoiValue&UTF-8Name=KoiValue). , , , , ( ) Koi8.

Thus, if you absolutely must support a different character encoding, you probably should use multipart/form(you specify this in the attribute enctypein the form element) AND DO NOT use the encoding filters that set UTF-8 as they are likely to cause corruption.

0
source

All Articles