I wrote a small application on Spring MVC where users can enter their data into the database. When launched locally, everything is normally displayed, and when launched on a VPS, the data that is entered by the controller into the database is displayed as (?????? ?? ????).

Added to pom.xml:

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> ..... </properties> 

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

In appconfig-mvc.xml:

 <beans:bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <beans:property name="messageConverters"> <beans:array> <beans:bean class = "org.springframework.http.converter.StringHttpMessageConverter"> <beans:property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" /> </beans:bean> </beans:array> </beans:property> </beans:bean> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> ....... 

I tried to write when creating a database on the server

CREATE DATABASE mydb
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;

Anyway, there are questions in the database (when I do a SELECT from the database - there are also questions in place of the Russian text).

EDITED: on the JSP page where the save comes from is also worth:

 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

connection to the database:

 <util:properties id="application" location="classpath:database.properties"/> <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <beans:property name="driverClassName" value="#{application.driverClassName}" /> <beans:property name="url" value="#{application.url}" /> <beans:property name="username" value="#{application.username}" /> <beans:property name="password" value="#{application.password}" /> <beans:property name="maxIdle" value="-1"/> <beans:property name="maxActive" value="-1"/> <beans:property name="maxOpenPreparedStatements" value="-1"/> <beans:property name="maxWait" value="20000"/> <beans:property name="validationQuery" value="SELECT 1"/> <beans:property name="testOnBorrow" value="true"/> </beans:bean> 
  • characterEncoding when connecting to the database worth? Add config as connect to the database - MrFylypenko
  • Added data from appconfig-data.xml - Dmitry
  • Try adding to the <beans:property name="characterEncoding" value="utf8" /> configuration - MrFylypenko
  • invalid property - Dmitry
  • then try specifying the url to connect to the database like this: jdbc:mysql://localhost:3306/?useUnicode=yes&characterEncoding=UTF-8 - MrFylypenko

1 answer 1

Thanks for the suggested options! Found a solution here . We do everything as it says in the last paragraph and everything works. Apparently the default encoding when installing MySQL on Ubuntu is not set to support the Russian language. However, when manually inserting data into the database, the text is displayed in hieroglyphs. But this is a completely different story (why insert it manually?).

In short, for example, we will replace the encoding with cp1251, then in the configuration file my.cnf, which is located in the /etc/mysql/my.cnf directory, look for the [mysqld] module and change it to:

 [mysqld] skip-character-set-client-handshake character_set_client=cp1251 character_set_server=cp1251 

We restart MySQL (reboot must always be done after making any changes): service mysql restart. We write in Russian letters what we need)

  • write in more detail, so that you can resolve the issue without following the link - MrFylypenko
  • @MrFylypenko wrote) - Dmitry