I am using Spring ResourceBundle to retrieve message bundle from my .properties file. It constains special european characters like so:
Ü, ß. ä, ö, ü
MessageSouce bean is as below (I am making sure UTF-8 encoding is followed)
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:/META-INF/i18/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
When I try to retrieve the message in my Java code I am getting Junk characters.
If I use below code, it helps to recognize few characters, but rest are still showing as ??
return new String (bundleString.getBytes(), "UTF-8")
Then I used below to encode my properties file, but still no effect
native2ascii -encoding utf8 resources.utf8 resources.properties
I also tried to manually open my properties file in Notepad++ and set UTF-8 encode, but no effect.
I see a post here, having exact same problem as mine. But the solution uses PropertiesBundle, whereas I have to use Spring based solution only. However even the accepted answer in that link is not working for me, and giving junk characters.
Please suggest any possible solution.
I was having the same issue and #sunny_dev's answer worked for me.
I don't understand why there is not answer for this question yet so I'm updating the question.
#sunny_dev answer:
Karol, I solved the problem by opening and saving the .properties file
in TextPad as "UTF-8" encoding and "Unix" platform. I had taken that
same approach, however in Notepad++ without the positive outcome
earlier. – sunny_dev
Thanks again to #sunny_dev
Related
In I am using file upload with Spring rest and MultipartFile.
everything is fine, but issue on UTF-8. it is NOT supporting international file name - it is replacing characters to "?" like this.
I tried to use encoding Filter -org.springframework.web.filter.CharacterEncodingFilter , but no Luck.
But surprised, same above filter working fine for JSP form, without Rest
like this
<form:form method="POST" action="uploadFile" enctype="multipart/form-data" accept-charset="UTF-8">
Please help, how to support this utf-8 in Spring Rest.
There are three steps to support - file upload with international name in Rest.
Configure the server to support UTF
in my case Jboss 6.1
<property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
Use UTF-8 filter
org.springframework.web.filter.CharacterEncodingFilter
pass the file name as parameter, and in host read the file from parameter instead of MultipartFile
I'm using Spring Test to test my Spring Application, which is basically a REST webservice that serves JSON and some other uploaded media.
The unit tests are working fine in all developer's machines, and some of them tests for strings returning inside the JSON responses, and a little subset of them tests special characters.
The application is completely configured to use UTF-8 with everything, however the JSON response is returning with the wrong charset, so the test fails.
I tried to configure on /etc/default/jenkins the following line:
JAVA_ARGS="-Djava.awt.headless=true -Dfile.encoding=UTF8"
But I still get the same result.
I also had the chance to see what Charset.defaultCharset() was, when running a particular test... And it appeared something like US-ASCII...
Can someone please tell me what am I doing wrong?
I found out what was wrong.
In my Spring Test configuration, I was reading a DML.sql file to create initial test data, and the line that initialized the database was like this:
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:sql/DML.sql" />
</jdbc:initialize-database>
That left my configuration dependent from the server charset, and in my case I had that weird "US-ASCII" set in my server.
I made it independent by setting the encoding attribute to UTF-8:
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:sql/DML.sql" encoding="UTF-8" />
</jdbc:initialize-database>
So the values that came from the H2 database could be tested against values with special characters.
In my project code I can see FilterDispatcher being extended to set the default encoding as "UTF-8". My question is there any other better way we can do the same or this is the best way.
PS: The initial development was done in a country on Non-English windows PCs.
Actually there is struts.i18n.encoding constant which sets default locale and encoding scheme. You can set it in struts.properties file:
struts.i18n.encoding=UTF-8
or in struts.xml file:
<constant name="struts.i18n.encoding" value="UTF-8" />
BTW: struts.i18n.encoding should be set to UTF-8 by default in Struts2 anyway.
BTW no.2: FilterDispatcher is deprecated since Struts 2.1.3. So if you using version higher than that use StrutsPrepareAndExecuteFilter instead.
Here's my problem: I want to organize my resource bundle files so that I have the messages in one file and the labels in another. For this I created two .properties files which I declared in my spring configuration files. The actual declaration is this:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<array>
<value>strings.gui</value>
<value>strings.messages</value>
</array>
</property>
</bean>
The folder layout goes like this:
/WEB-INF/classes/ro/** - source files
/WEB-INF/classes/strings/* - i18n files (gui_ro.properties and messages_ro.properties, along with their _en versions)
Everything works great when I display labels from gui_xx.properties but it fails to display messages from messages_xx.properties files. The error message in the server console is
ResourceBundle [strings.messages] not found for MessageSource: Can't find bundle for base name strings.messages, locale ro
I'm using JSTL with TilesView, and the problem occurs regardless of what tag I use to display the text - fmt:message key=... or spring:message code=....
Can anybody help pe with this?
Thank you very much and have a good day.
I think the correct basenames should be:
<value>classpath:/ro/gui</value>
<value>classpath:/strings/messages</value>
Can you please try these values for basenames.
I am building an application using spring mvc and jpa using jboss7 and mysql in eclipse ide. I am having a strange problem. All my jsp pages are encoded with charset: utf8, which I think is working correctly. But whenever I try to post a data from the jsp to the controller, my data gets encoded with a different encoding style. I tried to look for the header using firebug and was astonished to see that the post request has a header with content-type : "text/plain;charset=ISO-8859-1". I have already configured the SetCharacterEncodingFilter for UTF-8 in my web.xml (it is the first filter). But still the problem exists.
I also set "org.apache.catalina.connector.URI_ENCODING" to value="UTF-8".But in vain .
Also I have added bean messageSource with property defaultEncoding set to "UTF-8".
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" >
The problem still exists. Please help
Thanks in advance.
The request header is set by the browser, so your application can't control it. Usually, in your HTML form you could put an accept-charset=utf-8 attribute to specify the encoding, but that doesn't necessarily work. See this question Setting the character encoding in form submit for Internet Explorer.
you should need to set the encoding of the JVM like follow :
-Dfile.encoding=UTF-8 -Dfile.io.encoding=UTF-8 -DjavaEncoding=UTF-8
thus there wont be any doubt at all.