Springmvc+hibernatevalidator, configuring error messages in the properties file Echo front page appears garbled in Chinese

Source: Internet
Author: User

Problem:

Background in SPRINGMVC use Hibernate-validator to do parameter check (validator specific use method see Google), with the properties file configured error message verification failure. The Chinese error message appears garbled when I find the echo back to the front page.

Pojo class for encapsulating parameters

public class UserReqBean {    @NotNull(message="{user.name.notnull}") private String userName;}

Configuration in the Valerrmsg.properties file

user.name.notnull=用户名不能为空

Configuration of the validator checksum error message in the Spring-mvc.xml configuration file

<!--check error message profile--<BeanId="Messagesource"class="Org.springframework.context.support.ReloadableResourceBundleMessageSource" ><!--resource file name--<PropertyName="Basenames" ><List><value>classpath:valerrmsg</ value> </list> </property> <!--resource File Encoding format--<property name= "fileencodings" value=" UTF-8 "/> <!--Cache time for resource file contents, per second- > <property name=" cacheseconds "value="/> " </BEAN>         

The code of the controller that handles the request and the front-end page code are not pasted, simply reading the error message back. All files of the project, including the properties file, are UTF-8. But the Chinese error message is garbled

Solve:

After diving (search) the Heart (Research) (song), found that the problem appears in the Spring-mvc.xml configuration. Looks like there's no problem:

<!-- 资源文件编码格式 --><property name="fileEncodings" value="UTF-8"/>

I based on the network on a springmvc+hibernatevalidator article on the above configuration, there is a Chinese garbled.
Later found an article mentioned garbled, processing mode is controller to get error information from ios-8895-1 to UTF-8. But "Lu Xun wrinkly, found that things are not reliable. jpg", frame for so long, how can there still exist this need to manually transcode the problem.
Then search for a reliable article, found to use this configuration:

<property name="defaultEncoding" value="UTF-8"/>

So add this configuration, OK!!!

Reason:

When configuring Messagesource, use this Reloadableresourcebundlemessagesource class, click to see the source code to find out where the problem.

public class ReloadableResourceBundleMessageSource extends AbstractMessageSource implements ResourceLoaderAware { ··· private String defaultEncoding; private Properties fileEncodings; ··· }

The original fileencodings is not a string type, so the above configuration is wrong. Let's look at the method of loading the properties file (for the sake of clarity, I deleted the part of the log):

Protected PropertiesLoadProperties (Resource Resource, String filename)Throws IOException {InputStream is = Resource.getinputstream (); Properties props =New Properties (); Properties encoding1;try {if (Resource.getfilename (). EndsWith (". xml")) {THIS.PROPERTIESPERSISTER.LOADFROMXML (props, is); }else {Read the properties file here String encoding =NullFirst take the code from the Fileencodings (my previous configuration is a string type, this will not be encoded)IfThis.fileencodings! =NULL) {encoding =This.fileEncodings.getProperty (filename); }//did not take the direct use of defaultencoding (Khan!) Not Configured before! So not taken) if (encoding = null) {encoding = this.defaultencoding; } if (Encoding! = null) { This.propertiesPersister.load (props, new inputstreamreader (is, encoding));} else {//not to get the code, you can only come here // and inputstream The default encoding is not UTF-8, but iso-8859-1 //so no matter how you toss it all have a problem, unless you would like to manually transcode //is said:" Blame me!! this.propertiespersister.load (props, is);} } encoding1 = props; } finally {is.close ();} return encoding1;}           

Figure out why, so here's the right solution, the XML configuration is as follows:

<!-- 资源文件编码格式 --><property name="fileEncodings" > <props> <prop key="classpath:ValErrMsg">UTF-8</prop> </props></property>

This allows the Fileencodings configuration item to be set correctly.

Further down, Fileencodings is the properties type, and properties inherits from Hashtable. From the place where the file was loaded, you can see that the key with value is the file name filename.

this.fileEncodings.getProperty(filename);

Therefore, you need to set the key of the prop tag to the file name classpath:valerrmsgpreviously configured (note including classpath). It also means that if you have more than one configuration file, you can set individual encodings for each file in the corresponding resource file list . If a file does not have the encoding, it will use the defaultencoding configuration, if not configured, the assumption by InputStream bohemian ...

To summarize:
1. For the Reloadableresourcebundlemessagesource class, the encoding is set correctly by setting the encoding for each resource file and setting the default encoding. Of course, if the project will only appear one encoding or only one resource file, you can simply and rudely set a defaultencoding.
2. Online code does not necessarily run up, encounter problems to search the solution on the Internet, it is better to follow the source to see the first.

PS: Younger brother Caishuxueqian, still in the hard study, just want to record the learning process encountered problems. may be very simple place to speak very wordy also please forgive, and many other omissions also please treatise.

Update

Previously, the SPRINGMVC version of the 4.1.1.release,reloadableresourcebundlemessagesource class was directly inherited from Abstractmessagesource. Presumably in a later version of 4.2, a new class was abstracted: The abstractresourcebasedmessagesource,defaultencoding attribute was placed in the abstract class.
The inheritance relationship from
Reloadableresourcebundlemessagesource–> Abstractmessagesource
Into Reloadableresourcebundlemessagesource–> abstractresourcebasedmessagesource–> AbstractMessageSource.
But the code logic for getting the file encoding hasn't changed.

Springmvc+hibernatevalidator, configuring error messages in the properties file Echo front page appears garbled in Chinese

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.