Struts1.x Series Tutorials (3): Attribute (Resource) file the solution of garbled problem

Source: Internet
Author: User
Tags empty resource

First, use the NATIVE2ASCII command to enable resource files to support Chinese

From the Errordescription.properties file in the previous article, you can see that all the error messages are in English. There is no problem displaying these English messages on the client. But what if you change the English error message to the appropriate medium error message?

Before you modify, let us select the Errordescription.properties file, right-click, select the Properties menu item in the right-click menu, open the Property Settings dialog box, and select "Resource" in the items listed on the right. Take a look at the encoding format of the "Text file encoding" in the lower-right corner, and if the encoding format is not UTF-8 or GBK (also including GB2312, GB18030), modify the encoding to UTF-8 or GBK, Otherwise, you cannot use the MyEclipse property editor to enter Chinese. After the encoding format of the property file is set, the contents of the Errordescription.properties file are modified to the Chinese hint information as shown in the English version of the file.

Chinese version of the Errordescription.properties

error.user.blank= user name cannot be empty.

The error.password.blank= password cannot be empty.

error.password1.confirmation= passwords do not match.

Error.email.blank=email cannot be empty

Error.email.invalid= is not a correct email address.

After you restart Tomcat, enter the following URL in IE:

http://localhost:8080/samples/simpleValidation.jsp

When the information input interface appears, clicking the "Submit" button will show the effect shown in Figure 1.

Figure 1

Perhaps many readers will be surprised. We have changed the encoding format of the property file to UTF-8 or GBK, and the input of Chinese in the myeclipse can be normal display, and the layout of the page in the encoding format is consistent with the encoding format in the property file, then why is the output garbled?

The reason for this problem arises from the way Java handles various types of files. Java when processing *.java files, whether the encoding format of *.java files is UTF-8 or GBK. When compiled with Javac.exe, it is converted to a UCS-2 encoding format. Therefore, *.java files will not appear garbled. While Java handles property files, it can only handle ISO 8859-1 (also known as latin-1 or "Western European language") or UCS-2 (\uxxxx format) encoding format. Therefore, if you include characters in other encoded formats in the property file, such as Gbk,java will still interpret the characters in the ISO 8859-1 encoding format (if you encounter a character that starts with "u, it will be handled in the UCS-2 encoding format), you will see the display shown in Figure 1.

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.